9 Replies - 872 Views - Last Post: 15 January 2013 - 03:42 PM Rate Topic: -----

#1 Danikashi  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 11-May 12

[WinForms] Concatenation of a Label and a Variable

Posted 15 January 2013 - 02:24 PM

Good afternoon,
I have a question, is it possible to Concatenate a variable to a label?

For example, i'm doing:
Random Rnd = new Random();
      int num1 = Rnd.Next(1, 11);

And now i want to concatenate the random number to give test me if the label of that number has a char inside.
if ( lbl_s_num1.Text != "X")

Is this possible?

Is This A Good Question/Topic? 0
  • +

Replies To: [WinForms] Concatenation of a Label and a Variable

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9204
  • View blog
  • Posts: 34,582
  • Joined: 12-June 08

Re: [WinForms] Concatenation of a Label and a Variable

Posted 15 January 2013 - 02:31 PM

Sure.. just like any other string.
            String foo = "abc";
            Int32 bar = 123;

            foo += bar.ToString();

Was This Post Helpful? 0
  • +
  • -

#3 Danikashi  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 11-May 12

Re: [WinForms] Concatenation of a Label and a Variable

Posted 15 January 2013 - 02:36 PM

View Postmodi123_1, on 15 January 2013 - 02:31 PM, said:

Sure.. just like any other string.
            String foo = "abc";
            Int32 bar = 123;

            foo += bar.ToString();


The problem is that i want to concatenate a label name with an int.
I do know how to do it for a string and a int,
But as i want to do it for a label it gives me "The name 'lbl_s_' does not exist in the current context" since i dont have any label named 'lbl_s_' only one called 'lbl_s_1'.
Hope i've made my problem clear.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9204
  • View blog
  • Posts: 34,582
  • Joined: 12-June 08

Re: [WinForms] Concatenation of a Label and a Variable

Posted 15 January 2013 - 02:45 PM

Okay.. so change the name of the label.

            String foo = "abc";
            int bar = 123;

            label1.Name = foo + bar.ToString();

Was This Post Helpful? 0
  • +
  • -

#5 Danikashi  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 11-May 12

Re: [WinForms] Concatenation of a Label and a Variable

Posted 15 January 2013 - 02:59 PM

View Postmodi123_1, on 15 January 2013 - 02:45 PM, said:

Okay.. so change the name of the label.

            String foo = "abc";
            int bar = 123;

            label1.Name = foo + bar.ToString();

That's not exactly what i want to do though. I want to choose the label that the random gives me and change its text.
            if (comboBox1.Text == "1")
            {
                string concat;
                Random Rnd = new Random();
                int num1 = Rnd.Next(1, 11);
                int num2 = Rnd.Next(1, 11);
                do
                {
                    if (num2 == num1)
                    {
                        num2 = Rnd.Next(1, 11);
                    }
                } while (num2 == num1);
                concat = "lbl_s_" + num1.ToString();
                if (concat.Text != "X")
                {
                    concat.Text = "X";
                }
            }

Something like this. It doesn't let me though.
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9204
  • View blog
  • Posts: 34,582
  • Joined: 12-June 08

Re: [WinForms] Concatenation of a Label and a Variable

Posted 15 January 2013 - 03:02 PM

Quote

I want to choose the label that the random gives me and change its text.

How can you choose something the random object gives you?
Was This Post Helpful? 0
  • +
  • -

#7 Danikashi  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 11-May 12

Re: [WinForms] Concatenation of a Label and a Variable

Posted 15 January 2013 - 03:08 PM

View Postmodi123_1, on 15 January 2013 - 03:02 PM, said:

Quote

I want to choose the label that the random gives me and change its text.

How can you choose something the random object gives you?


For example i have 5 labels. named label1,label2,etc... i get an random number (from 1 to 5) and then i use that number to choose the label. If i get the number 3 i will use the label3 if i get the number 1 i will use the label1 and so on.
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9204
  • View blog
  • Posts: 34,582
  • Joined: 12-June 08

Re: [WinForms] Concatenation of a Label and a Variable

Posted 15 January 2013 - 03:14 PM

Well that was a round about way of explaining things, but in that cause use teh 'controls' collection... you can reference a control by it's name.

            string lblConstant = "label";
            int barbar = 1;
            this.Controls[lblConstant + barbar.ToString()].Name = "poot";

Was This Post Helpful? 2
  • +
  • -

#9 Danikashi  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 11-May 12

Re: [WinForms] Concatenation of a Label and a Variable

Posted 15 January 2013 - 03:24 PM

View Postmodi123_1, on 15 January 2013 - 03:14 PM, said:

Well that was a round about way of explaining things, but in that cause use teh 'controls' collection... you can reference a control by it's name.

            string lblConstant = "label";
            int barbar = 1;
            this.Controls[lblConstant + barbar.ToString()].Name = "poot";


Worked. I'm currently studying C# so i didn't know of the "Controls".
Thank you for your time :)
Was This Post Helpful? 0
  • +
  • -

#10 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: [WinForms] Concatenation of a Label and a Variable

Posted 15 January 2013 - 03:42 PM

You could also put your labels in an array, and use the random number as an index into that array. That way, you avoid some of the fragility that arises from relying on the names of the labels:

private readonly Label[] labels;
public YourForm() {
    InitializeComponent();
    labels = new Label[] { label1, label2, label3, label4, label5 };
}



And then, to select a label at random:

Label randomLabel = this.labels[rng.Next(0, labels.Length)];


where rng is the instance of the Random class.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1