Access variable based elements

Asked

Viewed 313 times

7

I need to assign the value to a label, example: xrLabel1.text = "teste", but as I have several Labels where changes only the number at the end example:

xrLabel1.text = "teste";
xrLabel2.text = "teste";
xrLabel3.text = "teste";

How do I fill these values using a loop as in the example below:

for (int i = 0; i < 3; i++)
    string.Concat("xrLabel", i.ToString()).Text = "teste";
  • No, there are over 100 Labels that I need to change, it is impossible to do in hand.

  • What class of Labels?

  • Class Xrlabel. It’s a label of the report Devexpress.XtraReports.UI.Xtrareport. I need to do in all, is a very specific report where I have no way to use a grid with Datadource because they are very different data and so I have to set individually.

  • In the php this kind of thing is very easy to do. Call a function with variable name for example: $variable = "my_function"; MyClass->$variable();. But here at C# I’m not getting it.

2 answers

3


You can search the object by name:

for (int i = 0; i < 3; i++) {
    XRLabel label = (XRLabel)this.Controls[string.Concat("xrLabel", i.ToString())];
    label.text = "teste"; 
}

2

One way to do this would be to use a linq expression to select all the Labels of the form, and Enumerable.Zip iterate on the list of Labels and the outcome of select of DataTable:

// Simulando os dados do teu DataTable
DataTable tabela = new DataTable("Jogadores");
tabela.Columns.Add(new DataColumn("Nome",  typeof(string)));
tabela.Columns.Add(new DataColumn("Idade", typeof(int)));
tabela.Columns.Add(new DataColumn("Sexo",  typeof(char)));

tabela.Rows.Add("Maria",   20, 'f');
tabela.Rows.Add("Leticia", 25, 'f');
tabela.Rows.Add("Pedro",   30, 'm');
tabela.Rows.Add("Tiago",   40, 'm');
tabela.Rows.Add("Joao",    29, 'm');

// Selecionar linhas onde o campo idade seja maior ou igual a 29
DataRow[] resultados = tabela.Select("Idade >= 29");

// No caso do AP ele usou this.Controls["PageHeader"].AllControls<XRLabel>()
var labels = this.Controls.OfType<XRLabel>(); 

foreach (var tupla in labels.Zip(resultados, Tuple.Create)){
    XRLabel label = tupla.Item1 as XRLabel;
    string valor = tupla.Item2.Field<String>(0); // Valor do campo "Nome"
    label.Text = valor;
}
  • I need to do in all but for each one I need to put a different value. Mine Select returns a Datatable containing a specific value for each label.

  • Thank you very much.

  • That way I believe it also works, but the answer above is better suited to what I need. Thank you very much for your attention.

  • @Daniellourusso Dispo. I edited the answer and put a code that should fit your scenario, I hope it helps. =)

  • Perfect. But when testing here, in my case I used so: this.Controls["PageHeader"].Controls.OfType<XRLabel>();. I got the following error: 'DevExpress.XtraReports.UI.XRControlCollection' does not contain a definition for 'OfType' and no extension method 'OfType' accepting a first argument of type 'DevExpress.XtraReports.UI.XRControlCollection' could be found (are you missing a using directive or an assembly reference?) How do I do?

  • 1

    I found it, just use this.Controls["PageHeader"].AllControls<XRLabel>()

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.