Windows程序设计学习资料.docx
《Windows程序设计学习资料.docx》由会员分享,可在线阅读,更多相关《Windows程序设计学习资料.docx(13页珍藏版)》请在冰豆网上搜索。
Windows程序设计学习资料
Windows程序设计
洛阳理工学院实验报告
系别
计算机与信息工程系
班级
B100505
学号
B10050527
姓名
张勋
课程名称
C#面向对象程序设计
实验日期
11.22
实验名称
Windows程序设计
成绩
实验目的:
练习windows窗体的使用,掌握多种控件的属性,学会应用窗体属性和事件以及代码两种方式操作。
实验条件:
微机1台、Visualstudio2008
实验内容与结果:
1.控制TextBox控件,使它只能输入数字。
当按下退格键时,执行退格操作.并且当数据将被清空时,提醒终止删除操作,输入数据。
2.
有两个组合框里分别为专业和课程,当专业选定时,课程组合框里的课程数组应属于该专业,通过增加和删除按钮可以把所选的课程添加或删除到列表框,并且在文本框内显示出列表框内的所有课程的总学分数。
当没有选课程直接点击添加按钮时:
选课程并添加:
从列表框内删除:
当再次选择“大学物理”时:
当更换专业时:
3.比较numericUpDown1.UpButton()和numericUpDown1.Value+=numericUpDown1.Increment;的区别,这里把文本框数值的上限设为20,下限设为0,增值为1.
当达到上限时,numericUpDown1.UpButton()执行正常,而另一句却出现错误,语句无法执行
添加快捷菜单,在窗口菜单中有大中小三个菜单项,分别控制着窗口的大小,单击右键弹出快捷菜单
使快捷菜单的功能与窗口菜单相同。
窗口菜单操作下:
快捷菜单操作下:
附加窗体的代码:
(部分功能在属性中执行了,故在代码中没有设置)
1.usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
namespacep7_4
{
publicpartialclassForm1:
Form
{
publicForm1()
{
InitializeComponent();
}
privatevoidtextBox1_KeyPress(objectsender,KeyPressEventArgse)
{
if(e.KeyChar>=48&&e.KeyChar<=57);
elseif(e.KeyChar==8)
{
textBox1.Text.Remove(textBox1.Text.Length-1);
if(textBox1.Text.Length==1)
MessageBox.Show("请输入数据!
","警告",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
elsee.Handled=true;
}
privatevoidForm1_Load(objectsender,EventArgse)
{
}
}
}
2.usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
namespacep7_5
{
publicpartialclassForm1:
Form
{
privateinttotalHours=0;
publicForm1()
{
InitializeComponent();
}
privatevoidForm1_Load(objectsender,EventArgse)
{
string[]course=newstring[4]{"数学","英语","计算机","电子工程"};
for(inti=0;i<4;i++)
{
comboBox1.Items.Add(course[i]);
}
textBox1.Text="0";
}
privatevoidcomboBox1_TextChanged(objectsender,EventArgse)
{
comboBox2.Items.Clear();
listBox1.Items.Clear();
textBox1.Text="0";
Course[]math=newCourse[4]{newCourse("英语",50),newCourse("高等数学",60),newCourse("数理统计",35),newCourse("计算机应用基础",40)};
Course[]English=newCourse[2]{newCourse("英语",50),newCourse("计算机应用基础",40)};
Course[]Computer=newCourse[5]{newCourse("英语",50),newCourse("高等数学",60),newCourse("大学物理",40),newCourse("计算机应用基础",40),newCourse("计算机语言程序设计",45)};
Course[]Electronic_Engineer=newCourse[5]{newCourse("英语",50),newCourse("高等数学",60),newCourse("大学物理",40),newCourse("计算机应用基础",40),newCourse("电子电工",45)};
strings=comboBox1.Text;
comboBox2.Text="请选择…";
switch(s)
{
case"数学":
for(inti=0;i<4;i++)
{
comboBox2.Items.Add(math[i]);
}
break;
case"英语":
for(inti=0;i<2;i++)
{
comboBox2.Items.Add(English[i]);
}
break;
case"计算机":
for(inti=0;i<5;i++)
{
comboBox2.Items.Add(Computer[i]);
}
break;
case"电子工程":
for(inti=0;i<5;i++)
{
comboBox2.Items.Add(Computer[i]);
}
break;
}
}
voidbutton2_Click(objectsender,EventArgse)
{
if(listBox1.SelectedIndex!
=-1)
{
Coursec1=(Course)listBox1.SelectedItem;
listBox1.Items.Remove(c1);
totalHours-=c1.hours;
textBox1.Text=totalHours.ToString();
}
}
voidbutton1_Click(objectsender,EventArgse)
{
if(comboBox2.SelectedIndex!
=-1)
{
Coursec1=(Course)comboBox2.SelectedItem;
if(!
listBox1.Items.Contains(c1))
{
listBox1.Items.Add(c1);
totalHours+=c1.hours;
textBox1.Text=totalHours.ToString();
}
elseMessageBox.Show("对不起,该课程已被添加,请重选!
","警告",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
elseMessageBox.Show("请选择课程!
","警告",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
publicclassCourse
{
publicstringname;
publicinthours;
publicCourse(stringname,inthours)
{
this.name=name;
this.hours=hours;
}
publicoverridestringToString()
{
returnname;
}
}
}
}
3.usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
namespace_6
{
publicpartialclassForm1:
Form
{
publicForm1()
{
InitializeComponent();
}
privatevoidForm1_Load(objectsender,EventArgse)
{
}
privatevoidnumericUpDown1_MouseClick(objectsender,MouseEventArgse)
{
numericUpDown1.UpButton();
//numericUpDown1.Value+=numericUpDown1.Increment;
}
}
}
4.usingSystem;
usingSystem.Drawing;
usingSystem.Windows.Forms;
namespaceP7_9
{
publicpartialclassForm1:
Form
{
publicForm1()
{
InitializeComponent();
menuItemWindowMiddle.Checked=true;
menuItemWindowBig.Click+=menuItem_Click;
menuItemWindowMiddle.Click+=menuItem_Click;
menuItemWindowSmall.Click+=menuItem_Click;
}
voidmenuItem_Click(objectsender,EventArgse)
{
ToolStripMenuItemitem=(ToolStripMenuItem)sender;
if(item==menuItemWindowBig||item==大ToolStripMenuItem)
{
item.Checked=true;
menuItemWindowMiddle.Checked=menuItemWindowSmall.Checked=false;
中ToolStripMenuItem.Checked=小ToolStripMenuItem.Checked=false;
this.Size=newSize(800,450);
}
elseif(item==menuItemWindowMiddle||item==中ToolStripMenuItem)
{
item.Checked=true;
menuItemWindowBig.Checked=menuItemWindowSmall.Checked=false;
大ToolStripMenuItem.Checked=小ToolStripMenuItem.Checked=false;
this.Size=newSize(480,270);
}
elseif(item==menuItemWindowSmall||item==小ToolStripMenuItem)
{
item.Checked=true;
menuItemWindowBig.Checked=menuItemWindowMiddle.Checked=false;
大ToolStripMenuItem.Checked=中ToolStripMenuItem.Checked=false;
this.Size=newSize(240,135);
}
}
privatevoidForm1_Load(objectsender,EventArgse)
{
}
}
}
实验总结:
通过这次上机操作让我深刻的掌握了窗体的灵活性,使我学会了用属性代替代码的简便设计方法。
另外在这次试验中发现,当控件的事件在事件窗体中添加时,如果在代码中也绑定了,就会出现该事件执行两次的情况。
窗体是我们生活中很常见的,这次的窗体设计大大的提高了我对编程的兴趣。
把课本的理论知识通过实践操作,使我对这些理论更好的理解了。