Skip to content

For loop

jmal salih

Asked May 14, 2014, 12:14 AM EDT1 answer
Share
LinkedInX
this code work only on number from 1 to 10 repeated 3 times for example
with other numbers do nothing here is my code:
C#
 private void button1_Click(object sender, EventArgs e)
        {

          if (textBox1.Text==""||textBox1.Text=="")
          {
              MessageBox.Show("enter the numbers");
          }
          else
          {
              string file = @"c:\\test.txt";

           try
              {
    // Check if file already exists. If yes, delete it.
                if (File.Exists(file))
                    {
                      File.Delete(file);
                  }
              int  x    = Int32.Parse(textBox1.Text);
              int  y    = Int32.Parse(textBox2.Text);
              int count = Int32.Parse(textBox3.Text);
              System.IO.StreamWriter sw = new System.IO.StreamWriter(file);
            for (int i = x; i <= count; i++)

                for (int j = x; j <= y; j++)
                {

                    sw.WriteLine(i);

                  }
            sw.Close();
                  }
              catch (Exception Ex)
            {
                  MessageBox.Show(Ex.ToString());
                }
        }
    }

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    I solved it like that: int x = Int32.Parse(textBox1.Text); int y = Int32.Parse(textBox2.Text); int repeat = Int32.Parse(textBox3.Text); int z = y - x; System.IO.StreamWriter sw = new System.IO.StreamWriter(file); for (int i = 1; i <= z+1; i++) { for (int j = 1; j <= repeat; j++) { sw.WriteLine(x); } x ++; thank you friends
    0