C#程序设计基础——学生窗体实验
4.4设计一个表决器,表决器中有6个候选城市(如北京、上海、深圳、广州、武汉、南京),选择数量有4个选项(1、2、3、4),当选择3时,只能选择三个候选城市......(具体如图)
当没有选择数量时,出现提醒
当选择了3,却选择了2个城市时
源代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
int N = 0;
if (radioButton1.Checked) N = 1;
if (radioButton2.Checked) N = 2;
if (radioButton3.Checked) N = 3;
if (radioButton4.Checked) N = 4;
int M = 0;
if (checkBox1.Checked) M++;
if (checkBox2.Checked) M++;
if (checkBox3.Checked) M++;
if (checkBox4.Checked) M++;
if (checkBox5.Checked) M++;
if (checkBox6.Checked) M++;
if (N == 0)
{
MessageBox.Show("你应该先选择城市数量");
}
else if (N != M)
{
MessageBox.Show("你应该选择" + N + "个城市");
}
else
{
string temp = "";
if (checkBox1.Checked) temp += "," + checkBox1.Text;
if (checkBox2.Checked) temp += "," + checkBox2.Text;
if (checkBox3.Checked) temp += "," + checkBox3.Text;
if (checkBox4.Checked) temp += "," + checkBox4.Text;
if (checkBox5.Checked) temp += "," + checkBox5.Text;
if (checkBox6.Checked) temp += "," + checkBox6.Text;
if (temp != "")
temp = temp.Substring(1);
richTextBox1.Text = temp;
}
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
Close();
}
}
}