Home > Net >  Newbie questions: c # Winform, change the selected radio button option, the display InvalidArgument=
Newbie questions: c # Winform, change the selected radio button option, the display InvalidArgument=

Time:10-07




From the point of spending to income, the pop-up error above
 
Private void rdoExpenditure_CheckedChanged (object sender, EventArgs e)
{
CboCatogory. Items. The Clear ();
If (rdoExpenditure. Checked==true)
{
CboCatogory. Items. The Add (" cost of living ");
CboCatogory. Items. The Add (" fixed assets ");
CboCatogory. Items. The Add (" entertainment ");
}
The else
{
CboCatogory. Items. The Add (" work income ");
CboCatogory. Items. The Add (" investment ");
CboCatogory. Items. The Add (" other income ");
}
CboCatogory. SelectedIndex=0;
}


CboCatogory category of the combo box is payments, specific Item is determined by the balance of payments types, code like the book, don't know why will go wrong, the great god, please give directions, thank you.

CodePudding user response:

 
If (rdoExpenditure. Checked==true)
{
CboCatogory. Items. The Add (" cost of living ");
CboCatogory. Items. The Add (" fixed assets ");
CboCatogory. Items. The Add (" entertainment ");
}
The else
{
CboCatogory. Items. The Add (" work income ");
CboCatogory. Items. The Add (" investment ");
CboCatogory. Items. The Add (" other income ");
}
CboCatogory. SelectedIndex=0;

Try this way

CodePudding user response:

You check whether there is SelectedIndexChange cboCatogory events, cboCatogory. Items. The Clear () the trigger SelectedIndexChange events, while SelectedIndexChange event. If you have the code to use cboCatogory Items [0] will throw an exception (for all Items Clear off, no item index of 0).

CodePudding user response:

refer to the second floor datafansbj response:
you check whether there is SelectedIndexChange cboCatogory events, cboCatogory. Items. The Clear () the trigger SelectedIndexChange events, while SelectedIndexChange event. If you have the code to use cboCatogory Items [0] will throw an exception (Clear off all the Items because it is over, no index 0 Items),

CboCatogory is selectedIndexChange event, but it was useless to the Items [0], still don't understand why go wrong, the code is as follows:
 
Private void cboCatogory_SelectedIndexChanged (object sender, EventArgs e)
{

If (cboCatogory. Items. Count & gt; 0)
{
ListBox1. Items. The Clear ();
The switch (cboCatogory. Items. The ToString ())
{
Case "cost of living" :
ListBox1. Items. The Add (" eat ");
ListBox1. Items. The Add (" drink ");
break;
Case "work income" :
ListBox1. Items. The Add (" normal wage ");
ListBox1. Items. The Add (" bonus ");
break;
}
ListBox1. SelectedIndex=0;
}
}

Private void listBox1_SelectedIndexChanged (object sender, EventArgs e)
{

}

CodePudding user response:

reference 1st floor ManBOyyy response:
 
If (rdoExpenditure. Checked==true)
{
CboCatogory. Items. The Add (" cost of living ");
CboCatogory. Items. The Add (" fixed assets ");
CboCatogory. Items. The Add (" entertainment ");
}
The else
{
CboCatogory. Items. The Add (" work income ");
CboCatogory. Items. The Add (" investment ");
CboCatogory. Items. The Add (" other income ");
}
CboCatogory. SelectedIndex=0;

Try this way

Still won't do

CodePudding user response:

Project selected debug mode, in the debug mode want him to look on the error screenshot again, may not be what you said the location of the error

CodePudding user response:

reference 5 floor HZTLTGG reply:
project selected debug mode, in the debug mode want him to look on the error screenshot again, may not be what you said the location of the error


It is the debug mode

CodePudding user response:

You failed in this is compiled, running is compiled code before?

CodePudding user response:

 
ListBox1. Items. The Clear ();
//the problem here other than the selected item
//but a collection of all the options directly ToString and could not enter into the switch
//to SelectedItem can
//comboBox1. Items. The ToString ()
The switch (comboBox1. SelectedItem. ToString ())
{
Case "cost of living" :
ListBox1. Items. The Add (" eat ");
ListBox1. Items. The Add (" drink ");
break;
Case "work income" :
ListBox1. Items. The Add (" normal wage ");
ListBox1. Items. The Add (" bonus ");
break;
}
ListBox1. SelectedIndex=0;

CodePudding user response:

refer to 7th floor HZTLTGG response:
you failed in this is compiled, running a compiled code before?


Excuse me so?

CodePudding user response:

Yes, listbox is wrong, the reason above has said

CodePudding user response:

reference f * * KCSDN eighth floor response:
 
ListBox1. Items. The Clear ();
//the problem here other than the selected item
//but a collection of all the options directly ToString and could not enter into the switch
//to SelectedItem can
//comboBox1. Items. The ToString ()
The switch (comboBox1. SelectedItem. ToString ())
{
Case "cost of living" :
ListBox1. Items. The Add (" eat ");
ListBox1. Items. The Add (" drink ");
break;
Case "work income" :
ListBox1. Items. The Add (" normal wage ");
ListBox1. Items. The Add (" bonus ");
break;
}
ListBox1. SelectedIndex=0;

Yes! I didn't notice, thank you!

CodePudding user response:

Where in the opinion of the switch inside the case didn't into you clear the first set. Then go to listBox1 SelectedIndex=0; It is only natural that the errornullnullnullnull
  •  Tags:  
  • C#
  • Related