473,661 Members | 2,457 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Create C# controls names at runtime

Hi all,

I'm trying to create a system where it reads a number of records from
a database and then creates a row in the GUI that contains a single
field from the database and a button that has a reference to the
record. This way, once the task has been accomplished, the button can
be pressed next to the field and a time-stamp is placed into the
database.

Can anyone point me in the direction of either a tutorial or
documentation that may help me achive this?

Thanks,

Matt

May 2 '07 #1
7 11348
I'm not sure I understand your question, but perhaps this will help:

I suggest you create the button and text statically, in the GUI builder.
Then study the code it generated in form.designer.c s. Copy that code (with
obvious modifications) to your application to dynamically create the
controls as triggered by your application logic.
"Matt" <ma************ ***********@fuj ifilmsericol.co mwrote in message
news:11******** **************@ h2g2000hsg.goog legroups.com...
Hi all,

I'm trying to create a system where it reads a number of records from
a database and then creates a row in the GUI that contains a single
field from the database and a button that has a reference to the
record. This way, once the task has been accomplished, the button can
be pressed next to the field and a time-stamp is placed into the
database.

Can anyone point me in the direction of either a tutorial or
documentation that may help me achive this?

Thanks,

Matt

May 2 '07 #2
On 2 May, 13:33, "Fred Mellender" <nospamPlease_f r...@frontierne t.net>
wrote:
I'm not sure I understand your question, but perhaps this will help:

I suggest you create the button and text statically, in the GUI builder.
Then study the code it generated in form.designer.c s. Copy that code (with
obvious modifications) to your application to dynamically create the
controls as triggered by your application logic.

"Matt" <matthew.macdon ald-wall...@fujifil msericol.comwro te in message

news:11******** **************@ h2g2000hsg.goog legroups.com...
Hi all,
I'm trying to create a system where it reads a number of records from
a database and then creates a row in the GUI that contains a single
field from the database and a button that has a reference to the
record. This way, once the task has been accomplished, the button can
be pressed next to the field and a time-stamp is placed into the
database.
Can anyone point me in the direction of either a tutorial or
documentation that may help me achive this?
Thanks,
Matt
Fred,

Thanks for the reply, I'll try and explain a bit better!

Basically, what I want to end up with is a grid as follows:

ROW1 description of task [button1]
ROW2 description of task [button2]
ROW3 description of task [button3]
....

Ideally, what I'd like to do is name the button after the task id from
the database. I've just discovered that I can rename buttons on the
fly, so I think that the following may work:

Button btn = new Button();
btn.name = dr[0]; // set the button name to the first column in the
data row

the question is, how do I then create the function to fire the data
into the database?

Matt.

May 2 '07 #3
On 2 May, 13:41, Matt <matthew.macdon ald-wall...@fujifil msericol.com>
wrote:
On 2 May, 13:33, "Fred Mellender" <nospamPlease_f r...@frontierne t.net>
wrote:
I'm not sure I understand your question, but perhaps this will help:
I suggest you create the button and text statically, in the GUI builder.
Then study the code it generated in form.designer.c s. Copy that code (with
obvious modifications) to your application to dynamically create the
controls as triggered by your application logic.
"Matt" <matthew.macdon ald-wall...@fujifil msericol.comwro te in message
news:11******** **************@ h2g2000hsg.goog legroups.com...
Hi all,
I'm trying to create a system where it reads a number of records from
a database and then creates a row in the GUI that contains a single
field from the database and a button that has a reference to the
record. This way, once the task has been accomplished, the button can
be pressed next to the field and a time-stamp is placed into the
database.
Can anyone point me in the direction of either a tutorial or
documentation that may help me achive this?
Thanks,
Matt

Fred,

Thanks for the reply, I'll try and explain a bit better!

Basically, what I want to end up with is a grid as follows:

ROW1 description of task [button1]
ROW2 description of task [button2]
ROW3 description of task [button3]
...

Ideally, what I'd like to do is name the button after the task id from
the database. I've just discovered that I can rename buttons on the
fly, so I think that the following may work:

Button btn = new Button();
btn.name = dr[0]; // set the button name to the first column in the
data row

the question is, how do I then create the function to fire the data
into the database?

Matt.
OK, I now have the data being red into a datarow which then creates
the row for a tableLayoutPane l to allow me to adjust the rows etc.

The code I currently have for the tableLayoutRow is as follows:

foreach (DataRow dr in dra)
{

// create the label
Label taskDesc = new Label();
taskDesc.Width = 500;
// create the button
Button btn = new Button();
btn.Text = "Complete Task";
btn.Name = "btn"+dr[0].ToString();
MessageBox.Show ("Btn name = " + btn.Name);
taskDesc.Text = dr[1].ToString();
tlop.Controls.A dd(taskDesc,0,i );
//tlop.Controls.A dd(btn+dr[0].ToString(),1,i );
i++;
}

Where tlop is the tableLayoutPane l The issue I am having is when it
comes to adding the button to the panel (the commented out line) I get
an error that I cannot convert an object to a control. The variable
is showing up as btn1, btn2 etc but I can't read it into the tlop!

Help!

Matt

May 2 '07 #4
The Add function does not want the name of the button, but the button
itself, as in:

Controls.Add(bt n);

To add the function that handles the click event, first code the function in
the form's class. Suppose its name is "myClick". Then after you create the
button, add the code:

btn.Click += new System.EventHan dler(myClick);

again, if you study the code generated by VS, you will see code for your
statically defined controls that you can use as a template.
OK, I now have the data being red into a datarow which then creates
the row for a tableLayoutPane l to allow me to adjust the rows etc.

The code I currently have for the tableLayoutRow is as follows:

foreach (DataRow dr in dra)
{

// create the label
Label taskDesc = new Label();
taskDesc.Width = 500;
// create the button
Button btn = new Button();
btn.Text = "Complete Task";
btn.Name = "btn"+dr[0].ToString();
MessageBox.Show ("Btn name = " + btn.Name);
taskDesc.Text = dr[1].ToString();
tlop.Controls.A dd(taskDesc,0,i );
//tlop.Controls.A dd(btn+dr[0].ToString(),1,i );
i++;
}

Where tlop is the tableLayoutPane l The issue I am having is when it
comes to adding the button to the panel (the commented out line) I get
an error that I cannot convert an object to a control. The variable
is showing up as btn1, btn2 etc but I can't read it into the tlop!

Help!

Matt

May 2 '07 #5
On 2 May, 15:50, "Fred Mellender" <nospamPlease_f r...@frontierne t.net>
wrote:
The Add function does not want the name of the button, but the button
itself, as in:

Controls.Add(bt n);

To add the function that handles the click event, first code the function in
the form's class. Suppose its name is "myClick". Then after you create the
button, add the code:

btn.Click += new System.EventHan dler(myClick);

again, if you study the code generated by VS, you will see code for your
statically defined controls that you can use as a template.
OK, I now have the data being red into a datarow which then creates
the row for a tableLayoutPane l to allow me to adjust the rows etc.
The code I currently have for the tableLayoutRow is as follows:
foreach (DataRow dr in dra)
{
// create the label
Label taskDesc = new Label();
taskDesc.Width = 500;
// create the button
Button btn = new Button();
btn.Text = "Complete Task";
btn.Name = "btn"+dr[0].ToString();
MessageBox.Show ("Btn name = " + btn.Name);
taskDesc.Text = dr[1].ToString();
tlop.Controls.A dd(taskDesc,0,i );
//tlop.Controls.A dd(btn+dr[0].ToString(),1,i );
i++;
}
Where tlop is the tableLayoutPane l The issue I am having is when it
comes to adding the button to the panel (the commented out line) I get
an error that I cannot convert an object to a control. The variable
is showing up as btn1, btn2 etc but I can't read it into the tlop!
Help!
Matt
Fred, that's great but how do I create the control in the first place
if the parameter for controls.add() changes with every row?

I understand the concepts of creating a button, I just don't know how
to create a button then name it dynamically.

May 2 '07 #6
Hi,
"Matt" <ma************ ***********@fuj ifilmsericol.co mwrote in message
news:11******** **************@ h2g2000hsg.goog legroups.com...
Hi all,

I'm trying to create a system where it reads a number of records from
a database and then creates a row in the GUI that contains a single
field from the database and a button that has a reference to the
record. This way, once the task has been accomplished, the button can
be pressed next to the field and a time-stamp is placed into the
database.
Would it work for you having a button outside the listview ?
IMHO this is much easy to implement, faster to execute and do not use a lot
of buttons int he interface.

You could use the Tag property of the listviewitem to keep track of the
record in question:

foreach( DataRow row in GetRows.......)
{
ListViewItem lvi = new ...
lvi.Tag = row;
....
}

void TimeStamp_Oncli ck( ... )
{
DataRow row = (DataRow) listView1.Items[
listView1.Selec tedIndices[0]].Tag;
}
May 2 '07 #7
Each button need not have a separate *variable* name used to create it.
I.E. you are creating one *new* button with the
Button btn = new Button();
each time you go through the loop. Just do all the work for that button
(using the "btn" variable) inside the loop. Once you add it via the
Controls.Add statement, one new button is attached to the form. When you
are done with the loop, you have created many new buttons, all via the
variable "btn" (which is different, conceptually, than the button's Name).

However, you may wish to clean up the buttons the next time through your
function (or, you can just let the gc collect them and it will call
Dispose()). In the former case, you should/could save the buttons as you
create them, in an array (declared (as a variable) in the Form class you are
creating):

List<ButtonmyBu ttons = null;

Then, just before you *enter* the loop:

if (myButtons != null)
{
foreach (Button myButton in myButtons)
myButton.Dispos e();
}
myButtons = new List<Button>(10 );

then inside the loop, in addition to your existing statements, place:
myButtons.Add(b tn);

If you want to do something else with the buttons, outside the loop
(although I don't know why you would), you can get at them via the
"myButtons" variable.

"Matt" <ma************ ***********@fuj ifilmsericol.co mwrote in message
news:11******** **************@ h2g2000hsg.goog legroups.com...
On 2 May, 15:50, "Fred Mellender" <nospamPlease_f r...@frontierne t.net>
wrote:
>The Add function does not want the name of the button, but the button
itself, as in:

Controls.Add(bt n);

To add the function that handles the click event, first code the function
in
the form's class. Suppose its name is "myClick". Then after you create
the
button, add the code:

btn.Click += new System.EventHan dler(myClick);

again, if you study the code generated by VS, you will see code for your
statically defined controls that you can use as a template.
OK, I now have the data being red into a datarow which then creates
the row for a tableLayoutPane l to allow me to adjust the rows etc.
The code I currently have for the tableLayoutRow is as follows:
foreach (DataRow dr in dra)
{
// create the label
Label taskDesc = new Label();
taskDesc.Width = 500;
// create the button
Button btn = new Button();
btn.Text = "Complete Task";
btn.Name = "btn"+dr[0].ToString();
MessageBox.Show ("Btn name = " + btn.Name);
taskDesc.Text = dr[1].ToString();
tlop.Controls.A dd(taskDesc,0,i );
//tlop.Controls.A dd(btn+dr[0].ToString(),1,i );
i++;
}
Where tlop is the tableLayoutPane l The issue I am having is when it
comes to adding the button to the panel (the commented out line) I get
an error that I cannot convert an object to a control. The variable
is showing up as btn1, btn2 etc but I can't read it into the tlop!
Help!
Matt

Fred, that's great but how do I create the control in the first place
if the parameter for controls.add() changes with every row?

I understand the concepts of creating a button, I just don't know how
to create a button then name it dynamically.

May 2 '07 #8

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

Similar topics

5
1487
by: Patrick Marti | last post by:
I wish to create some LinkButtons in DotNet. Because I will do it in dependence of the entries in a database, I can not add them with the mouse to the form as usually. I can create them in the Page_Load event but then I can not see them. I do not know the reason or how to do it correctly. Do anywhere know something about? For any help, I thanks a lot Greetings Patrick Marti
7
8850
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I want my users to be able to select a report, click on a command button on a form, which will then automatically create the report as a pdf file and save it to the user's machine. I am using Adobe Acrobat (5.0 I think) and have Adobe Distiller as a
2
1515
by: Mark Siffer | last post by:
I am trying to add a compare validator at runtime. I would add it before but my input controls are built at runtime via an editable datagrid. Therefore, the names of the controls are not known until the page has been rendered. Any ideas? Thanks in advance MS
1
2726
by: Luc | last post by:
Hi, I have a TabControl and, at runtime, I need to add some tabpages. The problem is that each tabpage is similar to the others and contains several controls. If I do TabControl.TabPages.Add(MyTabPage), a new BLANK tabpage is added. How can I add in few statements a new tabpage as well as its controls (textboxes, labels, etc.)? The first tabpage (that I create at design time) is the "template" to be used for the other tabpages. Is...
14
1983
by: 97T | last post by:
Well this is still bugging me. I know there are other ways around this, but for a number of reasons I would like to be able to do this one simple thing. I have a form with a number of controls on it. I have created these controls in the form design tool, and given them unique names. I would like to be able to put together a line of code that allows this:
1
1290
by: Terrance | last post by:
I have a problem I was hoping someone can help me with. My first problem is moving a link label that is created at runtime to the of an arraylist on the next line in the listbox. Currently, the Close link is showing up on the first line and covering up the file names when the box is populated. Here is the code: Dim docArray As New ArrayList() 'create an arraylist to hld the documents. Dim strDirectory =...
0
1589
by: anandv81 | last post by:
Hi, I encountered a strange problem while working on an application, the problem goes like this. I am generating a few textboxes at runtime at the server side and added to a placeholder, a value is set for each of the textboxes. There is a button that causes the page to be posted back and the texboxes are re-rendered. When the textboxes are re-rendered the values are reset and these texboxes are again added to the placeholder. The names
0
1954
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hello Gurus, I need to create a Microsoft Visual Studio 2005 report at runtime. I wrote a C# window application, that holds a DataSet and several DataGridView controls. Each of the DataGridView controls show filtered data of the DataSet. Using the Visual Studio 2005 designer, I have generated RDLC file with report tables and a Form that holds this reports for display. and its all
2
6169
by: sagarbakliwal | last post by:
hi, I am new to this forum but i have received a lot of help reading through this forum. I am creating a VB.NET application for Patient Record Management. In this there are various combo boxes and buttons being generated during run time when a particular patient ID is searched for. Say one patient might have 2 entries some may have 10 entries. So the controls are generated dynamically and then the control names are added to the forms...
0
8851
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8754
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8542
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8630
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6181
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4343
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1984
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1740
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.