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

How to dynamically create a button?

What am I missing? I am trying to dynamically create a button...

Button myButton = new Button();
myButton.Name="ButtonX";
Nov 17 '05 #1
5 16104
Keith Smith schrieb:
What am I missing? I am trying to dynamically create a button...

Button myButton = new Button();
myButton.Name="ButtonX";


myButton.Location = new Point(8,8);
myButton.Text = "Click me";
this.Controls.Add(myButton); // Adds the button to the form (if you put
this code e.g. in the constructor or better in InitializeComponent)

HTH

Michael
Nov 17 '05 #2

"Keith Smith" <ke*********@verizon.net> wrote in message
news:eK**************@TK2MSFTNGP15.phx.gbl...
What am I missing? I am trying to dynamically create a button...

Button myButton = new Button();
myButton.Name="ButtonX";


Well, you need to set a location, size, and add it to the control you want
it to appear on.
Nov 17 '05 #3
Hi,
In addition to the other post you better take a look at the code generated
by the designer. You will learn few things.

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Keith Smith" <ke*********@verizon.net> wrote in message
news:eK**************@TK2MSFTNGP15.phx.gbl...
What am I missing? I am trying to dynamically create a button...

Button myButton = new Button();
myButton.Name="ButtonX";

Nov 17 '05 #4
>> Button myButton = new Button();
myButton.Name="ButtonX";


myButton.Location = new Point(8,8);
myButton.Text = "Click me";
this.Controls.Add(myButton); // Adds the button to the form (if you put
this code e.g. in the constructor or better in InitializeComponent)


Thanks alot, Michael!

I forgot to mention this earlier.... How can I make an "array" of these
buttons as well? Would it be sometime like this...?

Button myButton = new array[10]
Nov 17 '05 #5
Hi,

Yes, that's the way of doing

But note that unless you need to change properties ( visible, position,
etc) of them during the course of the execution you do not need hold a
reference to them.
Usually what you do is create event handlers ( like Click ) and you interact
with them this way.

Also note that you can use Controls.AddRange instead of Controls.Add , also
a good idea to wrap it in a Suspend/ResumeLayout calls

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Keith Smith" <ke*********@verizon.net> wrote in message
news:er**************@TK2MSFTNGP14.phx.gbl...
Button myButton = new Button();
myButton.Name="ButtonX";


myButton.Location = new Point(8,8);
myButton.Text = "Click me";
this.Controls.Add(myButton); // Adds the button to the form (if you put
this code e.g. in the constructor or better in InitializeComponent)


Thanks alot, Michael!

I forgot to mention this earlier.... How can I make an "array" of these
buttons as well? Would it be sometime like this...?

Button myButton = new array[10]

Nov 17 '05 #6

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

Similar topics

2
by: Sean | last post by:
Hello all, I may just not be searching for the right thing, but I've been looking for a way to dynamically create controls (and name them) in my code so that I can create only the controls I...
0
by: goitz | last post by:
Im looking for a way to dynamically create a table and append a text box to it using a list of information from a database eg> display a results of a select statment then add a text box--> field1...
2
by: Quentin Huo | last post by:
Hi: I have a user control and I want to dynamically create a Label control and TextBox control when the button "add more author name" is clicked. I did like this: 1. there is a...
4
by: dyw55a | last post by:
Donna Mar 15, 10:11 am show options Newsgroups: microsoft.public.dotnet.framework.adonet From: "Donna" <dyw...@yahoo.com> - Find messages by this author Date: 15 Mar 2005 10:11:56 -0800...
7
by: pmclinn | last post by:
I was wondering if it is possible to dynamically create a structure. Something like this: public sub main sql = "Select Col1, Col2 from Table a" dim al as new arraylist al =...
7
by: rsaffy | last post by:
I am having trouble with my dynamically created button's event handling. I read that the buttons need to be recreated on every trip to the server, but how exactly do you do that when the datagrid...
2
by: mrceas | last post by:
Using VB.net, i want to dynamically create a workbook and add a command botton to one of the sheets. how would i do this. I see example using VBA but i am not able to translate them to Vb.net. ...
2
by: Lou | last post by:
I want to dynamically create an unknown amount of buttons into a panel. I want each button to respond to the same click event. In VB6 I would just create an array of buttons. I do know how to...
2
by: Erland Sommarskog | last post by:
Karl Rhodes (googlegroups@cortexa.co.uk) writes: The exact reason for the error is that you pass parameters to the command batch where you create the procedure. That does not fly. Nor would...
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:
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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.