Listboxes and Automatic Updating

should be my last two questions

Page 1 of 1

9 Replies - 872 Views - Last Post: 15 August 2009 - 08:31 AM Rate Topic: -----

#1 Demonstrasys  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 14-August 09

Listboxes and Automatic Updating

Posted 14 August 2009 - 08:08 PM

Alright, I have a couple more questions about the program I was asking about earlier.

First of all, I would like to know how to make a textbox contain a formula that updates automatically when new numbers are placed in other boxes. Like this:

+--------------------+.........+------------------------+..........+---------------------------------------------------------------------------------------------+
|This is the first #|....+....|This is the second #|....=...|This # should update automatically to be the other two boxes added together|
+--------------------+.........+------------------------+..........+---------------------------------------------------------------------------------------------+
User Input.......................User Input..............................The program calculates this one.

I know how to do this by making a button trigger the third box being updated, but I would like it to update automatically.

Ignore the periods in my little drawing, spaces like to be deleted on this thing.


And secondly, I've been reading some tutorials on listboxes, and I'm trying to make something else work here:

+--------------------+....+-------------------------------+
|listitem1..............|....|displays the listitemname|
|listitem2..............|....+-------------------------------+
|listitem3..............|....+----------------------------------+
|...........................|....|displays info on the listitem|
+--------------------+....+----------------------------------+
This is the listbox...........These are both textboxes

I want the two boxes on the right to fill out information depending on which item is currently selected in the list on the left. If it is easier to add a "show info" button on the bottom that updates the right two boxes I'm ok with that.

Am I going to have to make an array to store the values in for this instance? I get a little confused while messing with arrays apparently, I needed a lot of help for the last one I screwed up, thankfully a couple of you guys helped me out there.

If anyone can point me in the direction of a good tutorial or give me an example on how to get started on either of these two things I would appreciate it greatly. And hopefully my little drawings helped to clarify what I am trying to accomplish, I figured it was worth a shot.

Is This A Good Question/Topic? 0
  • +

Replies To: Listboxes and Automatic Updating

#2 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Listboxes and Automatic Updating

Posted 14 August 2009 - 08:57 PM

For the textbox that you are looking at you can use the TextChanged event of the two textboxes. If you change the text in a textbox, the TextChanged event handler will be triggered.

For the Listbox idea the listbox has an event called SelectedIndexChanged that will be triggered when you change the selected item in the listbox. There are a number of ways you can set the items in a listbox. If at design time you know the items needed then you can click the elpsise (...) and you will get a dialog box that you can set the strings for the listbox, 1 per line. You can add them in your code, in the Form1_Load event handler:

        private void Form1_Load(object sender, EventArgs e)
        {
            listBox1.Items.Add("String 1");
            listBox1.Items.Add("String 2");
        }



You are not limited to just strings using the Items.Add method. You can use any object. Hope that answers your questions. :)
Was This Post Helpful? 0
  • +
  • -

#3 Demonstrasys  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 14-August 09

Re: Listboxes and Automatic Updating

Posted 14 August 2009 - 09:19 PM

As for the textbox, that does work, and I feel stupid for not trying that in the first place, but I still have a little problem with it. The numbers they input are in string format right? So I cannot do any math with them like that. I try and convert them into doubles (that right?) but it gives me the "cannot implicitly convert type 'double' to 'string'" I have a lot of these boxes to fill in on my program.

As for the listbox, I already have the options in the listbox coded in, I would like the boxes on the right to display different information depending on with item is currently selected in the list.

Thanks for the reply.
Was This Post Helpful? 0
  • +
  • -

#4 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Listboxes and Automatic Updating

Posted 14 August 2009 - 09:53 PM

Yes, the textbox's text value is a string. You can covert the string to a double using Convert.ToDouble method. I would suggest using something similar to this for converting a string to a double though so your program will not generate an exception if the text in the textbox is not a double:
double result = 0.0;

double.TryParse(textbox1.Text, out result);



To convert the double to a string you can use:

textBox3.Text = result.ToString();



For the listboxes, in the selected index changed event handler you can do something like this(assuming that there are strings of doubles in the listboxes and you want to add them together and place them in a textbox):

		private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
		{
			double result1;
			double result2;
			double finalresult;

			result1 = double.Parse((string)listBox1.SelectedItem);
			result2 = double.Parse((string)listBox2.SelectedItem);

			finalresult = result1 + result2;
			textBox1.Text = finalresult.ToString();
		}


Was This Post Helpful? 0
  • +
  • -

#5 Demonstrasys  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 14-August 09

Re: Listboxes and Automatic Updating

Posted 14 August 2009 - 10:40 PM

Well, as for the textboxes filling out, I currently have:

		private void txtSTRBase_TextChanged(object sender, EventArgs e)
		{
			double STRBase;
			double STRBonus;
			double STRTotal;

			STRBase = Convert.ToDouble(txtSTRBase.Text);
			STRBonus = Convert.ToDouble(txtSTRBonus.Text);

			STRTotal = STRBase + STRBonus;
			
			txtSTRTotal.Text = Convert.ToString(STRTotal);
		}
	}
}



And it is giving me "Input string was not in correct format." whenever I enter a number into the field and crashes.

Edit: If I put the bonus txt in before the base txt it works without crashing, but since I'm going to have to put code similar this into all of the related txt boxes I would have to input the numbers simultaneously... which obviously won't work. So I need a different way of doing this.

Would something like this work?

		private void txtSTRBase_TextChanged(object sender, EventArgs e)
		{
			double STRBase;
			double STRBonus;
			double STRTotal;

			try
			{
				STRBase = Convert.ToDouble(txtSTRBase.Text);
				STRBonus = Convert.ToDouble(txtSTRBonus.Text);

				STRTotal = STRBase + STRBonus;
			}
			catch (Exception)
			{
				txtSTRTotal.Text = ("N/A");
			}
			
			txtSTRTotal.Text = Convert.ToString(STRTotal);
		}
	}
}



the problem is now it's telling me that STRTotal at the end there is an unassigned variable. I guess because it is defined in the try/catch block... is there a way to make it public without moving it around maybe? I'm not entirely sure if this is the best way to do it though.

This post has been edited by Demonstrasys: 14 August 2009 - 11:35 PM

Was This Post Helpful? 0
  • +
  • -

#6 Renagado  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Re: Listboxes and Automatic Updating

Posted 15 August 2009 - 12:14 AM

Using try...catch blocks for user input is always a good idea, think about the scenerio when your user types in "one" for example. And unless you need the variable outside the current method I wouldnt make it a public or class variable. But your problem here is not that. When the code in the try...catch block is interrupted, it skips the next try lines, go to the catch block, and then immediately to the line under that. Which means STRTotal is never calculated, and thus null. Solution is simple:

	   private void txtSTRBase_TextChanged(object sender, EventArgs e)
		{
			double STRBase;
			double STRBonus;
			double STRTotal;

			try
			{
				STRBase = Convert.ToDouble(txtSTRBase.Text);
				STRBonus = Convert.ToDouble(txtSTRBonus.Text);

				STRTotal = STRBase + STRBonus;
				txtSTRTotal.Text = Convert.ToString(STRTotal);
			}
			catch (Exception)
			{
				txtSTRTotal.Text = ("N/A");
			}
			

Was This Post Helpful? 1
  • +
  • -

#7 Demonstrasys  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 14-August 09

Re: Listboxes and Automatic Updating

Posted 15 August 2009 - 12:46 AM

Ah... thanks for that, I'm not sure why it didn't occur to me...

		private void txtSTRBase_TextChanged(object sender, EventArgs e)
		{
			double STRBase;
			double STRBonus;
			double STRTotal;
			double STRMisc01;
			double STRMisc02;
			double STRMod;

			try
			{
				STRBase = Convert.ToDouble(txtSTRBase.Text);
				STRBonus = Convert.ToDouble(txtSTRBonus.Text);
				STRMisc01 = Convert.ToDouble(txtSTRMisc01.Text);
				STRMisc02 = Convert.ToDouble(txtSTRMisc02.Text);

				STRTotal = STRBase + STRBonus + STRMisc01 - STRMisc02;
				STRMod = (STRTotal - 10) / 2;
				
				txtSTRTotal.Text = Convert.ToString(STRTotal);
				txtSTRMod.Text = Convert.ToString(STRMod);
			}
			catch (Exception)
			{
				txtSTRTotal.Text = ("N/A");
			}
		}



Is how it looks now, my very last problem with this is that It can show decimals, I only want it to be able to show whole numbers rounded down, is there a way to format the answer to this, I know I can format it to currency, so I thought there may be one for this as well.

If I get that working, and figure out how to use the listbox, I shouldn't have to bother you guys for a while, I appreciate all the help.

This post has been edited by Demonstrasys: 15 August 2009 - 01:09 AM

Was This Post Helpful? 0
  • +
  • -

#8 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Listboxes and Automatic Updating

Posted 15 August 2009 - 06:54 AM

View PostDemonstrasys, on 15 Aug, 2009 - 01:46 AM, said:

Is how it looks now, my very last problem with this is that It can show decimals, I only want it to be able to show whole numbers rounded down, is there a way to format the answer to this, I know I can format it to currency, so I thought there may be one for this as well.


You can just cast the double to an int, that will just remove the decimal part with out rounding up.

		private void txtSTRBase_TextChanged(object sender, EventArgs e)
		{
			double STRBase;
			double STRBonus;
			double STRTotal;
			double STRMisc01;
			double STRMisc02;
			double STRMod;

			try
			{
				STRBase = Convert.ToDouble(txtSTRBase.Text);
				STRBonus = Convert.ToDouble(txtSTRBonus.Text);
				STRMisc01 = Convert.ToDouble(txtSTRMisc01.Text);
				STRMisc02 = Convert.ToDouble(txtSTRMisc02.Text);

				STRTotal = STRBase + STRBonus + STRMisc01 - STRMisc02;
				STRMod = (STRTotal - 10) / 2;
				
				txtSTRTotal.Text = Convert.ToString((int)STRTotal);
				txtSTRMod.Text = Convert.ToString((int)STRMod);
			}
			catch (Exception)
			{
				txtSTRTotal.Text = ("N/A");
			}
		}



Was This Post Helpful? 0
  • +
  • -

#9 Renagado  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Re: Listboxes and Automatic Updating

Posted 15 August 2009 - 06:56 AM

There are several ways of doing this. Easiest is to convert your double to an int using Convert.ToInt(). This will round the number up or down mathematically. Also you can do a typecast in the way of:
int i_number=(int)d_number;
where d_number is a double(obviously). This will just cut off the decimals, so it always rounds down.

Third way is using string.format, for example:

textBox1.Text = string.Format("{0:F0}", d_number);

This tells it to display a floating point number, showing 0 decimals(F0). String.Format is a very powerful thing, you can also show multiple variables, etc. So it's worthwhile to look into.

Good luck!

Edit: SixOfEleven beat me to it

This post has been edited by Renagado: 15 August 2009 - 07:01 AM

Was This Post Helpful? 0
  • +
  • -

#10 Demonstrasys  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 14-August 09

Re: Listboxes and Automatic Updating

Posted 15 August 2009 - 08:31 AM

Thanks for your guys help once again, I think I've figured out how to do most of the things I wanted to do to accomplish what I set out to do, and I have a lot of work to do now. I've noticed something a little funky with my loading files back into the program, and need to figure out how to fix that, as well as making all of the boxes fill themselves out as they should (tons of them), and starting to build the databases.

Good luck to you both, and talk to you later.

This post has been edited by Demonstrasys: 15 August 2009 - 08:32 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1