Click a button programmatically - vb.net

I want to code a button that programmatically clicks the other button when I click it.
For example, I have two buttons named Button1 and Button2, what I wanted to do is that immediately after I click Button1, it should click Button2. Is this possible?

Best implementation depends of what you are attempting to do exactly. Nadeem_MK gives you a valid one. Know you can also:
raise the Button2_Click event using PerformClick() method:
Private Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
'do stuff
Me.Button2.PerformClick()
End Sub
attach the same handler to many buttons:
Private Sub Button1_Click(sender As Object, e As System.EventArgs) _
Handles Button1.Click, Button2.Click
'do stuff
End Sub
call the Button2_Click method using the same arguments than Button1_Click(...) method (IF you need to know which is the sender, for example) :
Private Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
'do stuff
Button2_Click(sender, e)
End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Button2_Click(Sender, e)
End Sub
This Code call button click event programmatically

The best practice for this sort of situation is to create a method that hold all the logics, and call the method in both events, rather than calling an event from another event;
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
LogicMethod()
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
LogicMethod()
End Sub
Private Sub LogicMethod()
// All your logic goes here
End Sub
In case you need the properties of the EventArgs (e), you can easily pass it through parameters in your method, that will avoid errors if ever the sender is of different types. But that won't be a problem in your case, as both senders are of type Button.

Let say button 1 has an event called
Button1_Click(Sender, eventarg)
If you want to call it in Button2 then call this function directly.
Button1_Click(Nothing, Nothing)

in c# this is working :D
protect void button1_Click(object sender, EventArgs e){
button2_Click(button2, null);
}
protect void button2_Click(object sender, EventeArgs e){
//some codes here
}
for vb.net
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Button2_Click(Sender, e)
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
//some codes here
End Sub

Related

Specific keyboard characters to call specific button actions

When I start my program, and push the specified keys to call my button commands, it does not do anything.
The focus remains on one of the buttons and nothing occurs.
I've tried multiple codes from using KeyDown to KeyPress to codes that include vbKey.
I am very new to vb, so it is very likely that I just do understand what I am doing. :(
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Me.KeyPreview = True
End Sub
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
If e.KeyCode = "/" Then
Call Button1_Click(sender, e)
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnExit.Click
'Close Program
Me.Close()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPMN.Click
'Add +1 to PMN Textbox (txtPMN)
txtPMN.Text = (Val(txtPMN.Text) + 1).ToString()
End Sub
End Class
I would like to simulate the clicking of certain buttons (activate the button code) when I press specific keys on the keyboard. For example: If I press "/" I would like Button1_Click to activate as if I clicked the button with the mouse. Then, if I push my next key ".", I would like Button2_Click to activate.
I do not want to use modifiers like: SHIFT, CTRL, ALT
Please try with keychar like this:
Private Sub Form1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
If e.KeyChar = "/" Then
Call Button1_Click(sender, New EventArgs)
End If
end sub
when use keydown, the keycode for "/" key is OEM, depend the keyboard type, so char(e.keycode) may be not "/"

DragLeave doesn't get called

in my VB.Net application I am try to drag and drop a custom control. It should be dropable onto another instance of the same class. So here is the code I use:
Private Sub HandleMouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Me.MouseDown
Me.DoDragDrop(Me, DragDropEffects.Copy)
End Sub
Private Sub HandleDragEnter(ByVal sender As Object, ByVal e As DragEventArgs) Handles Me.DragEnter
e.Effect = If(checkDropData(e), DragDropEffects.Copy, DragDropEffects.None)
End Sub
Private Sub HandleDragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles Me.DragDrop
addControl(e.Data.GetData(GetType(MyControl)))
End Sub
Private Sub HandleDragLeave(ByVal sender As Object, ByVal e As DragEventArgs) Handles Me.DragLeave
Console.WriteLine("HandleDragLeave: " & sender.ToString)
End Sub
I am able to drop the control but while dragging the DragLeave event never gets called. Did I miss something?
That's very bad... I used the wrong signature but the was nothing which told me I was wrong.
I found the solution here. I used DragEventArgs instead of EventArgs as type of e. After changing the type it works like expected.

Access Main thread data from the Background thread

I´m using a backgroundworker to handle processing of data while the user is still free to for instance click another button that aborts the process.
However, the code in backgroundworker requires several pieces of data, for instance it needs to know whether a radio button is checked.
Is there a way to access data in another thread from the background thread? Or should I create global variables that hold this information?
Public Class Test
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Button1.Content = "Working..."
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
If RadioButton1.IsChecked Then
MsgBox("It works")
End If
End Sub
End Class
You can pass the RadioButton checked state to the RunWorkerAsync method like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
BackgroundWorker1.RunWorkerAsync(RadioButton1.Checked)
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim checked As Boolean = CBool(e.Argument)
If checked Then
MessageBox.Show("It works")
End If
End Sub
Let me know if you need to be checking the RadioButton continuously from inside a loop, as that would be different.

form starting but not displaying vb.net

So I was coding a program that opens 20 web browsers on one page, for a youtube or view bot any URL really, but when I launched the program It displayed the text box I put in to see if it was actually starting and then nothing else
So my question is, Whats wrong with my code?
startup form (where you can launch the main code with button 1)
Public Class Startup
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Settings.Show()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
views.Show()
End Sub
End Class
main form
Public Class views
Private Sub views_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MessageBox.Show("Form Initalised")
WebBrowser1.Navigate(My.Settings.url)
WebBrowser2.Navigate(My.Settings.url)
WebBrowser3.Navigate(My.Settings.url)
WebBrowser4.Navigate(My.Settings.url)
WebBrowser5.Navigate(My.Settings.url)
WebBrowser6.Navigate(My.Settings.url)
WebBrowser7.Navigate(My.Settings.url)
WebBrowser8.Navigate(My.Settings.url)
WebBrowser9.Navigate(My.Settings.url)
WebBrowser10.Navigate(My.Settings.url)
WebBrowser11.Navigate(My.Settings.url)
WebBrowser12.Navigate(My.Settings.url)
WebBrowser13.Navigate(My.Settings.url)
WebBrowser14.Navigate(My.Settings.url)
WebBrowser15.Navigate(My.Settings.url)
WebBrowser16.Navigate(My.Settings.url)
WebBrowser17.Navigate(My.Settings.url)
WebBrowser18.Navigate(My.Settings.url)
WebBrowser19.Navigate(My.Settings.url)
WebBrowser20.Navigate(My.Settings.url)
Threading.Thread.Sleep(My.Settings.Time)
reload.Show()
Me.Close()
End Sub
End Class
and form reload mentioned in the main form.
Public Class reload
Private Sub reload_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Close()
views.Show()
End Sub
End Class
Try to use the Form.Shown event instead of the Form.Load event. In Form.Load the controls are often not fully initialized and if something goes wrong there the initialization of the whole form sometimes goes awry. It's better to use the Form.Shown for such lengthy procedures you have there.

call double click on mouse single click

how do we call double click on single mouse click event? Iam doing this and it's not working. Any ideas ?
Private Sub RichTextBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RichTextBox1.MouseClick
RichTextBox1_MouseDoubleClick(sender, e)
End Sub
In properties windows click Event Icon and find double click and in selected event name select "RichTextBox1_MouseClick"
Have you tried:
Private Sub RichTextBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RichTextBox1.MouseClick, RichTextBox1.MouseDoubleClick, RichTextBox1.DoubleClick
'Do stuff here
End Sub
There are basically 2 options. The first is how your doing it, which the click can still do more as well as the double click.
Private Sub RichTextBox1_MouseClick(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles RichTextBox1.MouseClick
'' i can do stuff here
RichTextBox1_MouseDoubleClick(sender, e)
'' and here
'' as well as do whatever double click is doing
End Sub
Private Sub RichTextBox1_MouseDoubleClick(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles RichTextBox1.MouseDoubleClick
'' to do something here
End Sub
The other option, if they are always going to do the same, you could do this:
Private Sub RichTextBox1_Single_DoubleClick(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles RichTextBox1.MouseClick, RichTextBox1.MouseDoubleClick
'' do something here
End Sub
Notice the second Handles bit on the end? that function can handle both now :)
Either should work fine for what you want.