A function is called twice in VB.NET - 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.

Related

How to share events between forms

So i have a tray icon that should behave the same way between 3 forms. I then created this code:
Private Sub TrayForm_MouseClick(sender As Object, e As MouseEventArgs) Handles NotifyIcon1.MouseClick
If e.Button = MouseButtons.Right Then
If Not Application.OpenForms().OfType(Of TrayForm).Any = 1 Then
TrayForm.ContextMenuStrip1.Show(Cursor.Position)
End If
End If
End Sub
Which is used to handle the tray icon. How can i do to share this event between the forms so i don't have to place this same code on every form?
How are event handlers working exactly? I looked online and on MSDN and it is not clear to me.
Thanks
Are you sure that you want to share the event, and not juste the code that will handle the event?
If you don't want to copy and paste your code, which you need to handle the events of more than one form, here's a way to do it:
Declare the sub which contains the code needed to handle the event as a public shared sub. Like this:
Public Shared Sub TrayForm_MouseClick(sender As Object, e As MouseEventArgs)
So, now you have a Sub which can handle the event you want to handle from all three forms.
Now, when you initialize those forms, add a line to make the shared Sub handle the event you want it to handle:
AddHandler NotifyIcon1.MouseClick, AddressOf ProjectName.FileName.TrayForm_MouseClick
ProjectName.FileName is meant here to be the path to refer to the shares Sub inside the file where you put it. I usually name it like ProjectNameUtils.vb or something like that.
If you just want to avoid copy and pasting your Sub so you don't have to modify it at several places every time you change something, this could be a way to achieve that.
As Stipulated by Hans Passant:
Sub Eclass_EventHandler(sender As Object, e As MouseEventArgs) Handles Me.MouseClick
If e.Button = MouseButtons.Right Then
If Not Application.OpenForms().OfType(Of TrayForm).Any = 1 Then
Me.ContextMenuStrip1.Show(Cursor.Position)
End If
End If
End Sub
On the Trayform.VB just did the trick.
But about the shared event. i Have one that would have to be:
Private Sub FormClosingEVENT(sender As System.Object, e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
If Not FromMenu Then e.Cancel = True
Me.WindowState = FormWindowState.Minimized
'Application.Exit()
End Sub
How should i handle this?

vb.net: Distinguish between me.close() and x-button

how is it possible to distinguish between the event, that the user explicitly pressed to x-button (or pressed ALT F4) to close a form and all other methodes of closing the form programatically (me.close(), etc).
I already figured out, that this could be done using sender objects but I don't really get it. Could somebody explain it for me with an example?
Thank you all very much in advance.
The class FormClosingEventArgs event args has an enumeration to tell what the reason the form is closing.
https://msdn.microsoft.com/en-us/library/system.windows.forms.closereason(v=vs.110).aspx
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles Me.Closing
If Not appClosing AndAlso e.CloseReason = System.Windows.Forms.CloseReason.UserClosing Then
' DO WHATEVER CODE YOU WANT IN HERE
' LIKE SETTING E.CANCEL TO TRUE
End If
End Sub
' You can also set a close appClosing local variable at the class level as a boolean, whenever you call closing in code, assign the variable.
appClosing = True
Me.Close();
It's as simple as that. :)
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs)
e.Cancel = True
End Sub
This will prevent the X button to close the form and also the :) .The issue is that if you do this you cannot close your form using Me.Close too , To fix this,just remove the EventHandler :)
RemoveHandler Me.Closing, AddressOf Form1_FormClosing
'Now close the form
Me.Close()

How to write a single click event in Visual Basic?

I have created a form that allow users to close a form by clicking anywhere on the enlarged picture form (There are 3 objects to consider) and go back to the other form, which is called: "frmPhone". There's an actual picture on the form: "frmPhonePics" which is what I'm using to accomplish what I'm trying to do (was unable to insert an image on here. Sorry.) What I want to do is write a single click event to close the large picture form to allow the user to close it absolutely anywhere in the form, but I don't know how to do that. Here's the code I have so far:
Private Sub frmPhonePics_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Click
frmPhone.Show()
Me.Hide()
End Sub
It sounds as though you have a picture on your frmPhonePics form. If you double click that (from the VBA editor), you should be taken to the code - for example, you might see
Private Sub Image1_Click()
End Sub
Now all you have to do is add your code there:
Private Sub Image1_Click()
Me.Hide
frmPhone.Show()
End Sub
Note - the order matters, since frmPhone.Show() will "hijack" the code flow until it's dismissed, and in your code Me.Hide will not execute (so the form will not close) until frmPhone has been dismissed.
You can map the click handler for various object to one thing, if that is what you are asking:
Private Sub frmPhonePics_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Click, Handles picLarge.Click, Handles otherThing.Click
frmPhone.Show()
Me.Hide() ' should be Me.Close?
End Sub
Not sure why it is MyBase.Click in your code instead of Me.Click. Is this a subclassed form?
I'd strongly suggest using a DoubleClick instead of a single Click. The chances of an errant click doing the wrong thing is very great.
The easiest way is right from the designer. Write the sub routine, then for each control, in the properties window, click the events icon(thunderbolt) and assign the sub routine to the double-click event.
Alternatively, dispense with the Handles clause completely and use a series of Addhandler statements in the Load event handler. If you put a unique string in the names of the controls or if it's all the controls, you can iterate through the controls and use one addhandler statement for all of them
For Each c As Control In Me.Controls
AddHandler c.DoubleClick, AddressOf Ctrl_DoubleClick
Next
Private Sub Ctrl_DoubleClick(sender As Object, e As EventArgs)
'Do stuff
End Sub

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

Textbox click event in vb.net

I am trying to write an onclick event for textbox but VB.net does not seem to support textbox1.click()
I want to open a new form every time someone clicks on the textbox.
Opening a new form is no problem, but I cant detect the click.
Is there any event for textbox that detects click event?
I saw something like TextboxBase that has Click but I am able to use it well.
please help!
This is how my class looks :
Partial Public Class TextBoxClick
Inherits System.Web.UI.Page
End Class
It has some basic load and init events.
I am trying to write a Sub like this :
Private Sub incident_clicked(ByVal sender As Object, ByVal e As System.EventArgs) Handles Incident.OnClick
Incident.Click does not work either.
I am guessing I need to import some class to access the Click event but I am not sure which.
Thanks is advance
TextBox has a Click event, using it is no problem. Your Handles clause however uses OnClick, that's not a valid event name. Do make sure this Sub is inside a Form class and not a module.
Public Class Form1
Private Sub TextBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Click
MessageBox.Show("Click!")
End Sub
End Class
You could use onFocus event :)
According to MSDN, your code should work as follows:
Private Sub TextBox1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles TextBox1.Click
' Code to handle the event here
End Sub
However, you could also try the MouseUp event:
Private Sub textbox1_MouseUp(sender As Object, _
e As System.Windows.Forms.MouseEventArgs) _
Handles textbox1.MouseUp
' Code to handle the event here
End Sub
' Will fire if textbox gets focused
Private Sub incident_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles incident.GotFocus
Debug.Print("inciden got focus")
End Sub
' Will fire if textbox gets mouse clicked
Private Sub incident_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles incident.MouseClick
Debug.Print("inciden got clicked")
End Sub
For anyone who is having trouble with this, I fixed it by switching to an asp control. My button now looks like this:
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
Not sure why, but I now have a working click event.
For me the Textbox_ click event is triggered only when I type a character in that box.
The textbox click event is triggered only when you type a character in that textbox.
That is disgusting. You may want to try mouse-enter mouse-leave events they are more reliable. Babu V Bassa.