How to wait for line of code to finish before moving onto the next line - vb.net

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.

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()

there is a loading cursor in visual studio build program, will not stop

I made a small program ( in visual studio 2013) that simply displays a label in Form1 and a message when closing it. However, when I open it, it has the loading cursor that never stops. Can anyone help me please?
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub PictureBox1_Click(sender As Object, e As EventArgs)
End Sub
Private Sub Form_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Dim response As MsgBoxResult
response = MsgBox("Skype must be restarted.", MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, "Confirm")
If response = MsgBoxResult.Ok Then
Me.Dispose()
End If
End Sub
End Class
Cause of Error?
Look, I can't clearly say what the error is, but I find, the two main things because of which you are getting the error are:
There is a background task in your computer who is generating this, or, your Visual Studio's Environment is taking too long to load it's features.
The second error may be that, your cursor is set to loading.
Steps to Overcome
You need to go to your form's properties and set the cursor property to default or whatever you want. This can do your work, if you are having the second problem.
You can set different cursor property for different controls.
I hope this helps!

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

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.