private void button1_Click(object sender, EventArgs e)
        {
            n = Convert.ToInt32(textBox1.Text);
            for (; i < n; i = i + 2)
            {
                textBox2.Text = textBox2.Text + " " + Convert.ToString(i); ;
            }

        }

I an mentioning that i,n=0; is located in the declarations zone of InitializeComponent();
I get the error : FormatExeption was unhandled.Make sure your methog arguments are in the right format.

Recommended Answers

All 10 Replies

In a for loop you have to use a local variable that you create in the first part of the loop. you cannot use a variable created somewhere else.

if you want to use vars created somewhere else use a while loop.

while(i < n)
{
   textBox2.Text += " " + Convert.ToString(i);
    
   i++;
}

Thank you so much but I still have a problem with n. Error saying that n is never used. And I still get the error about this line, saying "make sure that your method arguments are in the right format": int n = Convert.ToInt32(textBox1.Text);

namespace Test
{
    public partial class Form1 : Form
    {
     public Form1()
     {
            InitializeComponent();
     }   
       private void button1_Click(object sender, EventArgs e)
     {
         int n = Convert.ToInt32(textBox1.Text);
           while (i<n)
            {               
               textBox2.Text += " " + Convert.ToString(i);
               i=i+2;
            }
        }
    
  }
}

I'm not sure why you are getting a warning about 'n' since it is assigned to then used in a comparison. But you should be getting an error about 'i' since it is never declared so it doesnt exist in the context that it is used.
The error about your argument formatting is likely a result of trying to convert a non-numeric value to an integer. Try using

bool success = int.TryParse(textBox1.Text, out n);

instead. You can use the success variable to check wether valid input was received.

Ok, just re-read your original post. You are declaring 'i' and 'n' in the InitializeComponents method, is that correct?
Firstly, what were you tying to achieve? Variables declared inside a method only exist within the context of the method. This is why you are getting the warning about variables not being used. The 'n' you create in the button click event is used, the 'n' you create in the InitializeComponents method is not used. If you want to create 'i' and 'n' so that they can be accessed by all your methods then make them private class members.
Secondly, why would you want your counters to be class members? There is generally no need to edit the contents of the class.designer.cs file. The InitializeComponent method block has a comment at the top advising you NOT to edit it. The code there is auto generated by the designer, it is not a place to declare your variables.

Please explain what you were trying to achieve so that we can offer better advise on the best approach to take.

ALSO...as mentioned above. Please us code tags to post code: [code] code goes here [/code]

Thank you so much for the prompt answer. I saw that the InitializeComponent method block has a comment not to edit it but I just folowed Microsoft tutorial's steps to create a form which would dispaly all even numbers smaller than a given value "n". I started a new project and now is working. I think I messed my controls properties.
I 've kept the int i=0,n; in the Form1.Designer.cs code as:

private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.TextBox textBox2;
        int i = 0, n;

Thank you again.
Daniela

Ther eis a microsoft tutorial that told you to put that there? Can you post a link to it?
Are the values of i or n used inside the InitializeComponents method??

File no longer exists :/

I don't know , will try again. Yes, i and n are declared in the Intialization block. May I attached the document here?

it would be helpful if you just post your project. we can find out what is really going on.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.