Unanswered Hi

  • lunes, 04 de agosto de 2008 8:48
     
     

     

    please let me know how to get the keyboard cursor position in c#.net????

     

    I want to give a feature like when the user enter data ;i want to create a popup window underneath the position where the last key has been typed.

     

    Using the cursor class in .NET i get only the mouse cursors x and y posistions,but not the current keyboard focus posistion.

     

    Is there any way to retrive the keyboard blink cursor pos?

     

    Thanks
    prasanth.k

Todas las respuestas

  • lunes, 04 de agosto de 2008 9:54
     
     

    Hi!

     

    This will give you the cursor position in a Windows Form: http://msdn.microsoft.com/en-us/library/system.windows.forms.cursor.position.aspx

     

    Regards,

  • martes, 05 de agosto de 2008 5:50
     
     

    Hi,

     

    As i have already mentioned the Cursor.posistion.X gives the mouse cursor,not the current control with focus.

    I have used the following code to test this.

     

    private void timer1_Tick(object sender, EventArgs e)

    {

    Console.WriteLine(Cursor.Position.X);

    Console.WriteLine(Cursor.Position.Y);

    }

    i have 20 textboxes in my Form1 ,but when i move from one textbox to another ,i am unable to get current focused controls posistion.

     

    Any Help??

     

    regards

    prasanth.k

     

     

  • martes, 05 de agosto de 2008 5:59
     
     

     

    To be more specific, trying to get the blinking cursor posistion from a command window.

     

    regards

    prasanth.k

  • martes, 05 de agosto de 2008 7:55
     
     
     Prashanthmallu wrote:

    Hi,

     

    As i have already mentioned the Cursor.posistion.X gives the mouse cursor,not the current control with focus.

    I have used the following code to test this.

     

    private void timer1_Tick(object sender, EventArgs e)

    {

    Console.WriteLine(Cursor.Position.X);

    Console.WriteLine(Cursor.Position.Y);

    }

    i have 20 textboxes in my Form1 ,but when i move from one textbox to another ,i am unable to get current focused controls posistion.

     

    Any Help??

     

    regards

    prasanth.k

     

     

    Then get the X and Y of the control that has focus:

     

    Code Snippet

    foreach (Control c in this.Controls)

    {

    if (c.Focused)

    {

    MessageBox.Show(c.Top + " " + c.Left);

    }

    }

     

     

     

     

  • martes, 05 de agosto de 2008 9:21
     
     

    To be more specific, trying to get the blinking cursor posistion from a command window or a notepad,Am i clear now?

     

    regards

    prasanth.k

     

  • martes, 05 de agosto de 2008 9:24
     
     
     Prashanthmallu wrote:

    To be more specific, trying to get the blinking cursor posistion from a command window or a notepad,Am i clear now?

     

    regards

    prasanth.k

     

    Getting the position of the control is the closest thing to what you wanna achieve. You cannot get the blinking cursor's position in a textbox relative to the form.

     

    Regards, 

  • martes, 05 de agosto de 2008 16:37
     
     
    Well, actually I think you can.
    Get the position of the focused TextBox just like naicul suggested. then, use TextBox.GetPositionFromCharIndex(int index) method to get the position of the carret (blinking cursor) relative to the TextBox, and add it to the position of the TextBox (which should be relative to the form).

    the parameter you need to send to the GetPositionFromCharIndex(int index) method is the TextBox.SelectionStart property.

    hth,
    Lior.
  • jueves, 14 de agosto de 2008 11:15
     
     

    You can use

     

    [DllImport("user32.dll")]

    static extern bool GetCaretPos(ref Point lpPoint);

     

    To get the blinking Position

  • martes, 20 de marzo de 2012 6:59
     
     

    Hi Lucian,

    I am facing a problem, I had created a thread but did not get solution. I think you may solve my problem.

    Problem:  I want to set a focus on a text box after page load  and page is being generating dynamically, all controls coming from database.

                   I set the focus on desired text box but problem is the value is missing from this text box and cursor is also not appearing.

     below is generalize XAML code that I am using to generate more than 1 text box:

    <DataTemplate  x:Key="TextFieldTemplate" >

                <TextBox Width="150" x:Name="txt1"
                         MaxLength="{Binding Path = MaxLength}"
                         Visibility="{Binding Path=ParameterVisibility}"
                         HorizontalAlignment="Left"                   
                         Margin="1" 
                         Tag="{Binding Path=HierarchyTag}"
                         Text="{Binding Path=SelectedValue.Value,  UpdateSourceTrigger=PropertyChanged}">
                    <TextBox.Style>
                        <Style TargetType="{x:Type TextBox}"
                               BasedOn="{StaticResource {x:Type TextBox}}">
                        <Setter Property="IsReadOnly"
                                    Value="True" />
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Path=IsEnabled}"
                                             Value="True">
                                    <Setter Property="IsReadOnly"
                                            Value="False" />
                                </DataTrigger>
                                <DataTrigger Binding="{Binding Path=IsFocusable}" Value="True">
                                    <Setter  Property="FocusManager.FocusedElement" Value="{Binding ElementName = txt1}" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </TextBox.Style>
                </TextBox>
            </DataTemplate>