10 Replies - 1167 Views - Last Post: 21 April 2009 - 10:42 AM Rate Topic: -----

#1 blovesamson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-April 09

GUI Listbox Control Help

Posted 21 April 2009 - 08:53 AM

This is probably a very simple question for most programmers, but I'm creating a GUI in Visual Studios C# which will look like this in the end.

Posted Image


In a previous assignment, we created a Fraction class so we are using that for this assignment. The listbox inbetween the two fractions contains four operators (+,-,*,/). The problem I am having is when the Calculate button is clicked, my code will not work with the IF statements I have. I think it is the way I'm checking the IF statement, because I don't quite understand how to get or use the value of the selected item in the listbox. If you need to see the code for the Fraction class, I can post that also. Thank you.

private void buttonCalc_Click(object sender, EventArgs e)
		{
			int Num1, Den1, Num2, Den2;		

			Num1 = Convert.ToInt32(tbNum1.Text);
			Den1 = Convert.ToInt32(tbDen1.Text);
			Num2 = Convert.ToInt32(tbNum2.Text);
			Den2 = Convert.ToInt32(tbDen2.Text);

			Fraction Fract1 = new Fraction(Num1, Den1);
			Fraction Fract2 = new Fraction(Num2, Den2);
			Fraction Fract3;

			string curItem = lbOPERA.SelectedItem.ToString();

			if (curItem == "+")
			{
				Fract3 = Fract1 + Fract2;
			}
			if (curItem == "-")
			{
				Fract3 = Fract1 - Fract2;
			}
			if (curItem == "*")
			{
				Fract3 = Fract1 * Fract2;
			}
			if (curItem == "/")
			{
				Fract3 = Fract1 / Fract2;
			}

			tbNum3.Text = Convert.ToString(Fract3.Numerator);
			tbDen3.Text = Convert.ToString(Fract3.Denominator);

		}


Is This A Good Question/Topic? 0
  • +

Replies To: GUI Listbox Control Help

#2 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: GUI Listbox Control Help

Posted 21 April 2009 - 09:01 AM

Maybe you could use SelectedIndex?

For example:

int curItem = lbOPERA.SelectedIndex;

            if (curItem == 0)
            {
                Fract3 = Fract1 + Fract2;
            }
            if (curItem == 1)
            {
                Fract3 = Fract1 - Fract2;
            }
            if (curItem == 2)
            {
                Fract3 = Fract1 * Fract2;
            }
            if (curItem == 3)
            {
                Fract3 = Fract1 / Fract2;
            }



Each item you have in the box has an index that starts at 0. It's better to use the SelectedIndex property because you might use a capital letter and forget. With numbers it's kind of hard to get them mixed up.

Of course if you want to stick to the SelectedItem, I would probably check what is written literally in the Items.

Maybe your If's don't work because you have written something different in the box.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#3 blovesamson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-April 09

Re: GUI Listbox Control Help

Posted 21 April 2009 - 09:10 AM

Well it's weird. I used your advice, which is better, thank you, but when I hit the calculate button, nothing appears in the end result boxes in the form. It's as if the IF statements aren't even being executed. So what I did was (just to see if it was the IF statement code) was to get rid of it and replace it with something much simpler to see if the rest of my code was working. So I replaced all 4 IF statements with....

Fract3 = Fract1 + Fract2;
				tbNum3.Text = Convert.ToString(Fract3.Numerator);
				tbDen3.Text = Convert.ToString(Fract3.Denominator);


This worked, but obviously not what I want, so it has to be the IF statements. As soon as I add an IF statement to this program, it no longer calculates.
Was This Post Helpful? 0
  • +
  • -

#4 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: GUI Listbox Control Help

Posted 21 April 2009 - 09:14 AM

Do you mind posting your Fraction class? I'll build it myself and let you know what's wrong. It's kind of hard to visualize this without me actually programming it. :P
I'm kind of new too.

Edit: You could use a switch case. Here's how:

int curItem = lbOPERA.SelectedIndex;

switch (curItem)
{
case 0:
Fract3 = Fract1 + Fract2;
break;

case 1:
Fract3 = Fract1 - Fract2;
break;

case 2:
Fract3 = Fract1 * Fract2;
break;

case 3:
Fract3 = Fract1 / Fract2;
break;

default:
break;
}            



Try it now to see if it works. Just replace all the if's with this code.

This post has been edited by papuccino1: 21 April 2009 - 09:17 AM

Was This Post Helpful? 0
  • +
  • -

#5 blovesamson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-April 09

Re: GUI Listbox Control Help

Posted 21 April 2009 - 09:26 AM

class Fraction
	{
		private int num;
		private int denom;

		public Fraction(int x, int y)
		{
			Numerator = x;
			Denominator = y;
		}

		public Fraction(Fraction m)
		{
			Numerator = m.Numerator;
			Denominator = m.Denominator;
		}

		public int Numerator
		{
			set
			{
				num = value;
			}
			get
			{
				return num;
			}
		}

		public int Denominator
		{
			set
			{
				if (value == 0)
					denom = 1;
				else
					denom = value;
			}
			get
			{
				return denom;
			}
		}

		public int this[int x]
		{
			set
			{
				if (x == 0)
					num = value;
				else
					if (x == 1)
						denom = value;
			}
			get
			{
				if (x == 0)
					return num;
				else
					if (x == 1)
						return denom;
					else
						return 1;
			}
		}

		public override string ToString()
		{
			return Numerator + " / " + Denominator;
		}

		public static Fraction operator+(Fraction a, Fraction b)
		{
			int n1 = (a.Numerator * b.Denominator) + (b.Numerator * a.Denominator);
			int d1 = (a.Denominator * b.Denominator);

			return new Fraction(n1, d1);
		}

		public static Fraction operator-(Fraction a, Fraction b)
		{
			int n1 = (a.Numerator * b.Denominator) - (b.Numerator * a.Denominator);
			int d1 = (a.Denominator * b.Denominator);

			return new Fraction(n1, d1);
		}

		public static Fraction operator*(Fraction a, Fraction b) 
		{
			int n1 = (a.Numerator * b.Numerator);
			int d1 = (a.Denominator * b.Denominator);

			return new Fraction(n1, d1);
		}

		public static Fraction operator/(Fraction a, Fraction b)
		{
			int n1 = (a.Numerator * b.Denominator);
			int d1 = (a.Denominator * b.Numerator);

			return new Fraction(n1, d1);
		}
	 } 


Thank you I appreciate your help. I'm new to this too but I'm a quick learner. I know it's just something stupid. Here is my Form code as of right now and the only error I am getting is "Unassigned use of local variable Fract3". The error is at the end of the Form code. I will comment in the code where I am getting the error.

public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void tbNum1_TextChanged(object sender, EventArgs e)
		{

		}

		private void tbDen1_TextChanged(object sender, EventArgs e)
		{

		}

		private void tbNum2_TextChanged(object sender, EventArgs e)
		{

		}

		private void tbDen2_TextChanged(object sender, EventArgs e)
		{

		}

		private void tbNum3_TextChanged(object sender, EventArgs e)
		{

		}

		private void tbDen3_TextChanged(object sender, EventArgs e)
		{

		}


		private void lbOPERA_SelectedIndexChanged(object sender, EventArgs e)
		{

		}

		private void buttonreset_Click(object sender, EventArgs e)
		{
			tbNum1.Text = "";
			tbDen1.Text = "";
			tbNum2.Text = "";
			tbDen2.Text = "";
			tbNum3.Text = "";
			tbDen3.Text = "";
		}

		private void buttonCalc_Click(object sender, EventArgs e)
		{
			int Num1, Den1, Num2, Den2;		

			Num1 = Convert.ToInt32(tbNum1.Text);
			Den1 = Convert.ToInt32(tbDen1.Text);
			Num2 = Convert.ToInt32(tbNum2.Text);
			Den2 = Convert.ToInt32(tbDen2.Text);

			Fraction Fract1 = new Fraction(Num1, Den1);
			Fraction Fract2 = new Fraction(Num2, Den2);
			Fraction Fract3;

			int curItem = lbOPERA.SelectedIndex;

			if (curItem == 0)
			{
				Fract3 = Fract1 + Fract2;
			}
			if (curItem == 1)
			{
				Fract3 = Fract1 - Fract2;
			}
			if (curItem == 2)
			{
				Fract3 = Fract1 * Fract2;
			}
			if (curItem == 3)
			{
				Fract3 = Fract1 / Fract2;
			}

			tbNum3.Text = Convert.ToString(Fract3.Numerator);//ERROR "Use of unassigned local variable 'Fract3'"
			tbDen3.Text = Convert.ToString(Fract3.Denominator);

		}

	}


I think the real problem is not the IF statements or Switch, but with Fract3. If I just create it and don't declare it, I get the error stated above. If I do declare it when I make it, for example...

Fraction Fract3 = new Fraction(1,1);


Then when I hit the calculate button, the end result is always 1/1. I can't get the value for Fract3 to change due to the IF statements or Switch. I hope that makes sense.
Was This Post Helpful? 0
  • +
  • -

#6 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: GUI Listbox Control Help

Posted 21 April 2009 - 09:26 AM

Ah! I now know exactly where your problem is.

You are using Fract3 in the end correct? The compiler hates to use blank variables ;)

What happens if none of the if statements turn out to be true? Fract3 will still be used in the calculation at the end, and your program will commit suicide. :D

To fix your problem simply initialize the Fract3 variable with any number. I commented the line I modified.

private void buttonCalc_Click(object sender, EventArgs e)
        {
            int Num1, Den1, Num2, Den2;        

            Num1 = Convert.ToInt32(tbNum1.Text);
            Den1 = Convert.ToInt32(tbDen1.Text);
            Num2 = Convert.ToInt32(tbNum2.Text);
            Den2 = Convert.ToInt32(tbDen2.Text);

            Fraction Fract1 = new Fraction(Num1, Den1);
            Fraction Fract2 = new Fraction(Num2, Den2);
            Fraction Fract3 = new Fraction(1,1); //<------Initialize this with any number. 

            int curItem = lbOPERA.SelectedIndex;

            if (curItem == 0)
            {
                Fract3 = Fract1 + Fract2;
            }
            if (curItem == 1)
            {
                Fract3 = Fract1 - Fract2;
            }
            if (curItem == 2)
            {
                Fract3 = Fract1 * Fract2;
            }
            if (curItem == 3)
            {
                Fract3 = Fract1 / Fract2;
            }

            tbNum3.Text = Convert.ToString(Fract3.Numerator);
            tbDen3.Text = Convert.ToString(Fract3.Denominator);

        }


This post has been edited by papuccino1: 21 April 2009 - 09:28 AM

Was This Post Helpful? 0
  • +
  • -

#7 blovesamson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-April 09

Re: GUI Listbox Control Help

Posted 21 April 2009 - 09:29 AM

View Postpapuccino1, on 21 Apr, 2009 - 08:26 AM, said:

Ah! I now know exactly where your problem is.

You are using Fract3 in the end correct? The compiler hates to use blank variables ;)

What happens if none of the if statements turn out to be true? Fract3 will still be used in the calculation at the end, and your program will commit suicide. :D

To fix your problem simply initialize the Fract3 variable with any number.



That is what I just edited my post above with. The problem is when I do initialize Fract3 with values, they don't change with the Switch or IF statements, the end result is always the default values I declared. So when I add 3/4 and 4/5 the answer is 1/1, which is not right. It doesn't matter what I put in the first two fractions, 1/1 is always the answer.
Was This Post Helpful? 0
  • +
  • -

#8 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: GUI Listbox Control Help

Posted 21 April 2009 - 09:33 AM

Maybe you shouldn't be using a ListBox as your operator selection control. Try using a ComboBox. Maybe then it works.

If it doesn't work with that, then I'm baffled. Let me know.

This post has been edited by papuccino1: 21 April 2009 - 09:33 AM

Was This Post Helpful? 0
  • +
  • -

#9 blovesamson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-April 09

Re: GUI Listbox Control Help

Posted 21 April 2009 - 09:40 AM

Yeah I have to use a Listbox per the assignment, but I appreciate your help. I can't think of why it's not assigning the new value to Fract3. I've done tons of programs like this and there shouldn't be any error that I can see.
Was This Post Helpful? 0
  • +
  • -

#10 blovesamson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-April 09

Re: GUI Listbox Control Help

Posted 21 April 2009 - 10:26 AM

Papuccino, I got it to work. It wasn't even a problem with the code other than not initializing Fract3, it was that when I ran the program, I was not actually "clicking" on the operator sign in the listbox to select it so the switch never came into play. I realized when I saw that the listbox was on a ONE selection mode and I clicked the operator sign while running the program and it works perfect. Thank you for all your help. :D
Was This Post Helpful? 0
  • +
  • -

#11 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: GUI Listbox Control Help

Posted 21 April 2009 - 10:42 AM

Hahahahaha that's such a silly mistake xD

Well glad to hear you found out what was wrong.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1