您的位置:首页 > 编程语言 > C#

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);
}
}
}
运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: