By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,143 Members | 1,854 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,143 IT Pros & Developers. It's quick & easy.

Changing button.backcolor from listbox_doubleclick event

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.