using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _1_PictureBox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
label3.Visible = false;
label4.Visible = false;
label5.Visible = false;
label6.Visible = false;
label7.Visible = false;
}
private void radioNomal_CheckedChanged(object sender, EventArgs e)
{
pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
label3.Visible = true;
label4.Visible = false;
label5.Visible = false;
label6.Visible = false;
label7.Visible = false;
}
private void radioStretchImage_CheckedChanged(object sender, EventArgs e)
{
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
label3.Visible = false;
label4.Visible = true;
label5.Visible = false;
label6.Visible = false;
label7.Visible = false;
}
private void radioAutoSize_CheckedChanged(object sender, EventArgs e)
{
pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
label3.Visible = false;
label4.Visible = false;
label5.Visible = true;
label6.Visible = false;
label7.Visible = false;
}
private void radioCenterImage_CheckedChanged(object sender, EventArgs e)
{
pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
label3.Visible = false;
label4.Visible = false;
label5.Visible = false;
label6.Visible = true;
label7.Visible = false;
}
private void radioBZoom_CheckedChanged(object sender, EventArgs e)
{
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
label3.Visible = false;
label4.Visible = false ;
label5.Visible = false;
label6.Visible = false;
label7.Visible = true ;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _1_PictureBox
{
public partial class showPicture : Form
{
public showPicture()
{
InitializeComponent();
}
public int i = 0; //图片索引参数
public string[] path = Directory.GetFiles(@"C:\Users\Admin\Desktop\Images\"); //Directory需要引入命名空间using System.IO
private void 图片查看器_Load(object sender, EventArgs e)
{
//设定图片如何在PictureBox中显示
pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
//设定PicturBox默认显示图片
pictureBox1.Image = Image.FromFile(path[i]);
//获取指定文件夹下所有文件的全路径
//下面循环仅仅是验证获取的路径都是什么文件路径,与此示例功能无关
//foreach (string onePicturePath in path)
//{
// MessageBox.Show(onePicturePath); //是路径下所有的文件,不仅仅是图片文件
// //Console.WriteLine(onePicturePath); //不能空控制台输出
//}
}
private void button1_Click(object sender, EventArgs e)
{
i--;
if (i < 0) i = path.Length - 1; //翻到第一张后此时i变为负数,将i重新赋值为“总长度-1”即最后一张索引值,从而达到训话浏览
pictureBox1.Image = Image.FromFile(path[i]);
}
private void button2_Click(object sender, EventArgs e)
{
i++;
if (i > path.Length - 1) i = 0;//i大于总长度减一,即翻到最后一张后又点击了一次下一张,此时翻到第一张
pictureBox1.Image = Image.FromFile(path[i]);
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _3_ImageList控件
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button3_Click_1(object sender, EventArgs e)
{
//声明一个图像变量
Image image1 = Image.FromFile(@"C:\Users\Admin\Desktop\Images\1.png");
//imageList控件添加元素的方法
//方法1,添加单个元素
imageList1.Images.Add(image1);
//创建了一个窗口句柄(Handle) H wnd(windows)
Graphics mygph = Graphics.FromHwnd(this.Handle);
//Draw有三种方法
//第一种 (graphics对象,左上角点坐标,索引值)
imageList1.Draw(mygph, new Point(50, 50), 0); //第一个参数是句柄,第二个参数是图片显示起始位置(图片左上角的位置,用于定位图片),第三个参数是图片索引值,即ImageList中的图片索引
//第二种:(graphics对象,水平坐标,垂直坐标,索引值)
//第三种:(graphics对象,水平坐标,垂直坐标,图像宽度,图像高度,索引值)
}
private void button1_Click(object sender, EventArgs e)
{
//imageList控件添加元素的方法
//方法2 一次添加多个元素
Image image2 = Image.FromFile(@"C:\Users\Admin\Desktop\Images\2.png");
Image image3 = Image.FromFile(@"C:\Users\Admin\Desktop\Images\3.png");
Image image4 = Image.FromFile(@"C:\Users\Admin\Desktop\Images\4.png");
Image[] images = { image2, image3, image4 };
imageList1.Images.AddRange(images);
//方法3 AddStrip
Image image5 = Image.FromFile(@"C:\Users\Admin\Desktop\Images\5.png");
Image image6 = Image.FromFile(@"C:\Users\Admin\Desktop\Images\6.png");
imageList1.Images.AddStrip(image5);
imageList1.Images.AddStrip(image6);
//创建了一个窗口句柄(Handle) H wnd(windows)
Graphics mygph = Graphics.FromHwnd(this.Handle);
for (int i = 0; i < imageList1.Images.Count; i++)
{
imageList1.Draw(mygph, new Point(50, 50), i);
//因为显示速度太快,给线程添加一个休眠时间;
System.Threading.Thread.Sleep(1000);
}
}
private void button2_Click(object sender, EventArgs e)
{
//删除imageList对象的方法
//removeAt(索引值)
imageList1.Images.RemoveAt(5);
imageList1.Images.RemoveAt(4);
imageList1.Images.RemoveAt(3);
}
}
}
using System;
using System.Windows.Forms;
namespace _5_ListView
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
String[,] myCountry = { { "China", "中国", "北京" }, { "Korea", "韩国", "首尔" }, { "Canada", "加拿大", "渥太华" }, { "American", "美国", "纽约" }, { "Jpan", "日本", "东京" }, { "SingPore", "新加坡", "新加坡" }, { "UK", "英国", "伦敦" }, { "Vietnam", "越南", "河内" }, };
for (int i=0; i < myCountry.Length/3; i++) //length获取数组的长度,是指最小元素的数量。例如上面数组共24个元素。
{
ListViewItem mylvi = listView1.Items.Add(i.ToString()); //可以理解为添加了一个对象名称,或者行名称
mylvi.ImageIndex = i;
mylvi.SubItems.Add(myCountry[i,0]);
mylvi.SubItems.Add(myCountry[i,1]);
mylvi.SubItems.Add(myCountry[i,2]);
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedItem.ToString() == "LargeIcon") listView1.View = View.LargeIcon;
if (comboBox1.SelectedItem.ToString() == "SmallIcon") listView1.View = View.SmallIcon;
if (comboBox1.SelectedItem.ToString() == "Details") listView1.View = View.Details;
if (comboBox1.SelectedItem.ToString() == "List") listView1.View = View.List;
if (comboBox1.SelectedItem.ToString() == "Tile") listView1.View = View.Tile;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TreeView
{
public partial class Form1 : Form
{
public string[] nodeName = new string[100];
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
TreeNode mytn = new TreeNode(); //实例化一个树节点对象
if (textBox1.Text.Trim() != "")
{
mytn.Text = textBox1.Text.Trim(); //给节点对象赋值
treeView1.Nodes.Add(mytn); //将节点添加到TreeView控件中
}
else
{
MessageBox.Show("请先添加节点的名称");
}
}
private void button2_Click(object sender, EventArgs e)
{
TreeNode mytn = new TreeNode(); //实例化一个树节点对象
if (textBox1.Text.Trim() != "")
{
mytn.Text = textBox1.Text.Trim(); //给节点对象赋值
treeView1.SelectedNode.Nodes.Add(mytn);
}
else
{
MessageBox.Show("请先添加节点的名称");
}
}
private void button3_Click(object sender, EventArgs e)
{
TreeNode mytn = new TreeNode();
mytn = treeView1.SelectedNode;
if (mytn.Nodes.Count > 0)
{
DialogResult myDR = MessageBox.Show("该节点下有子节点不允许删除,请先删除子节点");
}
else
{
mytn.Remove();
}
}
private void button4_Click(object sender, EventArgs e)
{
TreeNode mytn = new TreeNode();
mytn = treeView1.SelectedNode;
if (textBox1.Text.Trim() != "")
{
if(((System.Collections.IList)nodeName).Contains(textBox1.Text) != true) mytn.Text = textBox1.Text.Trim();
else MessageBox.Show("已经存在\""+textBox1.Text+"\"的节点不能重复添加","错误提示");
}
else
{
MessageBox.Show("请先添加节点的名称");
}
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
//TreeNode mytn = new TreeNode();
//mytn = treeView1.SelectedNode;
//MessageBox.Show(mytn.Text);
}
private void Form1_Load(object sender, EventArgs e)
{
int i = 0;
foreach (TreeNode mynode in treeView1.Nodes)
{
nodeName[i] = mynode.Text;
i++;
}
}
}
}
using System;
using System.Windows.Forms;
namespace _8_DateTimePicker控件
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
string tg = "甲乙丙丁午己庚辛壬癸";
string dz = "子丑寅卯辰巳午未申申酉戌亥";
string sx = "鼠牛虎兔龙蛇马羊猴鸡狗猪";
int year = dateTimePicker1.Value.Year;
label1.Text = "您选择的年为:农历" + tg[(year - 4) % 10] + dz[(year - 4) % 12] + "年,生肖是" + sx[(year - 4) % 12] + "年";
}
}
}
MonthCalendar控件是用来查看和设置日期信息。它是一个可视化网络图形界面,网格内包含月份的编号日期,这些日期排列在周一~周日下方的七个列表中。用户可以单击月份标题任何一侧的箭头按钮来选择不同的月份。该控件还有一个更强的功能。就是可以选择日期范围。
以粗体显示特定日期
MonthCalendar控件以粗体显示特殊的日期或重要出现的日子,用此控件显示特殊日期的属性有三个:
- BoldedDates属性包含单个日期,(仅设置单个日期以粗体显示,上/下一个月以及上一年/下一年的日期都不影响)
- AnnuallyBoldeDates属性包含每年以粗体显示的日期;(设置每年的同一日期粗体显示)
- MonthlyBoleedDates属性包含每月以粗体显示的日期;(设置每月的同一日期粗体显示)
- 这三个属性中的每一个都包含有一个DateTime对象数组。若要从这些列表中添加或移除日期,必须添加或移除DateTime对象。
显示多个月份
- MonthCalendar控件可显示多个月份,最多可同时显示十二个月。默认情况下,控件只显示一个月,用户可通过CalendarDimensions属性来设置要显示的月份个数,它有两个属性width属性和height属性,分别用来设置列数和行数。在设置或更改月历尺寸时,控件的大小也会随之改变:因此应确保窗体上有足够的空间共新尺寸用。
选择日期范围
using System;
using System.Drawing;
using System.Windows.Forms;
namespace _12_ErrorProvider控件
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text.Length < 8 || textBox1.Text.Length > 10)
{
errorProvider1.SetError(textBox1, "用户名必须为8~12位");
errorProvider1.Icon = new Icon(@"E:\C#\project\Windows窗体高级控件\ico\error.ico");
}
else
{
errorProvider1.SetError(textBox1, "通过验证");
errorProvider1.Icon = new Icon(@"E:\C#\project\Windows窗体高级控件\ico\true.ico");
}
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
if (textBox3.Text != textBox2.Text)
{
errorProvider2.SetError(textBox3, "两次输入的密码不一致");
errorProvider2.Icon = new Icon(@"E:\C#\project\Windows窗体高级控件\ico\error.ico");
}
else
{
errorProvider2.SetError(textBox3, "通过验证");
errorProvider2.SetError(textBox2, "通过验证");
errorProvider2.Icon = new Icon(@"E:\C#\project\Windows窗体高级控件\ico\true.ico");
}
}
}
}
- 通过调用SetHelpNavigator并提供指定控件的HelpNavigator枚举值来指定提供的帮助类型:
- 通过调用setHelpKeyWord方法为帮助提供关键字或主题
注:网页可以用 Web DreamWeaver编辑器编辑网页帮助文档,chm可以用Easy chm进行编辑.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ProgressBar
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//progressBar1.Value = 1000;
//progressBar1.Increment(1500);
}
private void timer1_Tick(object sender, EventArgs e)
{
progressBar1.PerformStep();
}
}
}
using System;
using System.Windows.Forms;
namespace ProgressBar
{
public partial class Form1 : Form
{
//日志输出方法
public void outlog(string str)
{
textBox2.AppendText(DateTime.Now.ToString() + str + "\r\n");
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
btnSet.Enabled = true;
btnStart.Enabled = false;
btnPause.Enabled = false;
btnStop.Enabled = false;
}
private void timer1_Tick(object sender, EventArgs e)
{
progressBar1.PerformStep();
}
private void btnSet_Click(object sender, EventArgs e)
{
try
{
if (tbSettime.Text.Trim() != "")
{
progressBar1.Minimum = 0;
progressBar1.Maximum = Convert.ToInt32(tbSettime.Text);
progressBar1.Step = -1;
progressBar1.Value = progressBar1.Maximum;
btnSet.Enabled = false;
btnStart.Enabled = true;
textBox2.AppendText("========================================================\r\n");
outlog("时间正确设置,请点击开始键继续。。。");
}
else
{
MessageBox.Show("请输入正确的秒数,必须为正整数");
}
}
catch (Exception)
{
MessageBox.Show("请输入正确的秒数,必须为正整数");
}
}
private void btnStart_Click(object sender, EventArgs e)
{
btnStart.Enabled = false;
btnPause.Enabled = true;
btnStop.Enabled = true;
timer1.Enabled = true;
timer1.Interval = 1000;
timer1.Start();
}
private void timer1_Tick_1(object sender, EventArgs e)
{
if (progressBar1.Value > progressBar1.Minimum)
{
progressBar1.PerformStep();
outlog("进度条工作正在进行,已经完成了" + Convert.ToInt32(((1 - Convert.ToDouble(progressBar1.Value) / Convert.ToDouble(progressBar1.Maximum)) * 100)).ToString() + "%");
//(最大值-当前值)/最大值=进度百分比
// 1-当前值/最大值=进度百分比
}
else
{
outlog("进度已经完成");
timer1.Stop();
btnSet.Enabled = true;
btnStart.Enabled = false;
btnPause.Enabled = false;
btnStop.Enabled = false;
}
}
private void btnPause_Click(object sender, EventArgs e)
{
if (timer1.Enabled == true)
{
outlog("进度已经被暂停");
btnPause.Text = "继续";
timer1.Stop();
}
else
{
outlog("进度条继续工作");
btnPause.Text = "暂停";
timer1.Start();
}
}
private void btnStop_Click(object sender, EventArgs e)
{
outlog("进度条停止工作\r\n===================");
timer1.Enabled = false;
progressBar1.Value = 0;
btnPause.Enabled = false;
btnSet.Enabled = true;
btnStart.Enabled = false;
btnStop.Enabled = false;
btnPause.Text = "暂停";
}
}
}
using System;
using System.Drawing;
using System.Windows.Forms;
namespace 任务实施
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnAdd_Click(object sender, EventArgs e)
{
ListViewItem myLVI = lv1.Items.Add((lv1.Items.Count + 1).ToString());
myLVI.SubItems.Add(tbName.Text);
myLVI.SubItems.Add(cbGender.Text);
myLVI.SubItems.Add(TbTel.Text);
myLVI.SubItems.Add(tbDepartment.Text);
myLVI.SubItems.Add(DTPJOinTime.Value.ToString("yyyy年MM月dd日"));
tbName.Clear();
cbGender.Text = "";
TbTel.Clear();
tbDepartment.Clear();
DTPJOinTime.Value = DTPJOinTime.MaxDate;
tbName.Focus();
}
private void Form1_Load(object sender, EventArgs e)
{
//设置时间最大值为当前日期,并将最大值复制给value
DTPJOinTime.MaxDate = DateTime.Now;
DTPJOinTime.Value = DTPJOinTime.MaxDate;
progressBar1.Maximum = 5;
progressBar1.Minimum = 0;
progressBar1.Step = 1;
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox1.Image = Image.FromFile(@"E:\C#\project\Windows窗体高级控件\任务实施\banner\雪上行者.PNG");
}
private void btnModi_Click(object sender, EventArgs e)
{
//修改选定内容,首先判断是否有选定的内容,选定的项目数量大于0代表有被选中
if (lv1.SelectedItems.Count > 0)
{
lv1.SelectedItems[0].SubItems[1].Text = tbName.Text;
lv1.SelectedItems[0].SubItems[2].Text = cbGender.Text;
lv1.SelectedItems[0].SubItems[3].Text = TbTel.Text;
lv1.SelectedItems[0].SubItems[4].Text = tbDepartment.Text;
lv1.SelectedItems[0].SubItems[5].Text = DTPJOinTime.Value.ToString("yyyy年MM月dd日");
tbName.Clear();
cbGender.Text = "";
TbTel.Clear();
tbDepartment.Clear();
DTPJOinTime.Value = DTPJOinTime.MaxDate;
tbName.Focus();
}
}
private void btndel_Click(object sender, EventArgs e)
{
//删除所选定的项目
lv1.SelectedItems[0].Remove();
}
private void btnClear_Click(object sender, EventArgs e)
{
//清除ListView中所有内容
lv1.Items.Clear();
}
//声明图片存放的路径,并声明一个随机变量,以便随机显示图片
string[] path = System.IO.Directory.GetFiles(@"E:\C#\project\Windows窗体高级控件\任务实施\banner");
Random r = new Random();
private void timer1_Tick(object sender, EventArgs e)
{
int num = 0;
if (tbName.Text.Trim() != "") num++;
if (cbGender.Text != "") num++;
if (TbTel.Text.Trim() != "") num++;
if (tbDepartment.Text.Trim() != "") num++;
if (DTPJOinTime.Value != DTPJOinTime.MaxDate) num++;
progressBar1.Value = num;
//随机显示图片
pictureBox1.Image = Image.FromFile(path[r.Next(0,path.Length)]);
}
private void btnCheck_Click(object sender, EventArgs e)
{
lv1.ForeColor = Color.Black;
//在ListView中查询文本
ListViewItem checkItem = lv1.FindItemWithText(tbCheck.Text);
//判定是否查询到
if (checkItem != null)
{
this.lv1.TopItem = checkItem;
checkItem.ForeColor = Color.Red;
}
else
{
MessageBox.Show(tbCheck.Text+"不存在");
}
}
}
}