Newbe question

  • Thread starter Thread starter Jim
  • Start date Start date
J

Jim

I have created a windows form that contains several tab
pages which contain a panels. On a tab page I am trying
to dynamically create a series of buttons in that pages
panel. I am failing because I can not find the proper way
to point to the specific tab page and its panel when
creating the buttons.

I also need to dynamically change the background color
property of each button as it is clicked.

I can dynamically create the buttons in a blank form with
the code below, but am unable to create them in the tab
page-panel and I can not dynamically change the
background color within my click event.

Any help would be greatly appreciated.

Code is below (I used the sample on the net for the
structure):

This is fired from a click event in a statically placed
button.

For sX = 0 To 5
Dim bX As New Button
bX.Name = "Level" + m_controlCount.ToString
bX.Text = "C" + m_controlCount.ToString
bX.Width = 60
bX.Height = 20
bX.Location = New Point(m_Location.X + 70,
m_Location.Y)
m_Location.X += 65

AddHandler bX.Click, AddressOf
LevelHandler_Click
AddHandler bX.MouseHover, AddressOf
LevelHandler_MouseHover

Controls.Add(bX)
m_controlCount += 1
bX.Visible = True

Next sX

Here is the LevelHandler code that does not work. The
entire form changes color:

Private Sub LevelHandler_Click(ByVal sender As
System.Object, ByVal e As EventArgs)
If TypeOf sender Is Button Then
If System.Drawing.ColorTranslator.ToOle
(Button.DefaultBackColor) =
System.Drawing.ColorTranslator.ToOle
(System.Drawing.Color.Red) Then

BackColor =
System.Drawing.ColorTranslator.FromOle(&H8000000F)
Else
BackColor = System.Drawing.Color.Red
End If
End If
End Sub
 
Hi Jim,
To add the buttons to the panel, you need to add it to the Panel's control
collection. In the code below you are adding them to the form since the
form's properties are exposed by default.

The one line of code you need to change is from this:
Controls.Add(bX)
To this:
MyPanel.Controls.Add(bX)

This is the same problem you are facing with the changing color. Simply
using BackColor = System.Drawing.Color.Red sets the form's backcolor to
red. To set the button's backcolor, you need to specifically say:
Sender.BackColor=System.Drawing.Color.Red

BackColor won't show up in intellisense on Sender since it is of type
object but it is valid in the case below since you make sure that Sender is
of type button. If you did want intellisense, you could cast the sender to
a button.

Hope that helps,
Eric
VB.Net team

--------------------
| Content-Class: urn:content-classes:message
| From: "Jim" <[email protected]>
| Sender: "Jim" <[email protected]>
| Subject: Newbe question
| Date: Wed, 22 Oct 2003 10:51:39 -0700
|
| I have created a windows form that contains several tab
| pages which contain a panels. On a tab page I am trying
| to dynamically create a series of buttons in that pages
| panel. I am failing because I can not find the proper way
| to point to the specific tab page and its panel when
| creating the buttons.
|
| I also need to dynamically change the background color
| property of each button as it is clicked.
|
| I can dynamically create the buttons in a blank form with
| the code below, but am unable to create them in the tab
| page-panel and I can not dynamically change the
| background color within my click event.
|
| Any help would be greatly appreciated.
|
| Code is below (I used the sample on the net for the
| structure):
|
| This is fired from a click event in a statically placed
| button.
|
| For sX = 0 To 5
| Dim bX As New Button
| bX.Name = "Level" + m_controlCount.ToString
| bX.Text = "C" + m_controlCount.ToString
| bX.Width = 60
| bX.Height = 20
| bX.Location = New Point(m_Location.X + 70,
| m_Location.Y)
| m_Location.X += 65
|
| AddHandler bX.Click, AddressOf
| LevelHandler_Click
| AddHandler bX.MouseHover, AddressOf
| LevelHandler_MouseHover
|
| Controls.Add(bX)
| m_controlCount += 1
| bX.Visible = True
|
| Next sX
|
| Here is the LevelHandler code that does not work. The
| entire form changes color:
|
| Private Sub LevelHandler_Click(ByVal sender As
| System.Object, ByVal e As EventArgs)
| If TypeOf sender Is Button Then
| If System.Drawing.ColorTranslator.ToOle
| (Button.DefaultBackColor) =
| System.Drawing.ColorTranslator.ToOle
| (System.Drawing.Color.Red) Then
|
| BackColor =
| System.Drawing.ColorTranslator.FromOle(&H8000000F)
| Else
| BackColor = System.Drawing.Color.Red
| End If
| End If
| End Sub
|
|
 
Back
Top