0

I have a class that creates the controls:

Overrides Sub createControls()
    _GlobalCounter = 0
    Dim lblName As New Label
    lblName.Text = "Store Name"
    Dim txtName As New MaskedTextBox
    txtName.Mask = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
    txtName.Text = name
    addControl(lblName, txtName)
    '  txtName.SelectionStart = txtName.Text.Length()
    '  txtName.SelectionLength = 0
    '  txtName.SelectionStart = 0
    '  txtName.SelectionLength = 0

        addEntityControls()
End Sub

I have a dynamic form that calls those controls using the following method:

 Public Function returnEditorControls() As Control()
        createControls()
        Return _Controls
    End Function

In the form itself:

Public Sub loadEditor()
    Dim controlCount As Integer = 0
    createControls()
    tlpMain.Controls.Clear()
    tlpMain.ColumnStyles.Clear()
    tlpMain.RowStyles.Clear()
    tlpMain.ColumnCount = _ColumnCount
    Dim intControls As Integer = myControls.Length()
    Dim intTotalRows As Integer = intControls / _ColumnCount
    If (intControls Mod _ColumnCount) > 0 Then
        intTotalRows += 1
    End If
    tlpMain.RowCount = intTotalRows
    For row = 0 To intTotalRows - 1
        tlpMain.RowStyles.Add(New RowStyle(SizeType.Percent, 100 / tlpMain.RowCount()))
        For column = 0 To _ColumnCount - 1
            If controlCount < intControls Then
                tlpMain.Controls.Add(myControls(controlCount), column, row)
                controlCount += 1
            End If
        Next
    Next
End Sub

My problem is that the dynamically created controls will not place the cursor at the beginning of the MaskedTextBox. I attempted the ways commented out, and they did not work. Alternatively, if the textbox is full, I want the cursor at the end of the text. I cannot make the cursor do anything but stay where it is clicked.

Ralph Maurmeier
  • 101
  • 1
  • 11
  • Check this out [link](http://stackoverflow.com/questions/1435376/masked-textbox-input-align-left) – 3vts Mar 27 '17 at 16:13
  • 1
    `txtName.Select(txtName.TextLength, 0)` is probably what you are after. – Bugs Mar 27 '17 at 16:14
  • 1
    @Bugs : Almost. That places the caret in the end, but he wants it in the beginning. :) – Visual Vincent Mar 27 '17 at 16:40
  • 1
    @VisualVincent I thought that but this bit got me. _if the textbox is full, I want the cursor at the end of the text_ so thought I'll comment that and OP can adapt :) – Bugs Mar 27 '17 at 16:59
  • @Bugs I tried the line of code but it didn't change the position of the cursor compared to where clicked, with or without text. – Ralph Maurmeier Mar 27 '17 at 22:06
  • @3vts The control is not located on the form, it is called into the form dynamically. – Ralph Maurmeier Mar 27 '17 at 22:07

1 Answers1

0

With some help, here was the final answer.

  Public Function returnEditorControls() As Control()
        createControls()
        For Each mtb As MaskedTextBox In _Controls.OfType(Of MaskedTextBox)
            AddHandler mtb.Click, AddressOf fixMTBCursor
        Next
        Return _Controls
    End Function

Public Sub fixMTBCursor(sender As Object, e As EventArgs)
    If TypeOf sender Is MaskedTextBox Then
        Dim txt As MaskedTextBox = DirectCast(sender, MaskedTextBox)
        If txt.SelectionStart > txt.Text.Length Then
            txt.Select(txt.Text.Length, 0)
        End If
    End If
End Sub
Ralph Maurmeier
  • 101
  • 1
  • 11