Fire Validated Event on WinForms ComboBox - vb.net

I have some logic being handled in the Validated event on a combobox, but would like it to trigger as soon as a selection is made. I can handle the SelectedIndexChanged event separately in order to fire the event, but I'm not sure how to invoke it.
Private Sub ComboSelectedIndexChanged(sender As System.Object, e As System.EventArgs) _
Handles ComboBox1.SelectedIndexChanged
'Fire Validated Event
End Sub
Private Sub ComboValidated(sender As System.Object, e As System.EventArgs) _
Handles ComboBox1.Validated
'Do Something Here
End Sub
I'd rather not call into the method handling the validated event directly,
ComboValidated(Nothing, Nothing)
as it's not precisely what I'm trying to do with the code and it will run a second time when in fact the validated event actually does fire upon losing focus.
What code can I execute to fire the validated event (either by fire the event directly or by causing the control to lose focus)?

Why don't you make it as Sub ..
Private Sub ComboValidated(sender As System.Object, e As System.EventArgs) _
Handles ComboBox1.Validated
InCBValidated()
End Sub
Sub InCBValidated()
'Do Something Here
End Sub
So you just call InCBValidated()

I solved this by making the control lose focus which then fired all the neccessary events. The control happened to be in a groupbox so the easiest way to remove focus was just to give that control focus so the same place on the page was still selected
Private Sub ComboSelectedIndexChanged(sender As System.Object, e As System.EventArgs) _
Handles ComboBox1.SelectedIndexChanged
If ComboBox1.Focused Then GroupBox1.Focus()
End Sub

Related

How to utilize the button click event from all buttons to call a function?

Let me explain further. Let's say I have 20 buttons on a form, and in the all of the button's click event I want to call a specific function, instead of placing the calling to that function in each click event, is there a way to call it from any of the click events without having to place the code in each click event?
Hope that makes sense.
Yes, just add the click event for each button to the end of the Handles clause.
Private Sub My_Sub(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click
' do stuff
End Sub
If you have a lot of buttons or a dynamic number of buttons, you could use a recursive method to add the same event handler for each. This will take care of all buttons, even those inside group boxes or other containers.
First, create the method you want each button click to call.
Private Sub bt_ButtonClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
'
' Your code goes here
'
End Sub
Second, create a method to recursively find all the button controls and add the event handler.
Private Sub AddEventHandler(RootControl As Control)
For Each c As Control In RootControl
If c.HasChildren Then
AddEventHandler(c)
End If
If TypeOf c Is Button Then
AddHandler c.Click, AddressOf bt_ButtonClick
End If
Next
End Sub
Lastly, in your form load event, add this line:
AddEventHandler(Me)
Wire the one sub to the click event for every button.
Private Sub AnyButton_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click
End Sub

Vb.NET Windows Forms Focus Event

I am working with .Net Windows Forms application (vb.net),
I have Two Forms,
Form A = Main form
Form B = Which is being called by clicking a button on Form A
The Issue is, I want to update certain Controls(List,Grids) when ever my Form A gets Activated,
At Form_A_Load it will load controls one must, but when I open Form B and upon Exit of Exit of Form B, I want to reload Form A's controls(List,Grids).
I have tried many events
Activated,Deactivated,Enter,Leave,Enabled,Visibility changed , but could not trap any,
If I am using Activated/Deactivated with some flag to check which was triggered, then a continues loop occurs. Kindly some body suggest , the workable method
Here is the Edit code:
Public Class Form1
Private Sub Form1_Activated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Activated
MessageBox.Show("Activated")
End Sub
Private Sub Form1_Deactivate(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Deactivate
' MessageBox.Show("Deactivated")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Text = "Activated/Deactivated"
MessageBox.Show("This will set focus lost")
End Sub
End Class
--If a once click on Button1_Click .. "MessageBox.Show("Activated")" appears again and again.
Basically, It was something multiple forms opened, and what I did is that at the FormClosing of last Form where my code Returns to Forma A, I have checked through Loop the Opened Forms and from there I selected my Form A and Triggered the Function Which Reloads the List.
Try this:
[UPDATED]
Public Class FormA
Friend WithEvents objectFormB As FormB
Private Sub objectFormB_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles objectFormB.FormClosed
'do whatever...
End Sub
End Class

Setting focus to a textbox control

If I want to set the focus on a textbox when the form is first opened, then at design time, I can set it's tabOrder property to 0 and make sure no other form control has a tabOrder of 0.
If I want to achieve the same result at run-time, using code, how should I proceed?
Are there alternatives to using tabOrder?
I assume any run-time code will be in the form's constructor or its onload event handler?
EDIT
In other words I'd like to be able to type straight into the textbox as soon as the form appears without having to manually tab to it, or manually select it.
Because you want to set it when the form loads, you have to first .Show() the form before you can call the .Focus() method. The form cannot take focus in the Load event until you show the form
Private Sub RibbonForm1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.Show()
TextBox1.Select()
End Sub
I think what you're looking for is:
textBox1.Select();
in the constructor. (This is in C#. Maybe in VB that would be the same but without the semicolon.)
From http://msdn.microsoft.com/en-us/library/system.windows.forms.control.focus.aspx :
Focus is a low-level method intended primarily for custom control
authors. Instead, application programmers should use the Select method
or the ActiveControl property for child controls, or the Activate
method for forms.
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
TextBox1.Select()
End Sub
Using Focus method
Private Sub frmTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
yourControl.Focus()
End Sub
To set focus,
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
TextBox1.Focus()
End Sub
Set the TabIndex by
Me.TextBox1.TabIndex = 0
Quite simple :
For the tab control, you need to handle the _SelectedIndexChanged event:
Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As System.EventArgs) _
Handles TabControl1.SelectedIndexChanged
If TabControl1.SelectedTab.Name = "TabPage1" Then
TextBox2.Focus()
End If
If TabControl1.SelectedTab.Name = "TabPage2" Then
TextBox4.Focus()
End If
I think the appropriate event handler to use is "Shown".
And you only need to focus the appropriate text box.
Private Sub Me_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
myTextbox.Focus()
End Sub
create a textbox:
<TextBox Name="tb">
..hello..
</TextBox>
focus() ---> it is used to set input focus to the textbox control
tb.focus()

VB.NET - Handle many textboxes with less code

I got many textboxes (about 10) in a form. I want the text in the textbox to be higlighted whenever it gets foucs. The code for that looks like:
Private Sub txtBillNo_GotFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBillNo.GotFocus
HoverText(txtBillNo)
End Sub
Private Sub HoverText(ByRef ctrl as TextBox)
ctrl.SelectAll()
End Sub
It works perfectly, but I though I could do some code optimization here. Since, I have about 10 textboxes (and many other forms containing several textboxes), I have to HoverText(TextBox) in every Private Sub.. Handles TextBox.GotFocus for every textbox in each form.
I look for any form event (or any other way) that is trigerred when focus is given to another control (textbox) within the form, either by MouseClick or TAB so that HoverText(TextBox) is needed to be written only once for a form.
You can list all your textboxes in the Handles clause:
Private Sub atextbox_GotFocus(ByVal sender As System.Object, ByVal e As _
System.EventArgs) _
Handles txt1.GotFocus, _
txt2.GotFocus, _
txt3.GotFocus, _
(...remaining text boxes..)
txt9.GotFocus, _
txt10.GotFocus
Or you can add handlers to all textboxes when loading the form:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each t In Me.Controls.OfType(Of TextBox)()
AddHandler t.GotFocus, AddressOf HoverText
Next
End Sub
Private Sub HoverText(ByVal sender As System.Object, ByVal e As System.EventArgs)
DirectCast(sender, TextBox).SelectAll()
End Sub
The .NET way to alter the behavior of a class is to derive a new one. That's well supported by VB.NET and Winforms as well. For any class derived from Control, you can intercept an event by overriding the protected OnXxx event where Xxx is the event name. You should be using the Enter event btw. Use Project + Add Class and make the code look like this:
Public Class MyTextBox
Inherits TextBox
Protected Overrides Sub OnEnter(ByVal e As System.EventArgs)
Me.Select(0, Me.Text.Length)
MyBase.OnEnter(e)
End Sub
End Class
Compile. Go back to your form and note that you now have a new control on the top of the toolbox. Drag it on the form and note how it now behaves the way you want it, without writing any code or event handler in your form at all.

VB.Net Mouse.Enter and Mouse.Leave both called in same event?

In my VB.Net forms application, I have a function:
Private Sub pnlMain_MouseLeave(sender As Object, e As System.EventArgs) Handles pnlMain.MouseLeave
...
End Sub
and another function:
Private Sub pnlMain_MouseEnter(sender As Object, e As System.EventArgs) Handles pnlMain.MouseEnter
...
End Sub
When the mouse enters or leaves both are executed - first Enter and then Leave. Why is this happening?
It doesn't or it shouldn't
but if you have a breakpoint on the enter event then it will fire the mouseleave event as you hit it. (because you've triggered the leave event).