1

I will preface this question/information with the fact that I am by no means a .NET/VB/Windows programmer. I am supporting legacy code which at times just confounds me as to how it works...sometimes. Like this time.

Details:

  • Visual Studio 2008
  • Compact Framework 2.0
  • Visual Basic Application
  • Some libraries in C#, but not pertinent for this question.

In a nutshell, my problem is that I put text/content into a field. And when the field gets focus, the cursor is at the beginning of the field instead of the end of the field. This means that the user cannot edit what is in the field, but just re-type over it.

When searching for solutions, I found a lot of suggestions to SelectionStart. Unfortunately, the fields that I am using aren't setup as textboxes. Or maybe they are at their core, but I don't see that or know how to access it. I have access to a Control. Hopefully that makes sense.

To give the field focus, I have to set Enabled to False for the previous field, and then call Focus() for the field that I've preloaded with content. Like:

Public Structure UDT_CTL
    Dim ctl As Control
    Dim typ As CTLTYP
    Dim Tag As Integer
    Dim Text As String
    Dim evnSet As Boolean 
    Dim idctlbase As Integer
End Structure

Public Structure UDT_CTLPAG
    Dim nlbl As Integer
    Dim npic As Integer
    Dim nbtn As Integer
    Dim ntxt As Integer
    Dim nlblm As Integer
    Dim MapidBtnVer() As Integer
    Dim MapidBtnHor() As Integer
    Dim Ctl() As UDT_CTL
End Structure

Public gCtl As UDT_CTLPAG

gCtl.Ctl(CTL.FIELD2).ctl.Text = tmpVal   'Load content into field                      

gCtl.Ctl(CTL_MAP.TXT_FIELD1).ctl.Enabled = False   ' Disable previous field                     

gCtl.Ctl(CTL_MAP.TXT_FIELD2).ctl.Focus()   'Set focus on field with content

So... As you can see, I'm not working with the textbox directly. And I'm not sure I have all of the terminology correct.

But, beyond setting focus on the field, how can I move the cursor to the end of the text I set in the field? I'd like the end user to be able to edit what is there, not just re-type over it.

Thank you!!

Opie
  • 31
  • 8
  • That seems to be a custom control with a textbox as control inside. Try to overwrite the GotFocus handler and use Select(0, TextBox1.Text.Length) to move the cursor to the end. If you do not know how, post the complete code. – josef Aug 10 '18 at 05:07

1 Answers1

0

The code is incomplete. it is not clear what type of control is used:

Dim ctl As Control

And the used array variables are also unclear:

CTL.FIELD2

and

CTL_MAP.TXT_FIELD2

If ctl is a Textbox or based on a Textbox you may use the following:

AddHandler (gCtl.Ctl(CTL_MAP.TXT_FIELD2).ctl.GotFocus), AddressOf myOnGotFocus

and

Sub myOnGotFocus(ByVal sender As Object, ByVal e As System.EventArgs)
    DirectCast(sender, TextBox).SelectionStart = DirectCast(sender, TextBox).Text.Length + 1
End Sub

BTW: the default Compact Framework Textbox control does not select the whole text when the TextBox gets the focus, the caret (insertion point) is placed before the text!

UPDATE: as we do not know how this is be used, I assumed control being a TextBox:

Public Structure UDT_CTL
   Dim ctl As Control
   Dim typ As CTLTYP
   Dim Tag As Integer
   Dim Text As String
   Dim evnSet As Boolean 
   Dim idctlbase As Integer
End Structure

Please show the code where this structure is used if you need more help. Best would be you post the code temporary at github and delete after we fixed it.

josef
  • 5,951
  • 1
  • 13
  • 24
  • Yes... the code is incomplete. I tried to pull together as much of it as I could to show what is being used. Again, I'm not a VB developer. Just supporting old code that I had no hand in putting into place. So, is the example the you show above using the direct cast a way to move the Caret to the end of the text? – Opie Aug 10 '18 at 14:08
  • I wasn't able to use a DirectCast. I keep getting invalid cast exceptions. So the control used isn't directly a TextBox. But I'm not sure how to figure out what it is. – Opie Aug 10 '18 at 16:21
  • Can't you set a breakpoint on that line and check what the object type is? Failing that, add logging and write out what the control actually is (see https://stackoverflow.com/questions/3533774/vb-net-get-class-name-of-a-instance if you don't know how to find that info). – tcarvin Aug 14 '18 at 13:21