-
Apr 11th, 2014, 02:26 AM
#1
Thread Starter
Junior Member
Only one button active at a time
Hello,
I have 10 buttons in my form, that, when clicked on, perform some action. This is what i'm trying to accomplish: when i click on (for example) button 1, i want to make other buttons disabled, action from button 1 is being executed, and when i click button 1 again, action from button 1 is stopped, and all buttons are enabled again. In other words, i want only one button to be active at a time. What is the best way to accomplish this. Thought this would be easy one, but not for me
Thanks
-
Apr 11th, 2014, 04:43 AM
#2
Re: Only one button active at a time
You could use the _Click event of your Button1.
Create a global boolean variable that tells wether you're in the working state or not.
In the Button1 _click event:
-change the state of that variable
-either enable or disable the other buttons according to the working state
-start the execution
And in the executing code, check during each iteration, loop or whatever you are using wether you are still in the working state, if not stop the execution.
You're welcome to rate this post!
If your problem is solved, please use the Mark thread as resolved button
Wait, I'm too old to hurry!
-
Apr 11th, 2014, 10:32 AM
#3
Re: Only one button active at a time
You might consider using radiobuttons. The Appearance property has two settings: Normal and Button. As you might imagine, the latter makes the radiobutton appear just as a button, except that if you have several of them on a form, only one can be pressed at a time.
This might not suffice, though. When you press such a button, only one can appear pressed, but all of them are still enabled. So, if you want to disable the rest for some time, you may still have to do further work.
My usual boring signature: Nothing
-
Apr 11th, 2014, 11:06 AM
#4
Re: Only one button active at a time
Radiobuttons are an option but I am not sure they will work for your issue. And that is because, as Shaggy says, only one can be pressed at any time, but one MUST be pressed at all times, this is something I do not think you want.
I also do not think you need a state variable as you stated that the same button will "stop" the working state.
The solution, keeping in line strictly with what you stated in the problem, is to "toggle" the enabled property of the rest of the buttons each time you press one of them. In order to avoid typing the same code 10 times, I would put the disabling code in one sub and call it from each procedure that handles the click events, passing the "sender" button to it so it keeps that one enabled. You should also change the Text property of the active button so the user knows it is now in the STOP THE PROCESS state.
Last edited by kaliman79912; Apr 11th, 2014 at 11:11 AM.
More important than the will to succeed, is the will to prepare for success.
Please rate the posts, your comments are the fuel to keep helping people
-
Apr 11th, 2014, 11:53 AM
#5
Thread Starter
Junior Member
Re: Only one button active at a time
Thank you all very much, you have been very helpful. As kaliman79912 nicely stated, my goal is to "toggle" enabled state between "active" and "passive" buttons, and his idea is pretty much what i'm working on. Thank you all once more.
-
Apr 11th, 2014, 04:19 PM
#6
Addicted Member
Re: Only one button active at a time
This is how you can do it with buttons:
Code:
Option Strict On
Public Class Form1
Private counter As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If sender Is Me.Button1 Then
counter += 1
Button2.Enabled = False
Button3.Enabled = False
Button4.Enabled = False
If counter = 2 Then
Button2.Enabled = True
Button3.Enabled = True
Button4.Enabled = True
Button1.Enabled = False
counter = 0
End If
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If sender Is Me.Button2 Then
counter += 1
Button1.Enabled = False
Button3.Enabled = False
Button4.Enabled = False
If counter = 2 Then
Button1.Enabled = True
Button3.Enabled = True
Button4.Enabled = True
Button2.Enabled = False
counter = 0
End If
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If sender Is Me.Button3 Then
counter += 1
Button1.Enabled = False
Button2.Enabled = False
Button4.Enabled = False
If counter = 2 Then
Button1.Enabled = True
Button2.Enabled = True
Button4.Enabled = True
Button3.Enabled = False
counter = 0
End If
End If
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
If sender Is Me.Button4 Then
counter += 1
Button1.Enabled = False
Button2.Enabled = False
Button3.Enabled = False
If counter = 2 Then
Button1.Enabled = True
Button2.Enabled = True
Button3.Enabled = True
Button4.Enabled = False
counter = 0
End If
End If
End Sub
End Class
Edit:
You do the same for the six other buttons.
Last edited by Atenk; Apr 11th, 2014 at 04:25 PM.
-
Apr 12th, 2014, 02:02 AM
#7
Re: Only one button active at a time
Got the idea but I think it is 2 much. You are typing the same code 10 times (and what if there were more buttons?). Instead of a counter just negate the previous state. The IF there has no purpose, in the first sub it will always be Button1 and so on. As I said, use only one procedure for all of them. And do not use a counter.
Code:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles _
Button1.Click, Button2.Click, Button3.Click, Button4.Click, Button5.Click,
Button6.Click, Button7.Click, Button8.Click, Button9.Click, Button10.Click
Dim myButton As Button = DirectCast(sender, Button)
Button1.Enabled = Not Button1.Enabled
Button2.Enabled = Not Button2.Enabled
Button3.Enabled = Not Button3.Enabled
Button4.Enabled = Not Button4.Enabled
Button5.Enabled = Not Button5.Enabled
Button6.Enabled = Not Button6.Enabled
Button7.Enabled = Not Button7.Enabled
Button8.Enabled = Not Button8.Enabled
Button9.Enabled = Not Button9.Enabled
Button10.Enabled = Not Button10.Enabled
myButton.Enabled = True
End Sub
And if you put them in a container you can even loop through the controls instead of listing all of them.
Last edited by kaliman79912; Apr 12th, 2014 at 02:34 AM.
More important than the will to succeed, is the will to prepare for success.
Please rate the posts, your comments are the fuel to keep helping people
-
Apr 12th, 2014, 02:09 AM
#8
Re: Only one button active at a time
But since you most likely want each button to do something else, you need to do more. Either put a select case after the last line in the code to define what is it that the button does or put all that in a separate sub and call it from each click handler.
Code:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim myButton As Button = DirectCast(sender, Button)
toggleButtons(myButton)
'Do your stuff for button1
End Sub
Private Sub toggleButtons(sender As Button)
Button1.Enabled = Not Button1.Enabled
Button2.Enabled = Not Button2.Enabled
Button3.Enabled = Not Button3.Enabled
Button4.Enabled = Not Button4.Enabled
Button5.Enabled = Not Button5.Enabled
Button6.Enabled = Not Button6.Enabled
Button7.Enabled = Not Button7.Enabled
Button8.Enabled = Not Button8.Enabled
Button9.Enabled = Not Button9.Enabled
Button10.Enabled = Not Button10.Enabled
sender.Enabled = True
End Sub
More important than the will to succeed, is the will to prepare for success.
Please rate the posts, your comments are the fuel to keep helping people
-
Apr 12th, 2014, 02:16 AM
#9
Re: Only one button active at a time
Here is another version of toggleButtons, in this one I put all the buttons in a panel named panel1, (it can be done without the container but it is cleaner like this). And also you can do it now for any number of buttons without changing the sub
Code:
Private Sub toggleButtons(sender As Button)
For Each myButton As Button In Panel1.Controls
myButton.Enabled = Not myButton.Enabled
Next
sender.Enabled = True
End Sub
More important than the will to succeed, is the will to prepare for success.
Please rate the posts, your comments are the fuel to keep helping people
-
Apr 12th, 2014, 05:30 AM
#10
Addicted Member
Re: Only one button active at a time
Yes, kaliman, my code was repetitive. And I was aware that it would be more execessive in presence of a lot of buttons.
But at least, when a solution at hand for the moment works fine, one could adopt it.
In addition, the benefit of this forum is to exchange ideas and opinions.
I came with my idea, and that triggered a better one: yours.
-
Apr 12th, 2014, 08:57 AM
#11
Thread Starter
Junior Member
Re: Only one button active at a time
Once again, thank you very much guys, you helped me to solve my problem. Kaliman79912, you have my special thanks.
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
|