How to set mouseclick property of textbox in vb.net? - vb.net

When I click in textbox cursor position should be at where I click the mouse.
Currently after mouse click cursor is place at begining of text in textbox. Also when I hover clicking mouse on text in textbox hover text should be select. I think this should be default properties of textbox. How enable this properties. Thanks in advance.

#AndrewMorton
When I open form using code
With Form2
.TopLevel = False
Panel2.Controls.Add(Form2)
.BringToFront()
.Show()
End With
This problem is arise.
If I open form like
Form2.Show()
it works perfectly.
Is there any wrong.

Related

Using vb.net to display a form inside a panel. error transparency

I have a panel and button.
A form with some text-box.
Trying to display form inside my panel. code that i have written is....
Private Sub Btn_Click(sender As Object, e As EventArgs) Handles Btn.Click
Dim f As New form()
f.TopLevel = False
f.WindowState = FormWindowState.Normal
f.FormBorderStyle = FormBorderStyle.None
f.Visible = True
Pages.Controls.Add(f)
End Sub
in another page, i have written.....
Dim f As New add_customer()
f.TopLevel = False
f.WindowState = FormWindowState.Normal
f.FormBorderStyle = FormBorderStyle.None
f.Visible = True
Form1.Pages.Controls.Add(f)
Form displays inside panel. Button inside that form works well, Combo-box works well, Check-box works well, all most everything works well, but text-box isn't working.
Tried that form to open outside panel. All is fine. But inside panel it doesn't work.
All the letters and text-box become transparent.
Main form
After Customer button clicked
After add customer button clicked
When pages run solely....
Help needed.
I have found the solution.
When the TransparencyKey property is assigned a Color, the areas of the form that have the same BackColor will be displayed transparently. Any mouse actions, such as the click of the mouse, that are performed on the transparent areas of the form will be transferred to the windows below the transparent area. For example, if the client region of a form is made transparent, clicking the mouse on that area would send the event notification of the click to any window that is below it. If the color assigned to the TransparencyKey property is the same as any controls on the form, they also will be displayed transparently.
I was unaware of this property of Transparency.
I Have changed background colour and all is good now.
Thanks Visual Vincent for your fast reply.....
This link is useful.
https://msdn.microsoft.com/en-us/library/system.windows.forms.form.transparencykey(v=vs.110).aspx

Tabbing on Form disappears when using Control.FromHandle

VB2010: I have a form that I display within a mapping application that supports their framework through .NET. So when the user clicks a button on a custom extension then my form will be displayed.
Private frm As New frmDesigner 'since this is a floating form
Public Overrides Sub OnClick()
Try
''display the form normally
'Dim frm As New frmDesigner
'frm.ShowDialog()
'display the modeless form and the form will always be on top of the main app.
If frm.IsDisposed Then frm = New frmDesigner 'To handle user closing form
If frm.Visible Then
frm.BringToFront()
Else
Dim appCtr As Control = Control.FromHandle(CType(MyApp.hWnd, IntPtr))
frm.Show(appCtr)
End If
Catch ex As Exception
DisplayException(ex)
End Try
End Sub
This works great and I have been using this approach for a couple years now. The problem that I am seeing now is that the tabbing through controls on the form does not work. Once the form appears I press on the Tab button and the cursor does not tab through the controls. I can set focus on a textbox through the mouse and then attempt to tab over to the next control but that doesn't work either.
If I display the form normally through frm.ShowDialog then the tabbing does work. Is there something I am missing here to make the tabbing work properly?

Remove textbox that added dynamically (VB.Net)

I am trying to delete a TextBox control (that added dynamically) in my form through Button_Click event (that I added dynamically too) but I cannot find the exact way to do it. My TextBox will added together with Button control (delete button) when a LinkLabel is clicked. So when added dynamically my textbox.name will be like textbox_1,textbox_2,textbox_3 and along with them is a Button control like btnDel1,btnDel2,btnDel3 (all placed in a Panel control).
My coding goes like this :
Private Sub Button_Click(sender As Object, e As EventArgs)
Dim button As Button = TryCast(sender, Button)
Dim textbox As TextBox = TryCast(sender, TextBox)
'In this case when btnDel1 is clicked, textbox_1 will be removed as well
If button.Name = "btnDel1" Then
PanelOthers.Controls.Remove(button)
End If
End Sub
Button is removed successfully but how do I remove the textbox too? Thanks in advance.
There are a couple ways to do this:
Attach all the relevant controls to the delete button's Tag property.
Create a user control that encapsulates the button and textbox.
Option 1: the Tag property
When you create your controls, add the associated controls to the button's .Tag property:
Dim button As Button = New Button
Dim textbox As TextBox = New TextBox
button.Tag = {textbox}
' Add the button and textbox to the UI surface
Now when the button is clicked you can loop over the associated controls and remove them too:
For Each item As Control In button.Tag
item.Dispose()
Next
button.Dispose()
Option 2: A user control
So isn't a tutorial site.. but you can do your own research on this one.
Here's a place to start: https://msdn.microsoft.com/en-us/library/c316f119%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

Pressing Enter is not the same as clicking the Windows Form's AcceptButton

I have a simple VB.Net Windows Forms application with two TextBoxes and a Button which is the form's AcceptButton. The only code is on the ButtonClick, and validation for each TextBox using the Leave event. (I tried the LostFocus event also, and it seems to work the same). My problem is that if I click the Button with the mouse, then the proper Leave (or LostFocus) event happens for the TextBox that the user was in, but If I press Enter, focus is not lost and so the code for that TextBox does not run.
How can I make the Enter Key behave the same way as clicking the form's AcceptButton?
When you click directly on the button there is a chain of events including TextBox.LostFocus, TextBox.Leave, Button.MouseEnter, Button.Click
But when you press Enter it's the Form which intercept the keypress and if it's Enter and there is an AcceptButton then launch it's PerformClick method without raising most of those events.
You could easily verify that ; set your Button's DialogResult on one value via designer and handle your Button's GotFocus event when you change that DialogResult for another value.
Then test your code (store the result of Form.ShowDialog) ; with direct click you'll get the value set on GotFocus because it's during the Click the form set it's DialogResult on the button's DialogResult value ; with a press to Enter you'll get the initial Dialogresult (the designer one) effectively proving that GotFocus wasn't entered.
Back to the current problem ; to solve it you need to handle your validation in Validating (or Validated) event ; be sure that your Textbox CauseValidation is set to true [the default value] otherwise those event aren't raised. You could inside them set e.Cancel (with e the CancelEventArgs argument) to true to stop validation (and the chain of events) effectively sticking focus inside the "invalidated" control (and which also prevents the button's click whatever way was used to reach it)
one example : this form has two Textboxes and one Button as the Form's AcceptButton and "validate" it's input so that no empty TextBox.Text are allowed
Public Class NonEmptyTextBoxesForm
Private Sub TextBoxes_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating, TextBox2.Validating
Dim tb = DirectCast(sender, TextBox)
e.Cancel = tb.Text = "" ' e.Cancel = (tb.Text = "") ' if that's make this clearer
End Sub
End Class
So you are losing focus from textbox to the button when you click Enter Key?!
If this is that, you can try out this:
Protected Overrides Function ProcessCmdKey(ByRef sMessage As Message, ByVal oKeyData As Keys) As Boolean
If oKeyData = Keys.Enter Then
Me.Textbox1.Focus()
AcceptButton.PerformClick()
Return True
ElseIf oKeyData = Keys.Escape Then
Application.Exit()
Return True
Else
Return MyBase.ProcessCmdKey(sMessage, oKeyData)
End If
End Function
I hope this helped you ;)
I agree with Plutonix - why not just put the validation for both text boxes in the click event? (unless this is for education purposes?)
If you really want to validate on the text boxes' leave events, and you need the validation on the current text box to run before whatever the button does when you hit enter, then you need a key press event to handle key press for both text boxes. In that, check if the key pressed was enter, and if so then you could just get the text box that has focus...call it's leave event, and then call the button's click event.
Does that make sense?
How can I make the Enter Key behave the same way as clicking the form's AcceptButton?
To make the ENTER key behave the same as clicking the ACCEPT button you can use the ACCEPTBUTTON property of the form and assign the button you wish to be clicked when the ENTER key is pressed.
Although this will cause the code in the button's click event to execute,
I do not know if the code in the TEXTBOX-LOSTFOCUS event will execute or not!
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'
' Assign default button for ENTER key (Okay Button)
Me.AcceptButton = Me.MyOkayButton
'
' Assign default button for ESCAPE key (Cancel Button)
Me.CancelButton = Me.MyCancelButton
'
End Sub

Why does my new form move behind the opener?

I'm not going to post a bunch of code here since I do not think it is a code issue.
Here is a link to my original question where I have shown the code if you are interested. Code
Just as a test I created a blank form window (Form1.vb) and no code gets passed to it and no code runs when it opens. If I do Form1.Show() from a MenuStrip Control or a Button Control, the window opens and stays on top. Now if I do Form1.Show() from a TreeView Control, the window opens and goes behind the window with the TreeView Control.
So my question is, what is different about the TreeView opening a form vs a button or other control?
I am using the basic VB TreeView Controll, and the new form is being called in AfterSelect method for the TreeView.
The AfterSelect works if you use your keyboard navigation to select a node, but it doesn't work when you use the mouse because the mouse capture is forcing the parent form to remain in focus. You would have to run your code after the AfterSelect event:
Private Sub TreeView1_AfterSelect(sender As Object, e As TreeViewEventArgs) _
Handles TreeView1.AfterSelect
Me.BeginInvoke(New Action(Sub()
Dim f2 As New Form2
f2.Show(Me)
End Sub))
End Sub
Use the Form.Show(parentForm) option, this will always put the new form on top of the old one.
have you tried Form1.ShowDialog() ? or if you don't want to show it as a dialog you should use:
Form1.Show()
Form1.BringToFront()