3
votes

I have created a label called txt1 programmatically inside a private void when the app is running ( in runtime ), I want to change the text of this label inside another private void, but I can't get access to txt1 from another void!

script for creating the label dynamically:

private void labelCreate() 
{
Label txt1 = new Label();
}

script for changing the text of txt1 which has been created in labelCreatevoid ( & this script doesn't work because txt1 has not been declared as a control ):

private void labelTextChange()
{
txt1.Text = "Hello World!";
}

Update 1: I need to create 100 labels which have different names then I will use a for statement for creating 100 labels. I can't declare 100 global variables. So I need to pass the variables instead of declaring them as global.

Update 2: Is it possible to declare 100 labels inside a for statement as global?

Update 3: Suppose that I want to fetch some data from a DB, I want to show them separately in unique labels. So I name each label & I change the texts of them according to different data that I get from DB! So I need 2 voids: one for creating the labels according to the number of rows that I get from DB & the other void for changing the texts of labels which I have created before!

Question: How can I have access on a control which has been created in different void? If there is an answer please share the link :)

Thanks

4
By having it as a field of your class.Biesi Grr
@BiesiGrr: can you please provide a resource or a complete answer?user9936276
The approach that you're taking to implement this functionality is gonna make the software very complicated because the label control isn't designed to be used like this, and there are other controls that provide functionality to display tabular data. You'll need to do ui stuff like setting the location and size of the labels for example..Lennart
@Lennart: I am simulating the scenario with label just as an example.user9936276
@E. Mamaghani: Ah, alright.. Figured u knew about what variable scopes are.Lennart

4 Answers

4
votes

Declare Label Globally in your class

Label txt1;
private void labelCreate() 
{
  txt1 = new Label();
}

Than access in another method

private void labelTextChange()
{
 txt1.Text = "Hello World!";
}

Edit 1

If you have number of Labels, While creating Labels you can store those objects in an array and when you need to change those text do it like

Label[] labelsArray //declare globally
private void labelTextChange()
{
    // Get Label objects from array
    labelsArray = { label1, label2, label3, label4, label5 };
    for (int i = 0; i < labelsArray.Count(); i++)
    {
        labelsArray[i].Text = "Hello.. " + (i + 1).ToString();
    }
}
2
votes

If you have a dynamic number of labels you could use a list.

List<Label> labels;

private void labelCreate() 
{
    labels = new List<Label>(); 
    for(int i = 0; i < 100; i++)
    {
        labels.Add(new Label());
    }   
}

private void labelTextChange()
{   
    // use the index or search for the name of the label
    labels[42].Text = "Hello World!";
}

Here is some information about the lifetime of variables and their accessibility (where the variable can be read from and/or written to) you could find helpflul.

2
votes

In your code Label1 is a private variable which is not accessible in labelTextChange method.

You need to declare txt1 as class variable.

Label txt1;

 private void labelCreate() 
    {
    txt1 = new Label();
    }

    private void labelTextChange()
    {
    txt1.Text = "Hello World!";
    }
1
votes

The problem that you are facing has to do with the following:

  • When you create an object in a function, you can only use it in the function itself.
  • To be able to use a object (Label in this case), you have to declare it outside of the function, usually above all functions. (Label txt1;)
  • After having declared a Label outside of the function, you can use one function to instantiate the object (txt1 = new Label();)
  • In the other function you can set the .Text property of the label

EDIT 1: What you're looking for is a field that can store multiple labels like a List, or an array. Considering the usage of this field, i'd say a List is a good option.

If you would want to make your life alot easier, i'd use a ListBox or a DataGridView instead of alot of labels though. As far as I know, labels aren't meant to be used in the way that you describe, there are other controls as i described above that are more suitable for this kind of application.

EDIT 2:

int amountOfRows;
Label[] labels;

public void setLabels(List<string> inputData)
{
    //allocate memory for the array
    amountOfRows = inputData.Count;
    labels = new Label[amountOfRows];
    for(int i=0; i<amountOfRows; i++)
    {
        labels[i] = new Label();
        //set properties like location and size of labels here first
        labels[i].Text = inputData[i];
    }
}