C#—Windows应用基础2
2016-05-21 15:02
537 查看
/* * 窗体上有一个列表框(ListBox1)、一个文本框(textBox1)5个命令按钮。 * (1)在文本框里输入课程名称,单击“添加”按钮,如果列表框里没有此选项,则添加,否则不添加; * (2)单击“删除”按钮,则删除在列表框里选中的项目; * (3)单击“修改”按钮,则将在列表框里选中的项目显示在文本框里,已备修改; * (4)单击“修改确定”按钮,则将文本框修改后的内容替换原来选中的项目; * (5)单击“退出”按钮,则关闭窗体。 */ using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { listBox1.Items.Add("C#程序设计"); listBox1.Items.Add("网络技术"); listBox1.Items.Add("大学英语"); listBox1.Items.Add("大学物理"); } private void button1_Click(object sender, EventArgs e) //“添加”按钮 { if (!this.listBox1.Items.Contains(textBox1.Text)) { listBox1.Items.Add(textBox1.Text); } else { MessageBox.Show("已经存在"); } } private void button2_Click(object sender, EventArgs e) //“删除”按钮 { listBox1.Items.Remove(listBox1.SelectedItem); //三个语句效果相同 //listBox1.Items.RemoveAt(listBox1.SelectedIndex); //listBox1.Items.Remove(listBox1.Text); } private void button3_Click(object sender, EventArgs e) //“修改”按钮 { textBox1.Text = listBox1.Text; } private void button4_Click(object sender, EventArgs e) //“修改确定”按钮 { listBox1.Items.Insert(listBox1.SelectedIndex, textBox1.Text); listBox1.Items.Remove(listBox1.SelectedItem); } private void button5_Click(object sender, EventArgs e) //“退出”按钮 { Application.Exit(); } } }
运行结果:
/* * 当某项被选中时会出现两种情况:若该项复选框已被选中,则会显示标签;否则,就是选中某项,该项也不会在左侧标签上显示。 */ using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { string[] s = { "语文", "数学", "外语", "政治", "历史", "地理" }; checkedListBox1.Items.AddRange(s); checkedListBox1.SelectedIndex = 0; } private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e) { label1.Text = checkedListBox1.GetItemCheckState(checkedListBox1.SelectedIndex) == CheckState.Checked ? checkedListBox1.Text : string.Empty; //获得复选列表框中选择项的状态 CheckState.Checked—选中 } } }
运行结果:
/* * 点击domainUpDown1右边上下箭头,可以循环选取最大、较大、中、较小、最小各项值。 * 点击numericUpDown1右边上下箭头,可以在4~72之间循环取值。 */ using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication3 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { string[] s = { "最大", "较大", "中", "较小", "最小" }; domainUpDown1.Items.AddRange(s); domainUpDown1.Wrap = true; domainUpDown1.SelectedIndex = 0; numericUpDown1.Maximum = 72; numericUpDown1.Minimum = 4; } private void numericUpDown1_ValueChanged(object sender, EventArgs e) { label1.Text = numericUpDown1.Value.ToString(); } private void domainUpDown1_SelectedItemChanged(object sender, EventArgs e) { label1.Text = domainUpDown1.Text; } } }
运行结果:
/*
* 利用水平滚动条调整字体大小。在窗体上放置一个HscrollBar控件,用其控制标签字体的大小,同时在文本框显示字号。
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = "C#程序设计";
hScrollBar1.Minimum = 4;
hScrollBar1.Maximum = 72;
hScrollBar1.Value = 10;
hScrollBar1.SmallChange = 1;
hScrollBar1.LargeChange = 5;
}
private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
textBox1.Text = hScrollBar1.Value.ToString();
label1.Font = new Font(label1.Font.Name, hScrollBar1.Value);
}
}
}
运行结果:
相关文章推荐
- C#中系统时间和UNIX时间戳互相转换
- C#操作XML
- C# 委托的三种调用示例(同步调用 异步调用 异步回调)
- c#使用easyhook库进行API钩取
- 有关C#标签Attribute的熟悉
- 20个C#热点问题
- C#中组件与控件的主要区别是什么
- c#根据当前系统时间生成的随机序列数
- 数组,C#
- c#窗体控件datagriview与dataset的运用:修改,新增,删除,查找,保存,更新
- IF-ELSE IF-ELSE语句
- C# 常忘知识
- C# 字符串string和内存流MemoryStream及比特数组byte[]之间相互转换
- C#下汉字转换成拼音
- C#迭代器
- 简单实现DevExpress换肤
- C#理解Thread.Sleep方法(2)
- C#中Dock属性的作用
- 快速理解C#中的委托与事件
- C#管理员权限调用cmd以及以管理员身份生成程序