472,978 Members | 1,959 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,978 software developers and data experts.

Changing button.backcolor from listbox_doubleclick event

I am attempting to change the backColor property on the previously
instantiated buttons FROM a listbox_doubleClick event.

I thought it would be something like this:

If Me.Controls.Item(iSeatNumber).BackColor.Equals(Col or.White) Then
Me.Controls.Item(iSeatNumber).BackColor.Equals(Col or.CornflowerBlue)
'End If

Just as an FYI, there are 100 buttons on my form and they were instantiated
at run time.
iSeatNumber would be an integer and me.controls.item would essentially be
one of the buttons created.


I also tried this:

btn(iSeatNumber).backcolor.equals(Color.White)

but intellisense says that the button cannot be accessed because it has no
default property. Could it be that I need to pass the buttons object to the
doubleClick event?

Any ideas on this would help a lot!

Bob
My code as follows:

Private Sub lstSeatAvailable_DoubleClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles lstSeatAvailable.DoubleClick

Select Case CStr(lstSeatAvailable.Items.Item(iSeatNumber))

Case CStr("Seat " & (iSeatNumber) & " is booked")

lstSeatAvailable.Items.Item(iSeatNumber) = "Seat " & (iSeatNumber) & " is
available"

'If Me.Controls.Item(iSeatNumber).BackColor.Equals(Col or.White) Then

' Me.Controls.Item(iSeatNumber).BackColor.Equals(Col or.CornflowerBlue)

'End If

Dim ix1 As Integer, ix2 As Integer

ix2 = Me.Controls.Count - 1 ' -1 to remove list box from iteration

For ix1 = 0 To Me.Controls.Count

If Me.Controls.Item(ix1).Name = "btn" & iSeatNumber Then

btn.BackColor.Equals(Color.White)

End If

Next

ix1 = Nothing

ix2 = Nothing

Case CStr("Seat " & (iSeatNumber) & " is available")

lstSeatAvailable.Items.Item(iSeatNumber - 1) = "Seat " & (iSeatNumber) & "
is booked"

Dim ix1 As Integer, ix2 As Integer

ix2 = Me.Controls.Count - 1 ' -1 to remove list box from iteration

For ix1 = 0 To Me.Controls.Count

If Me.Controls.Item(ix1).Name = "btn" & iSeatNumber Then

btn.BackColor.Equals(Color.White)

End If

Next

ix1 = Nothing

ix2 = Nothing

'Implicitly set control white colored showing seat as booked

'Me.Controls.Item(iSeatNumber).BackColor.Equals(Co lor.White)

End Select

Dim ix3 As Integer

For ix3 = 0 To Me.Controls.Count - 1

If Me.Controls.Item(ix3).Name = "btn" & iSeatNumber Then

btn.BackColor.Equals(Color.White)

End If

Next

ix3 = Nothing

end sub
Nov 20 '05 #1
1 4052
"Firewalker" <fi***********@cox.net> schrieb
I am attempting to change the backColor property on the previously
instantiated buttons FROM a listbox_doubleClick event.

I thought it would be something like this:

If Me.Controls.Item(iSeatNumber).BackColor.Equals(Col or.White)
Then
Me.Controls.Item(iSeatNumber).BackColor.Equals(Col or.CornflowerBlue)
'End If

Just as an FYI, there are 100 buttons on my form and they were
instantiated at run time.
iSeatNumber would be an integer and me.controls.item would
essentially be one of the buttons created.


I also tried this:

btn(iSeatNumber).backcolor.equals(Color.White)

Hint: If you first copy your code to notepad and from there into your
posting, identation is not lost and no blank lines are inserted. It makes it
better to read for us.
Some (more or less important) comments to your code:

1.
If Me.Controls.Item(iSeatNumber).BackColor.Equals(Col or.White) Then
Me.Controls.Item(iSeatNumber).BackColor.Equals(Col or.CornflowerBlue)
End If

The code above does not change the color. Equals is a function to compare
the colors. In this case, the function is called but the return value is
ignored. It is like writing
Math.Sin(0)
It does call the Sin function but the return value isn't used - so it
doesn't make sense to call it.

You should write
...Backcolor = Color.CornflowerBlue

2. How and where did you declare btn? If your buttons are named btn1, btn2,
btn3, ..., you should declare an array instead and put the buttons in the
array. It is much easier to handle because you can simply use an index:

dim Buttons() As Button

Depending on where and how you create the buttons you can fill the array,
for example:

buttons = new button() {btn1, btn2, btn3, btn4, <and so on>}

As you can see, it would be a lot to type in. Creating the buttons in a loop
would be more comfortable:

const ButtonCount as integer = 100

dim i as integer

redim Buttons(ButtonCount - 1)
for i = 0 to ButtonCount - 1
Buttons(i) = New Button
'here: set location and other properties of
'the button, e.g. Buttons(i).Location = ...
Next i

Later, you can access the buttons using the index:
Buttons(iSeatNumber).BackColor = ...


3. Short version of
Dim ix1 As Integer, ix2 As Integer
is
Dim ix1, ix2 As Integer
4. Setting
ix1 = Nothing
ix2 = Nothing
does not make sense. Integer variables are value types, i.e. their value can
not be Nothing. If you want to assign the default value (assigning Nothing
assigns the default value), write ix1 = 0 because it is more obvious. In
this case there is no need to set ix1 or ix2 to zero.
5. In the line

Case CStr("Seat " & (iSeatNumber) & " is booked")

you don't need to call CStr and you also don't need the brackets around
iSeatNumber:

Case "Seat " & iSeatNumber & " is booked"

BUT, the line will be replaced anway, see below.

6. I guess you should always use iSeatNumber as the index in the listbox, or
iSeatNumber - 1. In your code you are mixing both versions.
7. You are writing "Seat...is available" twice, and you are comparing the
strings in the listbox. You should define a constant for this purpose or
write a function that returns the String containing the seat number:

Using a constant:
Const BookedText As String = "Seat {0} is booked"
'...

Instead of
Case CStr("Seat " & (iSeatNumber) & " is booked")
write
Case String.Format(BookedText, iSeatNumber)

Also replace
lstSeatAvailable.Items.Item(iSeatNumber - 1) = "Seat " & (iSeatNumber) &
" is booked"
by
lstSeatAvailable.Items.Item(iSeatNumber - 1) = String.Format(BookedText,
iSeatNumber)

8. It's considered as better design to separate the data from it's
presentation. In this case it means that you could/should create an array
that contains the booked status of all seats. The advantage is that you
don't have to work with the texts or values of the controls. For example,
you are comparing the text in the listbox or the color of the buttons. Using
an array (of type boolean) is simpler, and you can change the presentation
(e.g. different colors) without changing the logic of your comparisons.

9. You write:

ix2 = Me.Controls.Count - 1 ' -1 to remove list box from iteration

but you never use ix2.

10. You write

For ix1 = 0 To Me.Controls.Count

This will lead to an exception because it must be

For ix1 = 0 To Me.Controls.Count - 1
or
For ix1 = 0 To ix2

--
Armin

Nov 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Calan | last post by:
I have a form with some picture boxes as buttons. I'm setting the picture property to a gif file for "normal", and to a different gif for the "pressed" state. This works fine. I get into trouble...
5
by: Wolfgang | last post by:
Dear all, I've a lot of buttons named button1..button100 and want to change some of there propertys. Maintaining the overview I'm looking for a possibility for something like: ...
1
by: Stu | last post by:
Hi, I have a button in the footer of a datagrid that sometimes does not tigger the item command. The page is quite large & has a number of homegrown controls in it. Has anyone come across this...
18
by: Colin McGuire | last post by:
Hi - this was posted last weekend and unfortunately not resolved. The solutions that were posted almost worked but after another 5 days of working on the code everynight, I am not further ahead....
1
by: siliconpi | last post by:
I'm looking for the simplest and cleanest way of having 10 buttons on a form, on which if I move my mouse over, a label's text changes as specified. I'm using Visual Basic .NET and I'm not too...
3
by: Mad Scientist Jr | last post by:
Can someone post a clear example of how to change the background color of an individual datagrid cell for a Windows desktop app? (preferably in vb.net) I found some code on how to do this when...
12
by: Henry Jones | last post by:
I have a VS 2005 VB.NET project and would like to change the color of the textbox when the user hovers over it. In a Module I have the following routines: Public Sub Button_Hover(ByRef btnName...
0
by: tonyjeffs | last post by:
VC++Express; I have a form with one button. This is my code for the button1 click event. BackColor=Color::Red; // Red is in the scope Color ? changes color of Form1...
3
by: bettyboo | last post by:
Hi I'm new to the forum and also a VERY new user of Access to develop databases. I'm building a DB for a driving instructor acquaintance, and he wants a button on the pupil data entry form which...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.