Home > other >  Combobox Winforms
Combobox Winforms

Time:12-29

Currently I'm building a membership Form , Im attempting to use a combobox for the payment type selection (direct debit or credit card). Basically what I'm trying to do is when the user selects the option from the drop down , below that will generate the fields that the user can fill in IE credit card number or direct debit account info. Anyway I can do this without displaying both? Im new so hopefully there is a non complicated way, perhaps I can have some boxes that are hidden until the option is selected?

CodePudding user response:

1: Create two panels

2: The first one you call DirectDebit_Option and the second one CreditCard_Option or whatever else you want to call them

3 Set the visibility of both panels to false

4: Add all needed controls for your payment method into the panel

5: Add the SelectedIndexChanged-Event to your Combobox which selects these two payment options

6: In the SelectedIndexChanged-Event you can now do something like this:

DirectDebit_Option.Visible = comboBox1.SelectedIndex == 0; 
CreditCard_Option.Visible = comboBox1.SelectedIndex == 1;

CodePudding user response:

You Can Create two UserControl for direct debit And credit card and in the selected_Item event of your ComboBox when user select direct debit you will call your Direct_debit_UserControl in any created Panel, and do the same for credit card selection like this :

private comboBox1_SelectedItem(object sender, EventArgs e)
{
  panel1.Controls.Clear();
  
  if (comboBox1.SelectedItem == "direct debit")
  {
    panel1.Controls.Add(new direct_debit_UserControl(){DockType = DockType.Fill} );
  }
  else
  {
    panel1.Controls.Add(new credit_card_UserControl(){DockType = DockType.Fill} );
  }
}

for add a new UserControl from Visual Studio go to Solution Explorer > Right click on your project > Add > New Item... > In the Box Dialog Select User Control

  • Related