473,583 Members | 3,072 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Changing button.backcolo r from listbox_doublec lick event

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

I thought it would be something like this:

If Me.Controls.Ite m(iSeatNumber). BackColor.Equal s(Color.White) Then
Me.Controls.Ite m(iSeatNumber). BackColor.Equal s(Color.Cornflo werBlue)
'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.ite m would essentially be
one of the buttons created.


I also tried this:

btn(iSeatNumber ).backcolor.equ als(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 lstSeatAvailabl e_DoubleClick(B yVal sender As Object, ByVal e As
System.EventArg s) Handles lstSeatAvailabl e.DoubleClick

Select Case CStr(lstSeatAva ilable.Items.It em(iSeatNumber) )

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

lstSeatAvailabl e.Items.Item(iS eatNumber) = "Seat " & (iSeatNumber) & " is
available"

'If Me.Controls.Ite m(iSeatNumber). BackColor.Equal s(Color.White) Then

' Me.Controls.Ite m(iSeatNumber). BackColor.Equal s(Color.Cornflo werBlue)

'End If

Dim ix1 As Integer, ix2 As Integer

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

For ix1 = 0 To Me.Controls.Cou nt

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

btn.BackColor.E quals(Color.Whi te)

End If

Next

ix1 = Nothing

ix2 = Nothing

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

lstSeatAvailabl e.Items.Item(iS eatNumber - 1) = "Seat " & (iSeatNumber) & "
is booked"

Dim ix1 As Integer, ix2 As Integer

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

For ix1 = 0 To Me.Controls.Cou nt

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

btn.BackColor.E quals(Color.Whi te)

End If

Next

ix1 = Nothing

ix2 = Nothing

'Implicitly set control white colored showing seat as booked

'Me.Controls.It em(iSeatNumber) .BackColor.Equa ls(Color.White)

End Select

Dim ix3 As Integer

For ix3 = 0 To Me.Controls.Cou nt - 1

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

btn.BackColor.E quals(Color.Whi te)

End If

Next

ix3 = Nothing

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

I thought it would be something like this:

If Me.Controls.Ite m(iSeatNumber). BackColor.Equal s(Color.White)
Then
Me.Controls.Ite m(iSeatNumber). BackColor.Equal s(Color.Cornflo werBlue)
'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.ite m would
essentially be one of the buttons created.


I also tried this:

btn(iSeatNumber ).backcolor.equ als(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.Ite m(iSeatNumber). BackColor.Equal s(Color.White) Then
Me.Controls.Ite m(iSeatNumber). BackColor.Equal s(Color.Cornflo werBlue)
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.Cornflowe rBlue

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(ButtonC ount - 1)
for i = 0 to ButtonCount - 1
Buttons(i) = New Button
'here: set location and other properties of
'the button, e.g. Buttons(i).Loca tion = ...
Next i

Later, you can access the buttons using the index:
Buttons(iSeatNu mber).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(B ookedText, iSeatNumber)

Also replace
lstSeatAvailabl e.Items.Item(iS eatNumber - 1) = "Seat " & (iSeatNumber) &
" is booked"
by
lstSeatAvailabl e.Items.Item(iS eatNumber - 1) = String.Format(B ookedText,
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.Cou nt - 1 ' -1 to remove list box from iteration

but you never use ix2.

10. You write

For ix1 = 0 To Me.Controls.Cou nt

This will lead to an exception because it must be

For ix1 = 0 To Me.Controls.Cou nt - 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
4745
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 with the fact that the gif files have a transperent region in the middle (the button icon) that lets the back color of the picture box show through....
5
1322
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: System::Windows::Forms::Button * Wolfgang; for (i=1;i<100;i++) { Wolfgang = ???;
1
1653
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 type of behaviour before? Can anyone suggest a solution? Thanks in advance, Stu
18
2943
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. If you do have any ideas I would really like to hear them. Thanks Colin - 0 - 0 - 0 - I want a glorified popup/context menu on a button that...
1
2693
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 familiar with its intricacies, so I'm getting a bunch of errors on certain scenarios. The code below works fine but breaks if I click on Button1 a...
3
15394
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 the user clicks on the cell: Private Sub DataGrid1_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles...
12
3165
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 As Button) btnName.BackColor = Color.BlanchedAlmond End Sub
0
872
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 button1->BackColor=Color::White; //changes color of button1 MessageBox::Show("Hello"); //Show is in the scope MessageBox??? button1->Text="BUTTON!"; ...
3
2590
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 he can click to flag a pupil as "Do Not Rebook" (i.e. if they're a non-payer or something). Upon clicking the button, he wants a value to be recorded...
0
7895
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7826
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8182
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8327
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
5374
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3843
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2333
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1433
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.