将光标从单独的类放置在MaskedTextBox vb.net的开头

时间:2017-03-27 15:40:56

标签: vb.net dynamic controls maskedtextbox

我有一个创建控件的类:

NewNS = MsgBox("Would you like to pick new Force ID's in the North-South direction?", vbYesNo, "Roof North-South")
    If NewNS = vbYes Then
    Worksheets("ShearWalls").Activate
    Do Until CheckNS = vbYes
        On Error Resume Next
        Set rngNS = Application.InputBox("Select your Force ID's (North-South)", "Roof North-South", Type:=8)
        On Error GoTo EndJump
        CheckNS = MsgBox("The cells selected were " & rngNS.Address & " for North-South. Is this correct?", vbYesNo, "Roof North-South")
    Loop
    Worksheets("Reference").Range("B739").Value = rngNS.Address
    Else
        Setrng = Worksheets("Reference").Range("B739").Value
        Set rngNS = Range(Setrng)
    End If

我有一个动态表单,使用以下方法调用这些控件:

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

在表格中:

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

我的问题是动态创建的控件不会将光标放在MaskedTextBox的开头。我尝试了注释的方式,但它们没有用。或者,如果文本框已满,我希望光标位于文本的末尾。我不能让光标做任何事情,只能留在点击的地方。

1 个答案:

答案 0 :(得分:0)

在一些帮助下,这是最后的答案。

  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