[2005] DataBinding Buttons?
Hi all, not sure how to describe my problem but I'll give it a go...
I am designing a simple app to show the layout of an office. I have used buttons to represent 'chairs' and image boxes to display desks.
The idea is, when I click a button, the button turns green and text boxes fill with information such as: Username, login name, PC name, etc. And the button itself turns green.
I've done this bit and it all works fine. However, I added some 'reverse' code. ie: Added a button to search the database for a user. For this, I have a search button. When clicked an input box asks 'Enter user name', then it goes away and finds all the above details and populates the boxes.
However, it doesn't turn the button (the 'chair') green on the 'map' - which of course it wouldn't do, as it has no way of knowing which of the 200 buttons on the form relates to the selected user.
Here's my question:
How can I make it so that when the records are populated, it colours the relevant button?
If I know the name of the button to colour in, I can simply use btnSeat10.color = color.green etc. Is it possible to do something like this...
Dim strSeat as String
btn &strSeat.color = color.green
Obviously this doesn't work, as it isn't valid syntax (VB can be sooooo frustrating sometimes!).
Hope you understand my query, I look forward to any suggestions.
Thanks,
Shifty. :wave:
Re: [2005] DataBinding Buttons?
What you should do is create a Dictionary where the Buttons are the values and the user IDs are the keys. Then if you have an ID you simply get the corresponding Button from the Dictionary and set its BackColor, e.g.
vb.net Code:
Dim chairsByID As New Dictionary(Of Integer, Button)
Me.chairsByID.Add(1, Me.Button1)
Me.chairsByID.Add(2, Me.Button2)
Me.chairsByID.Add(3, Me.Button3)
'get the Button that corresponds to ID 2.
Dim btn As Button = Me.chairsByID(2)
btn.BackColor = Color.Green
Re: [2005] DataBinding Buttons?
That's great... never heard of 'Dictionary' before... looks a bit like an array.
I'll give it a try later and post the outcome on here.
Cheers!
Shifty.
Re: [2005] DataBinding Buttons?
Fantastic, it works a treat. The only difference for me, was that I changed the Integer to a string value (as this was already a field in my database) and passed it as a variable, my code as follows:
Dim UserID As New Dictionary(Of String, Button)
UserID.Add("floor2user1", Me.btnFloor2User1)
UserID.Add("floor2user2", Me.btnFloor2User2)
UserID.Add("floor2user3", Me.btnFloor2User3)
UserID.Add("floor2user4", Me.btnFloor2User4)
Dim btn As Button = UserID(strUserNumber)
btn.BackColor = Color.LimeGreen
The only thing that caught me out was that my variable (strUserNumber) was all in lower-case, which caused an error complaining that the value wasn't in the directory. I changed the string values above to lower-case, and bingo!
Thanks my friend, this one has been causing me a headache all week!
Shifty.