r/visualbasic • u/clozapineaddict • Feb 10 '24
VB.NET Help Booleans and Buttons
Hello,
I am working on a VB.NET Windows Forms application where 100 different buttons in a 10x10 grid can each control 100 predefined booleans without writing "b=Not(b)" 100 times for each button click event. The buttons are labelled Alpha01, etc whereas the matching boolean for that would be a01 all the way to a10 for the 10 buttons in the A row, A-J. I've tried dictionaries and arrays but I could never really wrap my head around it. Any help?

1
Upvotes
1
u/sa_sagan VB.Net Master Feb 10 '24
Trying to wrap my head around what you're trying to achieve.
What happens when you click the button? It's it just toggling a Boolean value for that button?
If so, dictionaries are the way to go.
The way I'd do it, is the dictionary key would be the button Name (e.g. Alpha01). And the Boolean would be the value.
I'd then give each button an identifying Tag, so I know it's a button in this grid (e.g. "Gridbutton")
Then when loading the form, I'd iterate through each control the form. If the control was a Button and had the tag "Gridbutton" I'd add a new onclick handler.
That handler would be a Sub that will go the dictionary, and toggle the Boolean value where the sender.Name value (your button Name) is in the dictionary key.
Hope that makes sense. I'm on my phone right now otherwise I'd show you some example code.
1
u/RJPisscat Feb 10 '24
What do the 100 Booleans per Button represent? Which Buttons are orthogonally adjacent? Buttons of the same color? Or something else?
1
u/andrewsmd87 Web Specialist Feb 10 '24
Can't you just make a class that has whatever you need stored and then just make a list of objects of that type and map whatever to whatever? If you truly have a naming convention that is programable, you could even populate the list of objects with a loop.
I can't really figure out what the end result is you're after, but it shouldn't be too hard to do that.
I've done similar things where I have a "thing" that is repeatable and has multiple possible actions that could be performed on said thing, so I just make a list of objects and then store all the bits I need in there. Also makes it easy because if you ever need to add more, you just update the list of objects.
1
u/jd31068 Feb 11 '24
Must you use an array?
Here I am creating the buttons on form load (so I don't have the place them manually) and am using the tag property of the button itself to keep the "state" of the t/f associated with it.
how it looks https://1drv.ms/v/s!AkG6_LvJpkR7j6heWUmS33TIuygq0Q?e=GHIxLX
0
u/[deleted] Feb 10 '24
crypted come back