Home > OS >  When I load form inside panel textbox default properties get lost
When I load form inside panel textbox default properties get lost

Time:11-17

when I load form inside panel using code

With Form_brand
    .TopLevel = False
    Panel2.Controls.Add(Form_brand)
    .BringToFront()
    .Show()
End With

textbox inside form does not show its default behaviour like when i click inside textbox cursor show at starting of the text instead of place where I click the mouse. Another problem is that when I move mouse pointer with clicking mouse button text should be selected but this does not happen.

If I open form simply using code

Form_Brand.show()

textbox shows it default behaviour. What should I do?

CodePudding user response:

Maybe you can use this:

Private Sub TextBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles TextBox1.MouseDown
    TextBox1.SelectionLength = 0
    TextBox1.SelectionStart = TextBox1.GetCharIndexFromPosition(e.Location)
End Sub

Private Sub TextBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles TextBox1.MouseMove
    If e.Button = MouseButtons.Left Then
        TextBox1.SelectionLength = TextBox1.GetCharIndexFromPosition(e.Location) - TextBox1.SelectionStart   1
    End If
End Sub

CodePudding user response:

Create a UserControl instead of a Form. You can design it visually in the designer exactly like a Form.

Example of adding a UserControl to a panel

Dim uc = New MyUserControl() ' But please, give it a better name!
Panel2.Controls.Add(uc)

Note that you don't have to set a TopLevel property nor to call BringToFront() or Show().

  • Related