3 Replies - 2488 Views - Last Post: 16 November 2009 - 09:22 AM Rate Topic: -----

#1 systemerror   User is offline

  • D.I.C Head

Reputation: -19
  • View blog
  • Posts: 205
  • Joined: 15-August 09

C# If Statement

Post icon  Posted 16 November 2009 - 03:12 AM

This is my code

		private void Browse_Click(object sender, EventArgs e)
		{
			if (label2.Text == "0/20")
	   {
			if (openFileDialog1.ShowDialog() == DialogResult.OK)
			{
				textBox1.Text = openFileDialog1.FileName;

				if (label2.Text == "0/20")
				{
					label2.Text = "1/20";
				}
				else
				{
				   
				}
			}
		}

			if (label2.Text == "1/20")
			{
				if (openFileDialog1.ShowDialog() == DialogResult.OK)
				{
					textBox2.Text = openFileDialog1.FileName;

					if (label2.Text == "1/20")
					{
						label2.Text = "2/20";
					}
					else
					{

					}
				}
			}

			if (label2.Text == "2/20")
			{
				if (openFileDialog1.ShowDialog() == DialogResult.OK)
				{
					textBox1.Text = openFileDialog1.FileName;

					if (label2.Text == "2/20")
					{
						label2.Text = "3/20";
					}
					else
					{

					}
				}
			}

			if (label2.Text == "3/20")
			{
				if (openFileDialog1.ShowDialog() == DialogResult.OK)
				{
					textBox1.Text = openFileDialog1.FileName;

					if (label2.Text == "3/20")
					{
						label2.Text = "4/20";
					}
					else
					{

					}
				}
			}

			if (label2.Text == "4/20")
			{
				if (openFileDialog1.ShowDialog() == DialogResult.OK)
				{
					textBox1.Text = openFileDialog1.FileName;

					if (label2.Text == "4/20")
					{
						label2.Text = "5/20";
					}
					else
					{

					}
				}
			}

			if (label2.Text == "5/20")
			{
				if (openFileDialog1.ShowDialog() == DialogResult.OK)
				{
					textBox1.Text = openFileDialog1.FileName;

					if (label2.Text == "5/20")
					{
						label2.Text = "6/20";
					}
					else
					{

					}
				}
			}


There is a problem, I can't make the openfiledialog to stop opening up, I want it so that you click it once it opens for that textbox, click 2nd it opens for that 2nd text box and so on, can any help?

Is This A Good Question/Topic? 0
  • +

Replies To: C# If Statement

#2 Adkins   User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: C# If Statement

Posted 16 November 2009 - 04:03 AM

The problem is that you are updateing your test value as the test is still going. I.E. it starts off with 0/20 you then change it to 1/20, then you test if it is equal to 1/20 and continue with this trend. It doesn't break out of this cycle till the end of the method.
Was This Post Helpful? 0
  • +
  • -

#3 Crehl   User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 49
  • Joined: 25-January 09

Re: C# If Statement

Posted 16 November 2009 - 04:19 AM

I don't quite understand what you're asking, but I'd like to point out 3 things:

1) You don't need those empty else statements (Unless you plan on using them at some later point), so you can remove them. Helps keep the code looking clean and easier to read.

2) You also don't need those inner if statements at all, as what you essentially have is:

			if (label2.Text == "1/20")
			{

				//Snip..

					if (label2.Text == "1/20")
					{
						label2.Text = "2/20";
					}


That second if is always going to be true (Since otherwise, the first wouldn't have been).

Thus, each if statement should look something like this:

			if (label2.Text == "1/20")
			{
				if (openFileDialog1.ShowDialog() == DialogResult.OK)
				{
					textBox2.Text = openFileDialog1.FileName;

					label2.Text = "2/20";
				}
			}



Already, it's starting to look simpler.

3) Your if statements are cascading (Don't believe that's an 'official' term for what's hapenning, but the best I know of). Let's have a look at what's hapening:

-You check if the label text is equal to "0/20"
-If so, open the dialog and then set the label text to "1/20"
-Check if the label test is equal to "1/20"
-Open dialog, etc etc

Each if statement changes the text such that the next if statement will always return true - opening up all the dialog boxes as a result (From what I can tell, I think this is the problem you described).

All that's needed is to merge all those if statements into an If/Elseif/ElseIf/.. and things should work as intended.

However, I wouldn't recommend that. A switch/case would be simpler, but with 21 possibilities (I assume you go up to 20/20?), I still wouldn't advise it.

Instead, use a for loop. It would also make sense to use a variable to check against rather than label text. It's better to use a variable to keep track of things because it stores only what is needed and so is easier to read/write. For example:

			if (curIteration == 1)
			{
				if (openFileDialog1.ShowDialog() == DialogResult.OK)
				{
					textBox2.Text = openFileDialog1.FileName;

					curIteration++;
					label2.Text = curIteration.ToString() + "/20";
				}
			}



A bit of a long post, but hopefully it should help with your problem and clean up the code a little,

-Crehl

[Edit] Looks like someone beat me to it, much more concisely :)
Was This Post Helpful? 0
  • +
  • -

#4 b.ihde   User is offline

  • D.I.C for life
  • member icon

Reputation: 45
  • View blog
  • Posts: 1,070
  • Joined: 29-September 08

Re: C# If Statement

Posted 16 November 2009 - 09:22 AM

Edit your if-statements like this:

 if (label2.Text == "1/20")
			{

				//code
			}
  else if (label2.Text == "1/20")
			{
					  // code
			}
  else if (label2.Text == "2/20) .....



Greetz

Ben


Edit* you could also use switch-case statements here.. :)

This post has been edited by b.ihde: 16 November 2009 - 09:23 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1