简体   繁体   English

如何在C#中使用多个单选按钮?

[英]How to use the multiple radio buttons in C#?

How can I use multiple radio buttons and save to my database access the select item text radio button he save only the gender when I try only from name to gender.when i try query insert into (lname, mname, fname, course, major, minor, gender) he save the data in my database access when I try the year with another radio buttons insert into (lname, mname, fname, course, major, minor, gender, year) he error to the insert into but he save the text when I leave the text in the string year. 当我尝试从名称到性别尝试输入(lname,mname,fname,course,major,major,次要,性别),当我尝试将年份插入另一个单选按钮(lname,mname,fname,当然,专业,次要,未成年人,性别,年份)时,他将数据保存在我的数据库访问权限中。当我在字符串年份中保留文本时显示文本。

Design of my form: 我的表格设计:

在此处输入图片说明

public partial class student : UserControl
{
    OleDbConnection con = new OleDbConnection(@"Provider = 
    Microsoft.ACE.OLEDB.12.0; Data Source 
    =C:\Users\ronaldcaymo\Desktop\Student Information System.accdb");
    string Gender;
    string year;
    string Status;

    private void button4_Click(object sender, EventArgs e)
    {
        con.Open();
        OleDbCommand cmd = con.CreateCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "insert into 
        table1(Lname,Fname,Mname,Course,Major,Minor,semester,Gender,Status) 
        values('"+tblname.Text+"','"+tbfname.Text+"','"+tbmname.Text+ 
        "','"+cbcourse.Text+"','" +cbmajor.Text 
  +"','"+cbminor.Text+"','"+cbsemester.Text+"','"+Gender+"','"+Status+"')";
        cmd.ExecuteNonQuery();
        con.Close();
        MessageBox.Show("Student Added");
    }    
    private void radioButton5_CheckedChanged(object sender, EventArgs e)
    {
        Gender = "Male";
    }

    private void radioButton6_CheckedChanged(object sender, EventArgs e)
    {
        Gender ="Female";
    }

    private void radioButton1_CheckedChanged(object sender, EventArgs e)
    {
        year = "1st";
    }

    private void radioButton2_CheckedChanged(object sender, EventArgs e)
    {
        year ="2nd";
    }

    private void radioButton3_CheckedChanged(object sender, EventArgs e)
    {
        year ="3rd";
    }

    private void radioButton4_CheckedChanged(object sender, EventArgs e)
    {
        year ="4th";
    }

    private void radioButton8_CheckedChanged(object sender, EventArgs e)
    {
        Status = "Single";
    }

    private void radioButton7_CheckedChanged(object sender, EventArgs e)
    {
        Status = "Married";
    }  

}

The CheckedChanged event of your radio buttons will trigger everytime your radio button's status is changed, that means when it's checked or unchecked. 单选按钮的CheckedChanged事件将在每次更改单选按钮的状态时触发,即选中或取消选中状态。

So when '1st year' is checked and you check another radio button such as '3rd year', '1st year' will become unchecked and thus trigger the event radioButton1_CheckedChanged at the same time with radioButton3_CheckedChanged . 因此, radioButton1_CheckedChanged中“第一年”并选中另一个单选按钮(例如“第三年”)时,“第一年”将变为未选中状态,从而与radioButton3_CheckedChanged同时触发事件radioButton3_CheckedChanged

To fix this issue, add a condition check inside all your radioButton_CheckedChanged events as such: 要解决此问题,请在所有radioButton_CheckedChanged事件中添加条件检查,例如:

private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
    if(radioButton3.Checked){
        year ="3rd";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM