Hi guys,

Sorry the title is vague. Couldn't come up with one.

Ok so I have a int variable which is, let's say, of the value 5.

Now I want to create a string which contains underscores, so: "_____" the amount of underscores is the same value of the integer. So if the int variable is of value 5 then the string should have 5 underscores, like this: "_____"

Any idea?

Something lke that?

private void method()
        {
            const int value = 5;
            string tag = "_";
            string item = "";
            for (int i = 0; i < value; i++)
                item += tag;
            MessageBox.Show("You have chosen " + value.ToString() + " underscores.\nThe result is: \"" + item + "\"");
        }

Mitja

I think i've done it. I did this:

word = myArray[random.Next(0,myArray.Length)];
            length = word.Length;

            Label lblWord = new Label();

            lblWord.AutoSize = true;
            lblWord.Location = new System.Drawing.Point(24, 91);
            lblWord.Name = "lblWord";
            lblWord.Size = new System.Drawing.Size(27, 13);
            lblWord.TabIndex = 1;
            lblWord.Text = "";

            for (int i = 0; i == length; i++)
            {
                lblWord.Text = lblWord.Text + "_";
            }

Where the string is actually a label and i'm drawing the label on to the form.

CODING IS FUN :D (Hi Mitja!)

Hi again. I added some stuff:

private void Form1_Load(object sender, EventArgs e)
        {
            lblOne.Visible = false;
            lblTwo.Visible = false;
            lblThree.Visible = false;
            lblFour.Visible = false;

            word = myArray[random.Next(0,myArray.Length)];
            length = word.Length;

            Label lblWord = new Label();

            lblWord.AutoSize = true;
            lblWord.Location = new System.Drawing.Point(24, 91);
            lblWord.Name = "lblWord";
            lblWord.Size = new System.Drawing.Size(27, 13);
            lblWord.TabIndex = 1;
            lblWord.Text = "";

            for (int i = 0; i == length; i++)
            {
                lblWord.Text = lblWord.Text + "_";
            }

            Controls.Add(lblWord);
            lblWord.Visible = true;


        }

But I still don't see the label appearing on to the form. What am I doing wrong. (Brb Family dinner).

Sure you cannot. This will not do:

for (int i = 0; i == length; i++)
 {
      lblWord.Text = lblWord.Text + "_";
 }

You have to do the loop which will go through it:

for (int i = 0; i < length; i++)
 {
      lblWord.Text += "_";  //this will do it too :)
 }

Mitja

Still....When I press F5 and run the program, the label doesn't appear.....

Nope - the length is definitely working. I did a quick test and made a new GUI to make sure the length and the array etc are working. It does:

private void Form1_Load(object sender, EventArgs e)
        {
            word = myArray[random.Next(0,myArray.Length)];
            length = word.Length;

            label1.Text = word.ToString();
            textBox1.Text = length.ToString();
        }

Works perfectly. The label1 and textbox1 on the GUI work as they should...So why isn't my initial program working? AM i missing something?

OMG IT WORKS! W000H00!!!!! I forgot to change thr for loop -_-' STUPID ME! :

for (int i = 0; i <= length; i++)
            {
                lblWord.Text += "_ ";
            }

Excellent. Thanks!

String s = string.Empty.PadLeft(6, '-');

Replace the six with your variable. No loops required.

Edited 5 Years Ago by Momerath: n/a

This question has already been answered. Start a new discussion instead.