PC Review


Reply
Thread Tools Rate Thread

Create C# controls names at runtime

 
 
Matt
Guest
Posts: n/a
 
      2nd May 2007
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

 
Reply With Quote
 
 
 
 
Fred Mellender
Guest
Posts: n/a
 
      2nd May 2007
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.cs. Copy that code (with
obvious modifications) to your application to dynamically create the
controls as triggered by your application logic.


"Matt" <matthew.macdonald-(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
Matt
Guest
Posts: n/a
 
      2nd May 2007
On 2 May, 13:33, "Fred Mellender" <nospamPlease_fr...@frontiernet.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.cs. Copy that code (with
> obvious modifications) to your application to dynamically create the
> controls as triggered by your application logic.
>
> "Matt" <matthew.macdonald-wall...@fujifilmsericol.com> wrote in message
>
> news:(E-Mail Removed)...
>
> > 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.

 
Reply With Quote
 
Matt
Guest
Posts: n/a
 
      2nd May 2007
On 2 May, 13:41, Matt <matthew.macdonald-wall...@fujifilmsericol.com>
wrote:
> On 2 May, 13:33, "Fred Mellender" <nospamPlease_fr...@frontiernet.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.cs. Copy that code (with
> > obvious modifications) to your application to dynamically create the
> > controls as triggered by your application logic.

>
> > "Matt" <matthew.macdonald-wall...@fujifilmsericol.com> wrote in message

>
> >news:(E-Mail Removed)...

>
> > > 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 tableLayoutPanel 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.Add(taskDesc,0,i);
//tlop.Controls.Add(btn+dr[0].ToString(),1,i);
i++;
}

Where tlop is the tableLayoutPanel 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

 
Reply With Quote
 
Fred Mellender
Guest
Posts: n/a
 
      2nd May 2007
The Add function does not want the name of the button, but the button
itself, as in:

Controls.Add(btn);

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.EventHandler(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 tableLayoutPanel 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.Add(taskDesc,0,i);
> //tlop.Controls.Add(btn+dr[0].ToString(),1,i);
> i++;
> }
>
> Where tlop is the tableLayoutPanel 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
>



 
Reply With Quote
 
Matt
Guest
Posts: n/a
 
      2nd May 2007
On 2 May, 15:50, "Fred Mellender" <nospamPlease_fr...@frontiernet.net>
wrote:
> The Add function does not want the name of the button, but the button
> itself, as in:
>
> Controls.Add(btn);
>
> 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.EventHandler(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 tableLayoutPanel 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.Add(taskDesc,0,i);
> > //tlop.Controls.Add(btn+dr[0].ToString(),1,i);
> > i++;
> > }

>
> > Where tlop is the tableLayoutPanel 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.

 
Reply With Quote
 
Ignacio Machin \( .NET/ C# MVP \)
Guest
Posts: n/a
 
      2nd May 2007
Hi,


"Matt" <matthew.macdonald-(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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_Onclick( ... )
{
DataRow row = (DataRow) listView1.Items[
listView1.SelectedIndices[0]].Tag;
}


 
Reply With Quote
 
Fred Mellender
Guest
Posts: n/a
 
      2nd May 2007
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<Button> myButtons = null;

Then, just before you *enter* the loop:

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

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

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" <matthew.macdonald-(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On 2 May, 15:50, "Fred Mellender" <nospamPlease_fr...@frontiernet.net>
> wrote:
>> The Add function does not want the name of the button, but the button
>> itself, as in:
>>
>> Controls.Add(btn);
>>
>> 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.EventHandler(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 tableLayoutPanel 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.Add(taskDesc,0,i);
>> > //tlop.Controls.Add(btn+dr[0].ToString(),1,i);
>> > i++;
>> > }

>>
>> > Where tlop is the tableLayoutPanel 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.
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Create controls at runtime TonyMast Microsoft VB .NET 6 6th Apr 2006 12:16 PM
Create controls in a form at runtime =?Utf-8?B?UGFudGVsaWFkaXMgQmFiaXM=?= Microsoft Access Form Coding 4 2nd Oct 2004 06:27 PM
How to create controls at runtime Andy Chan Microsoft Excel Programming 3 30th Aug 2004 10:07 AM
How to create controls at runtime michele Microsoft ASP .NET 1 4th Aug 2004 08:28 PM
Create controls in runtime Alberto Microsoft ASP .NET 4 10th Mar 2004 05:58 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:10 PM.