Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C# Instantiating an object.. dynamic object name..

Status
Not open for further replies.

tahirk

Programmer
Oct 24, 2005
47
GB
Hi,

Needs ome advice on what is probably, a no brainer question however its beeing driving me round in circles.

I have created a simple WinForms application in VS.NET 2005 that loads a database table during runtime, and then based on some rules, proceeds to dynamically create controls on the form based on the data it finds in the table.

However I am having trouble working out how to loop through the dataset and then based on the total number of rows, create 'n' number of labels that have their Text property filled by the data found in a particular column e.g:

Code:
for (i = 0; i <= RowCount - 1; i++)
{
  Label Label = new Label();  //create a new label each time we hit a new row.. this bit is not working yet :s

  // set the location of the control, move it down the Y axis each time we loop
  Label.Location = new Point(12, YPos);

  // populate the text property for the label
  Label.Text = this.DataSet1.Table1.Rows[i]["Column Name"].ToString();

  YPos = YPos + 12;  // set the new Y scale value

  this.Controls.Add(Label);  // add the new control to the form
}

As per above, I got most of what I wanted working but got a little stuck when I needed to dynamically create a new Label each time. As this is being done during runtime I need the code to do the following:

1. Start Loop
2. Create a new instance of the Label Control
3. Work with this Control (properties etc)
4. Add this Control to the WinForm
5. Next Loop
6. Create the next new instane of the label Control
..
..
etc

Thanks in advance,

Fz
 
well you can't name your Label "Label" - that will cause a problem.

Try changing it's name.
 
well you can't name your Label "Label" - that will cause a problem.

erm my post was just an example..

I have tried changing the name but this makes no difference.

In a nutshell what I need to do is each time I get a new value, create a new instance of the object Label and put this component on the form e.g:

Code loops through and reads row 1, my code creates a new Label instance which is created in the following fashion:

Code:
"MyLabel" + i.ToString

Obviously the above line does not work however thats pretty much what I want to do, so if I am on row 2 and I read in the new Label text, I want to create a new instance that is the next increment (i.e. dynamically create and place the labels on the form):

Code:
"MyLabel" + i.ToString()

// i is currently on a count of 2 so a new label object needs to be instantiated using the variable MyLabel2

The problem I am having is trying to find a way that I can go through the loop, increment a counter, then based on the current count, create a new instance of a label object so what I am doing is programmatically creating however many labels are needed based on the data that has been read in.

The main functionality of my mini app is to read in data, based on this data and any rules I have setup, build a form that the user can view/interact with.

Thanks in advance,

Fz

 
Forgot to add:

so the code would look something like:

Label MyLabel+<the current counter number> = new Label();

Cheers,

Fz
 
what you need is a collection. actually you already have one (controls) but we will make a different one.

pardon my c#, I'm more used to vb.net

Code:
Dictionary<String,Label> labellist = new Dictionary<String,Label>;
for (i = 0; i <= RowCount - 1; i++)
{
  Label tempLabel = new Label();  //create a new label each time we hit a new row.. this bit is not working yet :s

  // set the location of the control, move it down the Y axis each time we loop
  tempLabel.Location = new Point(12, YPos);

  // populate the text property for the label
  tempLabel.Text = this.DataSet1.Table1.Rows[i]["Column Name"].ToString();

  YPos = YPos + 12;  // set the new Y scale value

lablellist.add("label" + i,tempLabel) 
  this.Controls.Add(tempLabel);  // add the new control to the form
}

and from here you can do whatever you want with the labels. Just by calling the collection

labellist("label1").propertynameyouwant



Christiaan Baes
Belgium

"My new site" - Me
 
Cool cool, didn't think of that but I don't see why it shouldn't work as a collection.

Not to worry about the syntax, I prefer to explain things in more english than code myself.. and I am an ex Delphi coder so C# still leaves me scratching my head sometimes ;o)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top