data input from combobox and radio button to text in new form2 in c# windows form

44 Views Asked by At

This is a first so my apologies for any mistakes or further confusion.

First of all I'm creating a small game on c# windows form where user inputs name, date of birth, and chooses avatar I used textbook-name, combobox-date of birth, and lastly radio buttons as images-for user to control as picture box in my other form2.

I figured out you use string for name input but:

  • I was confused on the data for combobox input choice to text in the bottom right of my other form(should I use string for combobox input?)+name

  • As well as radibutton choice to picture box that user gets to control in form2 since I don't want it as text so I didn't use string.

It would be very helpful for any answers or notes. Thank you and sorry for any confusion.

2

There are 2 best solutions below

0
jmcilhinney On

If the user is entering a date of birth then the obvious data type for the value is DateTime. You might also be able to use DateOnly in newer versions but, even if you do, there's every chance that you'll need to convert to DateTime at some point because one has been around since the inception of .NET and one is very new.

If you're using WinForms to enter a date then the obvious choice of control is DateTimePicker, not ComboBox. The user can then enter the whole date in one place and you get a DateTime value from the Value property.

If you insist on using ComboBoxes then I assume that you have three - one each for day, month and year. If you populate the drop-down lists in the designer then you'll be adding strings, so you will then need to convert the Text or SelectedItem to an int first, then create a DateTime from those three int values.

var year = Convert.ToInt32(yearComboBox.Text);
var month = Convert.ToInt32(monthComboBox.Text);
var day = Convert.ToInt32(dayComboBox.Text);
var dateOfBirth = new DateTime(year, month, day);

If you populate the drop-downs in code instead, you can actually add int values in the first place and there will be no conversion required, but you'd still need to cast the SelectedItem from type object.

var year = (int)yearComboBox.SelectedItem;
var month = (int)monthComboBox.SelectedItem;
var day = (int)dayComboBox.SelectedItem;
var dateOfBirth = new DateTime(year, month, day);

Note that a DateTimePicker will ensure that only a valid date is selected. If you use ComboBoxes, you'll have to ensure that the user doesn't select an invalid day for the selected month, e.g. February 30.

0
goldgalaxy On

/// ==== method 1

// Form1.cs

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 dlg = new Form2(radioButton1.Text, comboBox1.Text);
        dlg.Show();
    }

// Form2.cs

public partial class Form2 : Form
{
    public Form2(string msg1, string msg2)
    {
        InitializeComponent();

        label1.Text = msg1 ;
        label2.Text = msg2 ;
    }
}

/// ==== method 2

// Form1.cs

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 dlg = new Form2();
        dlg.pMsg1 = radioButton1.Text;
        dlg.pMsg2 = comboBox1.Text;
        dlg.Show();
    }

// Form2.cs

public partial class Form2 : Form
{
    public string pMsg1, pMsg2 ;
    public Form2()
    {
        InitializeComponent();
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        label1.Text = pMsg1;
        label2.Text = pMsg2;
    }
}