Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

How To Pass Combobox Value From One From To The Textbox To Another Form

combobox

  • Please log in to reply
2 replies to this topic

#1 percis

percis

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 72 posts
  • Programming Language:C
  • Learning:C#, (Visual) Basic

Posted 18 July 2012 - 09:40 PM

private void cbb_SystemVoltage_SelectedIndexChanged(object sender, EventArgs e)
	  {
		 Double DailyEnergyDemand;
		 DailyEnergyDemand = Convert.ToDouble(txt_DailyEnergyDemand.Text);
		 if (DailyEnergyDemand <= 1000)
		 {
		    cbb_SystemVoltage.Items.Add(12);
		 }
		 else if (DailyEnergyDemand <= 4000)
		 {
		    cbb_SystemVoltage.Items.Add(24);
		 }
		 else if (DailyEnergyDemand > 5000)
		 {
		    cbb_SystemVoltage.Items.Add(48);
		    cbb_SystemVoltage.Items.Add(96);
		 }
		 double dblAmphrs;
		 int intAmphrs;
		 if (txt_DailyEnergyDemand.Text != "" && cbb_SystemVoltage.Text != "")
		 {
		    dblAmphrs = Convert.ToDouble(txt_DailyEnergyDemand.Text) / Convert.ToDouble(cbb_SystemVoltage.Text);
		    intAmphrs = Convert.ToInt32(dblAmphrs);
		    txt_AhDemand.Text = String.Format("{0:0.00}", Convert.ToString(intAmphrs));
		 }
	  }

private void button1_Click(object sender, EventArgs e)
	  {
		 frmBatterySelection form = new frmBatterySelection();
		 form.txt_DischargeRate.Text = String.Format("{0:0.##}", Convert.ToDouble(txt_DischargeRate.Text));
		 form.txt_Amp.Text = String.Format("{0:0.##}", Convert.ToDouble(txt_BatteryCapacity.Text));
		 //form.cbb_SystemVoltage.SelectedItem.Text = String.Format("{0:0.##}", Convert.ToDouble(textBox1.Text));
		 form.Show();
	  }

how pass cbb_SystemVoltage to the textBox1 of the frmBatterySelection (one from to the another).
code written in comment is wrong.
how write the code?
  • 0

#2 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 19 July 2012 - 03:53 AM

In event handler cbb_SystemVoltage_SelectedIndexChanged, you are adding items in a combobox in the 'SelectedIndexChanged' event of that combobox which may cause again fire that event (in turns stackoverflow due to recursive call).

However, I'm a bit confuse that your post title is opposite of the code you are showing.

If you just want to put the currently selected item in a combobox (cbb_SystemVoltage) to a textbox (textBox1), you can do it as
if (cbb_SystemVoltage.SelectedItem != null) textBox1.Text = cbb_SystemVoltage.SelectedItem.ToString();

Or, do you want to do oppsite (select a text from a textbox as current item in a combobox)?; If yes, do it as
cbb_SystemVoltage.SelectedItem = textBox1.Text;
. Note that, in this case, the item from textbox should be an item in the combobox, otherwise you may get exception.
  • 1

#3 percis

percis

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 72 posts
  • Programming Language:C
  • Learning:C#, (Visual) Basic

Posted 19 July 2012 - 04:25 AM

if (cbb_SystemVoltage.SelectedItem != null) textBox1.Text = cbb_SystemVoltage.SelectedItem.ToString();

this code solved my qn.

Thanks a lot!!!!
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download