Event Handles Button.Click - vb.net

Hello here is what i want to do:
Private Sub UpdateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpdateButton.Click
MyUpdate.CheckUpdate("version.txt")
If MyUpdate.CurrentVersion < MyUpdate.UpdateVersion Then
'IF USER PRESS THE BUTTON TO RAISE EVENT ONE MORE TIME
Else
'DO NOTHING
End If
End Sub
I don't know how to raise an event within an event. Thank you!

Intuitively enough, you use the RaiseEvent keyword.
More explanation about raising and consuming events in VB.NET can be found here on MSDN.
But in this case it's probably better to refactor your code and extract the logic out of event handler method into another function.

Related

How to wait for line of code to finish before moving onto the next line

I'm using Visual Basic 2010, and within my form shown sub I need two buttons to be pressed , however I need the first button's code to complete before the moving on to pressing the next button. Is there any way to allow this to happen? Thanks :)
Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
BindingNavigatorMoveLastItem.PerformClick()))
'I need this next button click to be carried out after the one above has completely finished
BindingNavigatorMovePreviousItem.PerformClick()))
End Sub
Use methods instead of "button-clicks":
Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
MoveLastItem()
MovePreviousItem()
End Sub
Private Sub MoveLastItem()
bindingSource1.MoveLast();
End Sub
Private Sub MovePreviousItem()
bindingSource1.MovePrevious();
End Sub
You just have call these methods from the button-click events handlers as well.
Private Sub BindingNavigatorMoveLastItem_Clicked(sender As Object, args As EventArgs) Handles BindingNavigatorMoveLastItem.Click
MoveLastItem()
End Sub
Private Sub BindingNavigatorMovePreviousItem_Clicked(sender As Object, args As EventArgs) Handles BindingNavigatorMovePreviousItem.Click
MovePreviousItem()
End Sub
I must admit I don't do VB, but I came across this page and it might be useful to you.
http://msdn.microsoft.com/en-us//library/system.windows.forms.application.doevents.aspx
If that is not relevant, in your situation I would either use the buttons to set flags and simply if-test the flags, or create a do-while loop so that the code can finish executing while the conditions are met. Careful with those, however, as infinite loops are something they are good at.
Another thought is to enable the second button in the last line of the code of the first button?
Hope something helps. Apologies if it is of no use.

A function is called twice in VB.NET

I have a function that is called twice and I don't know what to do.
This is the code that is called when I press an input button on a WebBrowser:
Private Sub WebBrowser_DocumentCompleted(ByVal sender As System.Object, ByVal e As WebBrowserDocumentCompletedEventArgs) _
Handles WebBrowser1.DocumentCompleted
Document = sender.Document
AddHandler Document.Click, New HtmlElementEventHandler(AddressOf Document_Click)
End Sub
Private Sub Document_Click(sender As Object, e As HtmlElementEventArgs)
Select Case Document.ActiveElement.Id.ToLower
Case "global" : prueba()
Case Else
End Select
End Sub
If you want to see the function called prueba() here it is: http://pastebin.com/Fi5LLX2N
I have a video where I show it, but the annotations are in Spanish: http://www.youtube.com/watch?v=OCJXk3qJwVA
Well I have another problem with my function, as you can see, on the bottom I have put this:
Else
MsgBox("Este ModPack ya lo tienes instalado!")
End If
But it doesn't work. :(
Try this:
PS: It's written on the fly maybe can have some syntax error.
Private Sub WebBrowser_DocumentCompleted(ByVal sender As System.Object, ByVal e As WebBrowserDocumentCompletedEventArgs) _
Handles WebBrowser1.DocumentCompleted
Document = sender.Document
try : removehandler Document.Click, addressof(Document_Click): catch : end try
AddHandler Document.Click, New HtmlElementEventHandler(AddressOf Document_Click)
End Sub
My immediate reaction is a sticky mouse button, but really it's likely due to the web page you are loading having multiple pages being loaded, thus adding the duplicate event handler. Put a breakpoint on this line of code:
AddHandler Document.Click, New HtmlElementEventHandler(AddressOf Document_Click)
You'll likely see it being hit twice. Make sure to only wire up one HtmlElementEventHandler to avoid the double firing of the click event handler. You can check e.Url for a match before wiring up as a possible solution.
in vb.net there is no need to define onclick in html for button because it is handled automatically.so if you are doing this then the click event will fire twise.

unable to hit the CheckBox_CheckedChanged event

I am trying to check one condition when i m checking a particular checkbox.below is the code
Private Sub CheckBox4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox4.CheckedChanged
If CheckBox4.Checked Then
check_CL()
End If
i put one debugger to check whether the control is going to this event or not but it's not going.please tell me what is the problem..
From your arguments it looks that you have performed appropriate steps. I would suggest that you should look for this piece of code:
RemoveHandler CheckBox4.CheckedChanged, AddressOf CheckBox4_CheckedChanged
It is possible that you have unregistered handler somewhere in code either in .vb or .designer.vb
Have you tried using the full code to see if it works?
If CheckBox4.Checked = True Then
check_CL()
End If

How to get event name in vb.net?

Here there are two handlers in a particular procedure then how to get which event handler has performed.
for example
Private Sub TextBox1_Events(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox1.GotFocus
End Sub
how to get which event has occured.
It is possible using the StackTrace (could be a better way I'm not sure...). Try the following code.
Private Sub TextBox1_Events(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox1.GotFocus
Dim s As New StackTrace(True)
For Each f As StackFrame In s.GetFrames
Debug.WriteLine(f.GetMethod.Name)
Next
End Sub
When the text box gets focus the following is written:
TextBox1_Events
OnGotFocus
OnGotFocus
WmSetFocus
Ect…….
Where as when it’s a text changed event
TextBox1_Events
OnTextChanged
OnTextChanged
Ect….
I’m sure you could write something using this to do what you need. But i fully agree with the other guys separate handlers is better.
In this case, you cannot.
If the events were bound to two separate controls, you could check the sender property for the type
If the e argument for the event had some type other than EventArgs (some events use a different arguments type), or the control passed some type derived from EventArgs, then you might be able to check properties on that variable
There aren't any other tricks you could use because events don't provide any sort of data to the handler specifying which event occurred.
With these two events, they're both going to be sent from the same text box, so the first option is out. Also, with both events, they send just an instance of the EventArgs class (not a derived class), so that option is out.
Ultimately, you're going to have to have multiple event handlers to solve this specific problem.
It's not possible. If you're in a situation where you need to know which event occurred, you will always be better off using two separate handlers.
Since you are dealing with 2 events (similar in signature) emitted by the same control the easiest/cleanest way of solving this would be 2 separate event handlers (as suggested by Merlyn Morgan-Graham):
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
'the TextChanged specific code would go here
HandletTextBox1EventInternal(sender, e)
End Sub
Private Sub TextBox1_GotFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus
'the GotFocus specific code would go here
HandletTextBox1EventInternal(sender, e)
End Sub
Private Sub HandleTextBox1EventInternal(ByVal sender As System.Object, ByVal e As System.EventArgs)
'code common to GotFocus and TextChanged handlers
End sub

DataGridView cell click event in VS 2010

I am a tad new to the DataGrid controls, but I am just curious as to why the first code block below works, but the second code block does not? (Only thing I can see is the
Handles DataGridClaims
syntax
Block 1
Private Sub DataGridClaims_CellContentClick_1(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridClaims.CellContentClick
If e.RowIndex <> -1 Then
Dim frmViewClaims As New objViewClaim
frmViewClaims.ClaimID = DataGridViewClaims.CurrentRow.Cells("ClaimNum").Value
frmViewClaims.Show()
End If
End Sub
Block 2
Private Sub DataGridClaims_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
If e.RowIndex <> -1 Then
Dim frmViewClaims As New objViewClaim
frmViewClaims.ClaimID = DataGridViewClaims.CurrentRow.Cells("ClaimNum").Value
frmViewClaims.Show()
End If
End Sub
The "handles" keyword in VB.net marks the Function as a listener to the given event. Without the "Handles DataGridClaims", the grid has no way to know to fire your function when the event is triggered.
[See MSDN Doc's][1]
http://msdn.microsoft.com/en-us/library/6k46st1y(v=VS.100).aspx
I am not too familiar with the VB.NET, but CellContentClick is an event which occurs when the content within a cell is clicked.
In order for the program to understand that this is an event you use the keyword Handles in VB.NET. It allows you to wire-up the bindings to event handlers on the event handler methods themselves.
This is the equivalent of += in c# and would look something like
DataGridClaims.CellContentClick += DataGridClaims_CellContentClick;