473,387 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 4080
"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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.