c#练习代码集锦Word下载.docx
《c#练习代码集锦Word下载.docx》由会员分享,可在线阅读,更多相关《c#练习代码集锦Word下载.docx(17页珍藏版)》请在冰豆网上搜索。
![c#练习代码集锦Word下载.docx](https://file1.bdocx.com/fileroot1/2022-11/18/6fa9ed36-2eed-4324-9a85-7637071e3aa9/6fa9ed36-2eed-4324-9a85-7637071e3aa91.gif)
if(checkBox1.Checked)ah+=checkBox1.Text;
if(checkBox2.Checked)ah+="
"
+checkBox2.Text;
if(checkBox3.Checked)ah+="
+checkBox3.Text;
str="
你的姓名是:
+name+"
\r\n"
+"
你的性别是:
+sex+"
你的爱好是:
+ah;
}
2.ListBox
privatevoidbutton2_Click(objectsender,EventArgse)
{
boolcanAdd=true;
stringminfo="
if(textBox1.Text=="
)
canAdd=false;
minfo="
添加项不能为空!
}
intz=listBox1.FindStringExact(textBox1.Text);
if(z!
=-1)canAdd=false;
列表中已经存在“"
+textBox1.Text+"
”,无法完成添加操作!
if(canAdd)//没有相同项时,执行下面的操作
listBox1.Items.Add(textBox1.Text);
else//否则提示信息
DialogResultdlogRs=MessageBox.Show(minfo,"
系统提示"
MessageBoxButtons.OK,MessageBoxIcon.Information);
if(dlogRs==DialogResult.OK)
textBox1.Focus();
textBox1.Select(0,textBox1.Text.Length);
privatevoidbutton1_Click(objectsender,EventArgse)
if(listBox2.Items.Count>
-1)
listBox2.Items.Clear();
button1.Enabled=false;
privatevoidlistBox1_DoubleClick(objectsender,EventArgse)
if(listBox1.SelectedIndex!
=-1)
listBox2.Items.Add(listBox1.SelectedItem);
listBox1.Items.Remove(listBox1.SelectedItem);
if(button1.Enabled==false)button1.Enabled=true;
3.Math类
if(comboBox2.SelectedIndex!
=-1||comboBox2.Text!
="
doublech=double.Parse(comboBox2.Text);
doubleresult=0;
switch(comboBox1.SelectedIndex)
case0:
result=Math.Sin(ch*Math.PI/180);
break;
//x*Math.PI/180转换成角度
case1:
result=Math.Cos(ch*Math.PI/180);
case2:
result=Math.Sqrt(ch);
label3.Text="
结果="
+result.ToString("
0.00"
);
else
stringminfo="
请选择或输入一个值!
MessageBox.Show(minfo,"
privatevoidForm1_Load(objectsender,EventArgse)
comboBox1.SelectedIndex=0;
4.PictureBox、RadioButton、ScrollBar
privatevoidForm1_Load(objectsender,EventArgse)
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;
privatevoidradioButton2_CheckedChanged(objectsender,EventArgse)
if(radioButton1.Checked)pictureBox1.Load(@"
elsepictureBox1.Load(@"
图片2.jpg"
privatevoidradioButton1_CheckedChanged(objectsender,EventArgse)
{//注意此处也可radioButton2共用一段代码,这里进行了改写
if(radioButton2.Checked)pictureBox1.Load(@"
elsepictureBox1.Load(@"
}
privatevoidhScrollBar1_Scroll(objectsender,ScrollEventArgse)
pictureBox1.Width=hScrollBar1.Value;
5.RichTextBox、FontDialog、ColorDialog、Menu
privatevoid退出UToolStripMenuItem_Click(objectsender,EventArgse)
this.Close();
privatevoid打开ToolStripMenuItem_Click(objectsender,EventArgse)
open=@"
C:
\DocumentsandSettings\Administrator\MyDocuments\"
//设定默认目录
open="
//默认文件名
纯文本(*.txt)|*.txt|RTF文件(*.rtf)|*.rtf"
//文件过滤
open=2;
//设定默认过滤器
if(open()==DialogResult.OK)
toolStripStatusLabel1.Text=open;
if(open==1)
richTextBox1.Load,RichTextBoxStreamType.PlainText);
richTextBox1.Load);
privatevoid保存ToolStripMenuItem_Click(objectsender,EventArgse)
stringfname=open();
save=true;
if(fname!
="
&
&
fname!
open"
)save=fname;
save="
新建文件"
)save=open;
if(save()==DialogResult.OK)
if(save==1)
richTextBox1.Save,RichTextBoxStreamType.PlainText);
richTextBox1.Save);
//将‘是否修改’值重置
richTextBox1.Modified=false;
toolStripComboBox1.SelectedIndex=0;
//字号列表初始化选择第一项
toolStripStatusLabel2.Text=DateTime.Now.ToLocalTime().ToString();
//2011-11-1916:
08:
07
toolStripStatusLabel1.Text="
暂未打开任何文件!
privatevoid字体ToolStripMenuItem_Click(objectsender,EventArgse)
FontDialogfdg=newFontDialog();
fdg.ShowDialog();
richTextBox1.SelectionFont=fdg.Font;
privatevoid颜色ToolStripMenuItem_Click(objectsender,EventArgse)
ColorDialogcdg=newColorDialog();
cdg.ShowDialog();
richTextBox1.SelectionColor=cdg.Color;
privatevoidtimer1_Tick(objectsender,EventArgse)
{
toolStripStatus