天津市大学软件园2011-2012学年
C#程序设计复习集锦
1.RadioButton、 CheckBox
关键代码:
private void button1_Click(object sender, EventArgs e)
{
string str,name,sex,ah="";
name = textBox1.Text;
if (radioButton1.Checked) sex = radioButton1.Text;
else
if (radioButton2.Checked) sex = radioButton2.Text;
else sex = "未知";
if (checkBox1.Checked) ah += checkBox1.Text;
if (checkBox2.Checked) ah += " " + checkBox2.Text;
if (checkBox3.Checked) ah += " " + checkBox3.Text;
str = "你的姓名是:" + name + "\r\n" + "你的性别是:" + sex + "\r\n" + "你的爱好是:" + ah;
}
2.ListBoxcheckbox和radiobutton的区别
private void button2_Click(object sender, EventArgs e)
{
bool canAdd = true;
string minfo="";
if (textBox1.Text == "")
{
canAdd = false;
minfo = "添加项不能为空!";
}
else
{
int z= listBox1.FindStringExact(textBox1.Text);
if (z != -1) canAdd = false;
minfo = "列表中已经存在“" + textBox1.Text + "”,无法完成添加操作!";
}
if (canAdd)//没有相同项时,执行下面的操作
{
listBox1.Items.Add(textBox1.Text);
}
else//否则提示信息
{
DialogResult dlogRs= MessageBox.Show(minfo, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
if (dlogRs == DialogResult.OK)
{
textBox1.Focus();
textBox1.Select(0, textBox1.Text.Length);
}
}
}
private void button1_Click(object sender, EventArgs e)
{
if (listBox2.Items.Count > -1)
{
listBox2.Items.Clear();
button1.Enabled = false;
}
}
private void listBox1_DoubleClick(object sender, EventArgs e)
{
if (listBox1.SelectedIndex != -1)
{
listBox2.Items.Add(listBox1.SelectedItem);
listBox1.Items.Remove(listBox1.SelectedItem);
if (button1.Enabled == false) button1.Enabled = true;
}
}
3.Math类
private void button1_Click(object sender, EventArgs e)
{
if (comboBox2.SelectedIndex!=-1||comboBox2.Text!="")
{
double ch = double.Parse(comboBox2.Text);
double result=0;
switch (comboBox1.SelectedIndex)
{
case 0: result = Math.Sin(ch * Math.PI / 180); break;//x* Math.PI / 180 转换成角度
case 1: result = Math.Cos(ch * Math.PI / 180); break;
case 2: result = Math.Sqrt(ch); break;
}
label3.Text = "结果=" + result.ToString("0.00");
}
else
{
string minfo = "请选择或输入一个值!";
MessageBox.Show(minfo, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.SelectedIndex = 0;
}
4.PictureBox、RadioButton、ScrollBar
private void Form1_Load(object sender, EventArgs e)
{
radioButton1.Checked=true;
pictureBox1.Load(@"图片1.jpg");//注意:在调试时需要将图片复制到Debug目录下
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Width = 200; hScrollBar1.Minimum = 200; hScrollBar1.Maximum = 300; hScrollBar1.Value = 200;
hScrollBar1.SmallChange = 2; hScrollBar1.LargeChange = 5;
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
if (radioButton1.Checked) pictureBox1.Load(@"图片1.jpg");
else pictureBox1.Load(@"图片2.jpg");
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{ //注意 此处也可radioButton2 共用一段代码,这里进行了改写
if (radioButton2.Checked) pictureBox1.Load(@"图片2.jpg");
else pictureBox1.Load(@"图片1.jpg");
}
private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
pictureBox1.Width = hScrollBar1.Value;
}
5.RichTextBox、FontDialog、ColorDialog 、Menu
private void 退出UToolStripMenuItem_Click(object sender, EventArgs e)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论