|
-
Apr 1st, 2016, 01:38 PM
#1
Thread Starter
Member
Call GoTo from first Private Sub, and use it in Private Sub Button5_Click
Hi everyone,
I'd like to abort my GoTo loop in "Private Sub Button1_Click", by clicking the" Private Sub Button5_Click". I need to activate "GoTo eend" from Button1_Click by clicking Button5_Click, so it will jump to eend: and will stop GoTo loop.
In different words: I'd like call Button1's "GoTo" function by clicking Button5. I've found this code, but it calls whole sub:
Code:
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
'stop manually'
Call Button1_Click(Me, e)
End Sub
Thanks for help, I'll give reputation
-
Apr 1st, 2016, 01:43 PM
#2
Re: Call GoTo from first Private Sub, and use it in Private Sub Button5_Click
GoTo's only lead to spaghetti code and in my opinion should never be used and should never have been implemented in VB.NET.
One thing that you're able to do is call PerformClick on the button.
-
Apr 1st, 2016, 02:00 PM
#3
Re: Call GoTo from first Private Sub, and use it in Private Sub Button5_Click
GoTo has a really bad reputation. It doesn't deserve all of it, but it's almost completely unused in modern code, and there are other structures that do the same thing with more clarity.
The same can be said for "calling button click methods". Let's take a step back. Imagine a button click method that displays a message:
Code:
Private Sub Button1_Click(ByVal sender As Object, ByVal e As Eventargs) Handles Button1.Click
MessageBox.Show("I'm the 'bold' button!")
End Sub
But we also have a menu item that we want to do the same thing! One way to accomplish that is to use the Button.PerformClick() method that dday9 suggested, but sometimes we don't have access to the button, or we want to do something a little different if the menu item is clicked instead of the button.
Another way to solve the problem is to say, "Showing the message box needs to be accessed from many places, not just one button click. It should be its own Sub."
Code:
Private Sub Button1_Click(ByVal sender As Object, ByVal e As Eventargs) Handles Button1.Click
Bold()
End Sub
Private Sub Bold()
MessageBox.Show("I'm the 'bold' button!")
End Sub
Now the answer to the question's really clear! If we want something else to display the message box, it should call the "Bold()" sub! And we don't have to worry about faking a sender and event args!
Code:
Private Sub MenuItem584_Click(ByVal sender As Object, ByVal e As EventArgs) Handles MenuItem584.Click
Bold()
End Sub
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
-
Apr 1st, 2016, 02:36 PM
#4
Thread Starter
Member
Re: Call GoTo from first Private Sub, and use it in Private Sub Button5_Click
Maybe there is a way stop the Button1_Click loop by clicking Button5, but not using GoTo. Something like break, opposition of Application.DoEvents() ?
I've tried that but it doesn't work neither:
Code:
Private Sub Bold()
GoTo eend
End Sub
and
Code:
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
'stop manually'
Bold()
End Sub
-
Apr 1st, 2016, 03:12 PM
#5
Re: Call GoTo from first Private Sub, and use it in Private Sub Button5_Click
I can't comment on what you did wrong without seeing more of your code. "It doesn't work" isn't a problem description.
We need to see more of your code if you want something that's a copy/paste solution. Without more of your code, you're going to have to read what we said, think about it, and write code that implements it yourself. I would bet a lot there is no need for a GoTo. Why do you think you need it?
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
-
Apr 1st, 2016, 03:20 PM
#6
Re: Call GoTo from first Private Sub, and use it in Private Sub Button5_Click
You talk about a loop, but we haven't seen that loop. If clicking on some button starts a loop that will run for a very long time, or even forever, then you can't really interrupt it from any other button without doing a bit of contortions that people frown upon. The problem is that the loop will be running in the UI thread, as stated, and that will prevent the UI thread from processing messages in the message queue, which includes not being able to process user input such as button clicks. So, at first, it would appear that the button won't click at all, because the UI is too busy to even get around to processing the minor changes to the appearance of the button that indicates that it has been clicked. You could get around that if there was an Application.DoEvents in the loop, because that pauses the loop and explicitly directs the UI thread to process all messages in the queue. This can have other, decidedly negative, results, so it isn't encouraged. Still, there are times when it is just the easiest solution. A better solution might be to have the loop performing in a different thread, but whether or not that even makes sense depends entirely on what the loop is doing, and you haven't talked about that, nor shown us the loop.
My usual boring signature: Nothing
 
-
Apr 1st, 2016, 03:22 PM
#7
Re: Call GoTo from first Private Sub, and use it in Private Sub Button5_Click
Maybe there is a way stop the Button1_Click loop by clicking Button5
I'm not sure with what you meant about stopping Button click. But if you want to disable the click event, an option is to remove the
event handler of that control. And then add again somewhere else in your code.
VB.NET Code:
RemoveHandler Button1.Click, AddressOf Button1_Click
AddHandler and RemoveHandler
- kgc
-
Apr 1st, 2016, 03:24 PM
#8
Thread Starter
Member
Re: Call GoTo from first Private Sub, and use it in Private Sub Button5_Click
Ok, thanks for help. I've replaced GoTo loop with Do While abc is False loop, and stop button makes abc boolean True, which stops the loop
-
Apr 1st, 2016, 03:27 PM
#9
Re: Call GoTo from first Private Sub, and use it in Private Sub Button5_Click
That loop is still disturbing. If that is on the form, the only way you can use a different button is if you are looping around an Application.DoEvents. That can be ok, depending on what else is happening in the loop, but the most common way that people loop around a DoEvents is by creating a Busy Wait, and those are pretty bad, even if they work.
My usual boring signature: Nothing
 
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|