12 Replies - 1400 Views - Last Post: 10 April 2015 - 01:36 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 online

  • Suitor #2
  • member icon



Reputation: 10232
  • View blog
  • Posts: 39,365
  • 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 online

  • Suitor #2
  • member icon



Reputation: 10232
  • View blog
  • Posts: 39,365
  • 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 online

  • Suitor #2
  • member icon



Reputation: 10232
  • View blog
  • Posts: 39,365
  • 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 online

  • Suitor #2
  • member icon



Reputation: 10232
  • View blog
  • Posts: 39,365
  • 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? 3
  • +
  • -

#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: 998
  • Posts: 972
  • 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
  • +
  • -

#11 Osjim  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 10-April 15

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

Posted 10 April 2015 - 12:03 PM

Hi, how can i do this but the label is in other form.
this.controls["Form2"+lblconstant].text , doest work...

Thank you

This post has been edited by andrewsw: 10 April 2015 - 02:11 PM
Reason for edit:: Removed previous quotes, just use the REPLY button

Was This Post Helpful? 0
  • +
  • -

#12 rgfirefly24  Icon User is online

  • D.I.C Lover
  • member icon


Reputation: 351
  • View blog
  • Posts: 1,766
  • Joined: 07-April 08

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

Posted 10 April 2015 - 01:26 PM

that's because this refers to the form that you are currently on. If you want to reference another form you need to give some ability for form1 to pass the data to form2. There are several tutorials on how to do it, but I would suggest creating a property on form2 that populates the labels, then in form1 you can do this:


public partial class form1 : Form
{
    private Form2 form2 { get; set;}

    protected void GreetingBtn_Click(Object s, EventArgs e)
    {
        DoGreeting();
        //otherstuff;

    }

    private void DoGreeting()
    {
        form2.UserName = UserName;

        form2.Show();

    }

}


This post has been edited by rgfirefly24: 10 April 2015 - 01:31 PM

Was This Post Helpful? 0
  • +
  • -

#13 andrewsw  Icon User is offline

  • I'm not here to twist your niblets
  • member icon

Reputation: 4276
  • View blog
  • Posts: 15,682
  • Joined: 12-December 12

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

Posted 10 April 2015 - 01:36 PM

Just to note that this is a necro-post.

@Osjim If you're question hasn't been resolved then you should start your own topic, not attach it to the bottom of someone else's question.

rgfirefly24 I think that you should note in your sample that form2 needs to be instantiated ;)

This post has been edited by andrewsw: 10 April 2015 - 01:38 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1