[SOVED! CODING BEGINNER] Make A SHORTCUT to execute a code for several checkbox etc?
Hello!
i tell your help for a task for several checkbox ( 15 in my situation). i suppose it could have a shortcut to gather all checkboxes in a line.
for example:
private checkbox(1,2,3,4,5,6,7 until 15).checkstatechanged ............etc
and add this code:
Code:
dim n as integer
if checkbox(n).checkstate = checkbox.checked then
listbox1.items(n-1) = "true"
end if
sorry, for details i am a beginner in coding! if it is possible thank you in advance for your precious help!:thumb:
Re: [HELP CODING BEGINNER] Make A SHORTCUT to execute a code for several checkbox et
Just create a Sub that handles the CheckChanged event.
Code:
Private Sub CheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged, CheckBox2.CheckedChanged
MessageBox.Show(DirectCast(sender, CheckBox).Name)
End Sub
Re: [HELP CODING BEGINNER] Make A SHORTCUT to execute a code for several checkbox et
Quote:
Originally Posted by
wes4dbt
Just create a Sub that handles the CheckChanged event.
Code:
Private Sub CheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged, CheckBox2.CheckedChanged
MessageBox.Show(DirectCast(sender, CheckBox).Name)
End Sub
Note that the Handles clause here has multiple events listed. You can add as many as you like and they don't have to be on the same type of control or even the same event, as long as the signatures are compatible. You can get the IDE to generate an event handler like this for you automatically. In the designer, just select all the CheckBoxes by Ctrl+clicking or click+dragging, open the Properties window, click the Events window and then double-click the CheckedChanged event. Hey presto, you have your single method to handle multiple events. If you add more controls later, you can select the existing event handler from the drop-down list in the Properties window or you can edit the Handles clause manually.
Note that you should probably change the name of the method, because it will be named after just one of the controls. I would likely change it to something like CheckBoxes_CheckChanged but there may be something else even more appropriate.
Re: [HELP CODING BEGINNER] Make A SHORTCUT to execute a code for several checkbox et
You should almost certainly not being using the CheckState property in your code. You ONLY use that property if you have set ThreeState to True and you care about the Indeterminate state. Otherwise, there are only two states so you use the Checked property, which is True when checked and False when unchecked.
Re: [HELP CODING BEGINNER] Make A SHORTCUT to execute a code for several checkbox et
Quote:
Originally Posted by
wes4dbt
Just create a Sub that handles the CheckChanged event.
Code:
Private Sub CheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged, CheckBox2.CheckedChanged
MessageBox.Show(DirectCast(sender, CheckBox).Name)
End Sub
thank you so much for your answer!!!!!:bigyello:
by the way, is this code right?
Code:
dim n as integer
if checkbox(n).checkstate = checkbox.checked then
listbox1.items(n-1) = "true"
end if
Re: [HELP CODING BEGINNER] Make A SHORTCUT to execute a code for several checkbox et
Quote:
Originally Posted by
danzey
by the way, is this code right?
Code:
dim n as integer
if checkbox(n).checkstate = checkbox.checked then
listbox1.items(n-1) = "true"
end if
All code is right because all code does exactly what it does. If you would like us to tell you whether that code will do what you want it to do, you'd probably need to tell us what you actually want it to do. That said, why do you need to ask us at all? Why can't you run it and see what it does? If it does as you want then it's OK and if it doesn;t then you have something to look into and maybe ask us about. You never need to ask us what you can find out for yourself.
That said, how can that code be of use when you never assign a value to n? Also, you have simply ignored what I said about CheckState.
Re: [HELP CODING BEGINNER] Make A SHORTCUT to execute a code for several checkbox et
Quote:
Originally Posted by
jmcilhinney
All code is right because all code does exactly what it does. If you would like us to tell you whether that code will do what you want it to do, you'd probably need to tell us what you actually want it to do. That said, why do you need to ask us at all? Why can't you run it and see what it does? If it does as you want then it's OK and if it doesn;t then you have something to look into and maybe ask us about. You never need to ask us what you can find out for yourself.
That said, how can that code be of use when you never assign a value to n? Also, you have simply ignored what I said about CheckState.
sorry, i didn't see and read your message . thank you for alll! i understood how i must code my work!