473,379 Members | 1,220 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,379 software developers and data experts.

Newbe question

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

Nov 20 '05 #1
1 1987
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" <jh***@cox.net>
| Sender: "Jim" <jh***@cox.net>
| 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
|
|

Nov 20 '05 #2

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

Similar topics

4
by: WindAndWaves | last post by:
Hi Gurus I hope I am going to make sense with this question: I have an html page that I have turned into a php page with a bit of php code above the html (connect to database, massage data a...
1
by: Sebastien GIRAUD | last post by:
Hello, First let say that am french and that i'll try to write the best english i can... I'm a python newbe and have the following problem : I try to create 2 threads in a server program and they...
1
by: Ron | last post by:
Is this built into any of the python versions? Need it! Using 2.3.5 and doesn't seem to have it.Newbe needs help!email ron@nac.net Thanks Ron
7
by: Jean Pierre Daviau | last post by:
Hi, <script language="javascript" type="text/javascript"> if(navigator.appName.indexOf("Netscape") != -1){ document.writeln('<link rel="stylesheet" href="~styles/aquarelle_ns.css"...
9
by: Yaro | last post by:
Hello DB2/NT 8.1.3 Sorry for stupid questions. I am newbe in DB2. 1. How can I read *.sql script (with table and function definitions) into a database? Tool, command... 2. In Project Center...
6
by: ken | last post by:
Hi all, I copied this code from the examples. "How to: Receive Strings From Serial Ports in Visual Basic" When I call the function and using the single step method it hangs at Dim Incoming As...
19
by: AMP | last post by:
I have a simple question. If i have a button on form1 that creates : Form2 newform = new Form2(); newform.Show(); As I click the button a new form shows,but acording to my code each one has...
17
by: Eric_Dexter | last post by:
def simplecsdtoorc(filename): file = open(filename,"r") alllines = file.read_until("</CsInstruments>") pattern1 = re.compile("</") orcfilename = filename + "orc" for line in alllines: if not...
13
by: Eric_Dexter | last post by:
All I am after realy is to change this reline = re.line.split('instr', '/d$') into something that grabs any line with instr in it take all the numbers and then grab any comment that may or may...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.