C#——Windows应用程序高级控件

1.ImageList控件(存储图像控件)

ImageList控件用于存储图像资源,然后在控件上显示出来,这样就简化了对图像的管理。ImageList控件的主要属性是Images,它包含关联控件将要使用的图片。

  • 在ImageList控件中添加图像

使用ImageList控件的Images属性的Add()方法,可以以编程的方式向ImageList控件中添加图像。

namespace ImageList
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //设置要加载的第一张图片的路径
            //取当前运行的exe所在目录的上两级目录的位置,比如说当前的程序的exe在c:\temp\abc\123\sample.exe
            //用下面的就会取到c:\temp
            //下面的语句相当于string Path = Application.StartupPath + @"\..\..";
            string Path = Application.StartupPath.Substring(0, Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\"));
            //string Path = Application.StartupPath + @"\..\..";
            //@的意思是忽略字符中的转义符,比如有路径是c:\temp\123.txt,就不能直接写成string str = "c:\temp\123.txt";
            //这样编译时就会报错,因为它以为你是要转义,而不是要\这个字符,所以要写成string str = "c:\\temp\\123.txt";或是string str = @"c:\\temp\\123.txt";
            Path += @"\01.jpg";
            string Path2 = Application.StartupPath.Substring(0, Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\"));
            Path2 += @"\02.jpg";
            Image One = Image.FromFile(Path, true);
            imageList1.Images.Add(One);
            Image Two = Image.FromFile(Path2, true);
            imageList1.Images.Add(Two);
            imageList1.ImageSize = new Size(250, 200);
            pictureBox1.Width = 250;
            pictureBox1.Height = 200;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //设置pictureBox1的图像索引是imageList1、控件索引为0的图片
            pictureBox1.Image = imageList1.Images[0];
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //设置pictureBox1的图像索引是imageList1、控件索引为1的图片
            pictureBox1.Image = imageList1.Images[1];
        }
    }
}

  • 在ImageList控件中移除图像

可以使用RemoveAt()方法移除单个图像或可以使用Clear()方法清除图像列表中的所有图像。

RemoveAt()方法用于从列表中移除图像

public void RemoveAt(int index)

Clear()方法用于从ImageList中移除所有图像

2.ListView控件(列表视图控件)

ListView控件显示带图标的项的列表,可以显示大图标、小图标和数据。使用ListView控件可以创建类似Windows资源管理器右窗口的用户界面。

①添加项Add()

namespace ListView

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }



        private void Form1_Load(object sender, EventArgs e)

        {



        }



        private void button1_Click(object sender, EventArgs e)

        {

            if (textBox1.Text == "")

            {

                MessageBox.Show("项目不能为空");

            }

            else

            {

                //使用ListView控件的Items属性的Add()方法向控件中添加项

                listView1.Items.Add(textBox1.Text.Trim());

                textBox1.Clear();

            }

        }



        private void textBox1_TextChanged(object sender, EventArgs e)

        {



        }

    }

}

②移除项

RemoveAt()方法移除指定项,Clear()方法移除列表中的所有项。

namespace ListView

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }



        private void Form1_Load(object sender, EventArgs e)

        {



        }



        private void button1_Click(object sender, EventArgs e)

        {

            if (textBox1.Text == "")

            {

                MessageBox.Show("项目不能为空");

            }

            else

            {

                //使用ListView控件的Items属性的Add()方法向控件中添加项

                listView1.Items.Add(textBox1.Text.Trim());

                textBox1.Clear();

            }

        }



        private void textBox1_TextChanged(object sender, EventArgs e)

        {



        }



        private void button2_Click(object sender, EventArgs e)

        {

            if (listView1.SelectedItems.Count == 0) //判断是否选择了要删除的项

            {

                MessageBox.Show("请选择要删除的项");

            }

            else

            {

                //使用RemoveAt()方法移除选择的项目

                listView1.Items.RemoveAt(listView1.SelectedItems[0].Index);

                listView1.SelectedItems.Clear(); //取消控件的选择

            }

        }



        private void button3_Click(object sender, EventArgs e)

        {

            if (listView1.Items.Count == 0) //判断控件中是否存在项目

            {

                MessageBox.Show("项目中已经没有项目");

            }

            else

            {

                listView1.Items.Clear(); //使用Clear()方法移除所有项目

            }

        }

    }

}

③选择ListView控件中的项

通过控件的Selected属性设置控件中的选择项

private void Form1_Load(object sender, EventArgs e)

 {

     listView1.Items.Add("支付宝");

     listView1.Items.Add("微信");

     listView1.Items.Add("QQ");

     listView1.Items[2].Selected = true;

 }

④为ListView控件中的项添加图标

要为ListView控件中的项添加图标,则需要与ImageList控件相结合,使用ImageList控件设置ListView控件中项的图标。

private void Form1_Load(object sender, EventArgs e)

{

//LargeImagelist:获取或设置当前项以大图标显示在控件中显示使使用的ImageList

//SmallImagelist: 获取或设置当前项以小图标显示在控件中显示使使用的ImageList

    listView1.LargeImageList = imageList1; //设置控件的LargeImageList属性

    imageList1.ImageSize = new Size(37, 36); // 设置ImageList控件图标的大小

    //向imageList1中添加两个图标

    imageList1.Images.Add(Image.FromFile("D:\\ListView\\ListView\\01.jpg"));

    imageList1.Images.Add(Image.FromFile("D:\\ListView\\ListView\\02.jpg"));

    listView1.SmallImageList = imageList1; //设置控件的SmallImageList属性

    //向控件中添加两项

    listView1.Items.Add("微信");

    listView1.Items.Add("回收站");

    listView1.Items[0].ImageIndex = 0;

    listView1.Items[1].ImageIndex = 1;



}

⑤在ListView控件中启用平铺视图

将ListView控件中的View属性设置为Tile

listView1.View = View.Tile; //设置listView1控件的View属性

⑥为ListView控件中的项分组

(a)添加组

public int Add(ListViewGroup group)

其中group为要添加到集合中的ListViewGroup

返回值为该组在集合中的索引;如果集合中已存在该组,则返回-1。

private void Form1_Load(object sender, EventArgs e)

{

    listView1.Groups.Add(new ListViewGroup("测试",HorizontalAlignment.Left));

//LargeImagelist:获取或设置当前项以大图标显示在控件中显示使使用的ImageList

//SmallImagelist: 获取或设置当前项以小图标显示在控件中显示使使用的ImageList

    listView1.LargeImageList = imageList1; //设置控件的LargeImageList属性

    imageList1.ImageSize = new Size(37, 36); // 设置ImageList控件图标的大小

    listView1.View = View.Tile; //设置listView1控件的View属性

    //向imageList1中添加两个图标

    imageList1.Images.Add(Image.FromFile("D:\\ListView\\ListView\\01.jpg"));

    imageList1.Images.Add(Image.FromFile("D:\\ListView\\ListView\\02.jpg"));

    listView1.SmallImageList = imageList1; //设置控件的SmallImageList属性

    //向控件中添加两项

    listView1.Items.Add("微信");

    listView1.Items.Add("回收站");

    listView1.Items[0].ImageIndex = 0;

    listView1.Items[1].ImageIndex = 1;

    listView1.Items[0].Group = listView1.Groups[0];

    listView1.Items[1].Group = listView1.Groups[0];



}

(b)移除组

使用Group集合的RemoveAt或Clear()方法

public void RemoveAt(int index)

public void Clear()

(c)向组分配项或在组之间移动项

设置各个项的System.Windows.Forms.ListViewItem.Group属性,可以向组分配项或在组之间移动项。

ListView1.Item[0].Group = listView1.Group[0];

3.TreeView控件(树控件)

使用TreeView控件Nodes属性的Add()方法,可以向控件中①添加节点。

public virtual int Add(TreeNode node)

namespace TreeView

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)

        {

            //为控件建立3个父节点

            TreeNode tn1 = treeView1.Nodes.Add("名称");

            TreeNode tn2 = treeView1.Nodes.Add("性别");

            TreeNode tn3 = treeView1.Nodes.Add("年龄");



            //建立3个子节点

            TreeNode Ntn1 = new TreeNode("马云");

            TreeNode Ntn2 = new TreeNode("董明珠");

            TreeNode Ntn3 = new TreeNode("马化腾");



            //将以上3个子节点添加到第一个父节点中

            tn1.Nodes.Add(Ntn1);

            tn1.Nodes.Add(Ntn2);

            tn1.Nodes.Add(Ntn3);



            //再建立3个子节点,用于显示性别

            TreeNode Stn1 = new TreeNode("男");

            TreeNode Stn2 = new TreeNode("女");

            TreeNode Stn3 = new TreeNode("男");



            //将以上3个子节点添加到第二个父节点中

            tn2.Nodes.Add(Stn1);

            tn2.Nodes.Add(Stn2);

            tn2.Nodes.Add(Stn3);



            //再建立3个子节点,用于显示年龄

            TreeNode Atn1 = new TreeNode("21");

            TreeNode Atn2 = new TreeNode("22");

            TreeNode Atn3 = new TreeNode("23");



            //将以上3个子节点提那家到第三个父节点中

            tn3.Nodes.Add(Atn1);

            tn3.Nodes.Add(Atn2);

            tn3.Nodes.Add(Atn3);

        }



        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)

        {



        }



       

    }

}

②移除节点

使用TreeView控件Nodes属性的Remove()方法可以从树节点集合中移除指定的树节点。

public void Remove(TreeNode node)

private void button1_Click(object sender, EventArgs e)

{

    if (treeView1.SelectedNode.Text == "名称" || treeView1.SelectedNode.Text == "性别" || treeView1.SelectedNode.Text == "年龄")

    {

        MessageBox.Show("请选择要删除的子节点");

    }

    else

    {

        treeView1.Nodes.Remove(treeView1.SelectedNode);

    }

}

③为树控件中的节点设置图标

图标紧挨着节点文本的左侧,若要显示图标,必须使树视图与ImageList控件相关联。

步骤:设置TreeView控件的ImageList属性为想要使用的现有的ImageList控件;设置节点的ImageIndex和SelectImageIndex属性,ImageIndex属性确定正常和展开状态下的节点显示的图像,SelectImageIndex属性确定选定状态下的节点显示的图像。

private void Form1_Load(object sender, EventArgs e)

{

    //为控件建立3个父节点

    TreeNode tn1 = treeView1.Nodes.Add("名称");

    TreeNode tn2 = treeView1.Nodes.Add("性别");

    TreeNode tn3 = treeView1.Nodes.Add("年龄");



    //建立3个子节点

    TreeNode Ntn1 = new TreeNode("马云");

    TreeNode Ntn2 = new TreeNode("董明珠");

    TreeNode Ntn3 = new TreeNode("马化腾");



    //将以上3个子节点添加到第一个父节点中

    tn1.Nodes.Add(Ntn1);

    tn1.Nodes.Add(Ntn2);

    tn1.Nodes.Add(Ntn3);



    //再建立3个子节点,用于显示性别

    TreeNode Stn1 = new TreeNode("男");

    TreeNode Stn2 = new TreeNode("女");

    TreeNode Stn3 = new TreeNode("男");



    //将以上3个子节点添加到第二个父节点中

    tn2.Nodes.Add(Stn1);

    tn2.Nodes.Add(Stn2);

    tn2.Nodes.Add(Stn3);



    //再建立3个子节点,用于显示年龄

    TreeNode Atn1 = new TreeNode("21");

    TreeNode Atn2 = new TreeNode("22");

    TreeNode Atn3 = new TreeNode("23");



    //将以上3个子节点提那家到第三个父节点中

    tn3.Nodes.Add(Atn1);

    tn3.Nodes.Add(Atn2);

    tn3.Nodes.Add(Atn3);



    //设置ImageList1控件中显示的图像

    imageList1.Images.Add(Image.FromFile("D:\\Visual Studio\\Visual Studio files\\TreeView\\TreeView\\1723617351691.jpg"));

    imageList1.Images.Add(Image.FromFile("D:\\Visual Studio\\Visual Studio files\\TreeView\\TreeView\\1723617389443.jpg"));



    //设置TreeView1的ImageList属性为ImageList1

    treeView1.ImageList = imageList1;

    imageList1.ImageSize = new Size(16,16);



    //设置treeView控件节点的图标在ImageList1控件中的索引为0

    treeView1.ImageIndex = 0;

    //选择某个节点后显示的图标在ImageList1控件中的索引为1

    treeView1.SelectedImageIndex = 1;

}

4.DateTimePicker控件(日期控件)

①使用DateTimePicker控件显示时间

通过将控件的Format属性设置为Time,实现控件只显示时间。Format属性用于获取或设置控件中显示的日期和时间格式。

private void Form1_Load(object sender, EventArgs e)

{

    //设置dateTimePicker1的Format属性为Time,使其只显示事件

    dateTimePicker1.Format = DateTimePickerFormat.Time;

    textBox1.Text = dateTimePicker1.Text;

}

②使用DateTimePicker控件以自定义格式显示日期

通过DateTimePicker控件的CustomFormat属性可以自定义日期/时间格式字符串。

public string CustomFormat{get;set;}

private void Form1_Load(object sender, EventArgs e)

 {

     //设置dateTimePicker1的Format属性为Time,使其只显示事件

     dateTimePicker1.Format = DateTimePickerFormat.Time;

     textBox1.Text = dateTimePicker1.Text;



     //自定义格式显示日期

     //设置DateTimePicker2的Format属性为Custom,使其用户自定义的时间格式生效

     dateTimePicker2.Format = DateTimePickerFormat.Custom;

     //通过控件的CustomFormat属性设置自定义的格式

     dateTimePicker2.CustomFormat = "MMMM dd,yyyy-dddd";

     label1.Text = dateTimePicker2.Text;

 }

③返回DateTimePicker控件中选择的日期

调用控件的Text属性 以返回与控件中的格式相同的完整值,或调用Value属性的适当方法来返回部分值,这些方法包括Year、Month、Day方法等,使用ToString将信息转换成可显示给用户的字符串。

private void Form1_Load(object sender, EventArgs e)

{

    //设置dateTimePicker1的Format属性为Time,使其只显示事件

    dateTimePicker1.Format = DateTimePickerFormat.Time;

    textBox1.Text = dateTimePicker1.Text;



    //自定义格式显示日期

    //设置DateTimePicker2的Format属性为Custom,使其用户自定义的时间格式生效

    dateTimePicker2.Format = DateTimePickerFormat.Custom;

    //通过控件的CustomFormat属性设置自定义的格式

    dateTimePicker2.CustomFormat = "MMMM dd,yyyy-dddd";

    label1.Text = dateTimePicker2.Text;



    //使用控件的Text属性获取当前控件选择日期

    textBox2.Text = dateTimePicker3.Text;

    //适应Value属性的Year方法获取选择日期的年

    textBox3.Text = dateTimePicker3.Value.Year.ToString();

    //使用Value属性的Month方法获取选择日期的月

    textBox4.Text = dateTimePicker3.Value .Month .ToString();

    //使用Value属性的Day方法选择日期的日

    textBox5.Text = dateTimePicker3.Value .Day .ToString();

}

5.MonthCalendar控件(月历控件)

MonthCalendar控件提供了一个直观的图形界面,可以让用户查看和设置日期。

①更改MonthCalendar控件的外观

将ShowWeekNumbers属性设置为true,实现在控件中显示周数。也可以用代码或在“属性”窗口中设置此属性。周数以单独的列出现在一周的第一天的左边。

private void Form1_Load(object sender, EventArgs e)

{

     monthCalendar1.ShowWeekNumbers = true;

}

②在MonthCalendar控件中显示多个月份

通过设置CalendarDimensions属性指定显示多少个月以及它们在控件中的排列方式。

private void Form1_Load(object sender, EventArgs e)

{

    //ShowWeekNumbers属性设置为true显示周数

    monthCalendar1.ShowWeekNumbers = true;



    //设置控件的CalendarDimensions属性,使控件在水平和垂直方向都显示2个月份

    monthCalendar1.CalendarDimensions = new Size(2, 2);



}

③在MonthCalendar控件中选择日期范围

如果要在MonthCalendar控件中选择日期范围,必须设置SelectionStart和SelectionEnd属性。这两个属性分别用于设置日期的起始和节数。

private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)

{

    //通过SelectionStart属性获取用户选择的开始日期

    textBox2.Text = monthCalendar1 .SelectionStart.ToString();

    //通过SelectionEnd属性获取用户选择的结束日期

    textBox3.Text = monthCalendar1 .SelectionEnd.ToString();

}

6.其他高级控件

①使用ErrorProvider控件验证文本框输入

namespace others

{

    public partial class Form1 : Form

    {

        private int a, b, c;

        public Form1()

        {

            InitializeComponent();

        }



        private void textBox1_Validating(object sender, CancelEventArgs e)

        {

            if (textBox1.Text == "")

            {

                errorProvider1.SetError(textBox1, "不能为空");

            }

            else

            {

                errorProvider1.SetError(textBox1, "");

                a = 1;

            }

        }



        private void textBox2_Validating(object sender, CancelEventArgs e)

        {

            if (textBox2.Text == "")

            {

                errorProvider2.SetError(textBox2, "不能为空");

            }

            else

            {

                try

                {

                    int x = Int32.Parse(textBox2.Text);

                    errorProvider2.SetError(textBox2, "");

                    b = 1;

                }

                catch

                {

                    //如果出现异常,设置errorProvider2控件的错误信息

                    errorProvider2.SetError(textBox2, "请输入一个数字");

                }

            }

        }



        private void textBox3_Validating(object sender, CancelEventArgs e)

        {

            if (textBox3.Text == "")

            {

                errorProvider3.SetError(textBox3, "不能为空");

            }

            else

            {

                errorProvider3.SetError(textBox3, "");

                c = 1;

            }

        }



        private void button1_Click(object sender, EventArgs e)

        {

            if (a + b + c == 3)

            {

                MessageBox.Show("数据录入成功","提示",MessageBoxButtons.OK ,MessageBoxIcon.Warning );

                a = 0;

                b = 0;

                c = 0;

            }

        }



        private void button2_Click(object sender, EventArgs e)

        {

            //清空所有文本框

            textBox1.Text = "";

            textBox2.Text = "";

            textBox3.Text = "";



        }



        private void Form1_Load(object sender, EventArgs e)

        {



           

        }



        private void textBox1_TextChanged(object sender, EventArgs e)

        {

        }





    }

}

②使用HelpProvider控件调用帮助文件

HelpProvider控件可以将帮助文件(.html文件或.chm文件)与Windows应用程序相关联,为特定对话框或对话框中的特定控件提供区分上下文的帮助,打开帮助文件到特定的部分,如目录、索引、搜索功能的主页。

public virtual string HelpNamespace{get;set;}

private void Form1_Load(object sender, EventArgs e)

{

    //s设置帮助文件的额位置

    string strPath = Application.StartupPath .Substring (0,Application.StartupPath.Substring (0,Application .StartupPath .LastIndexOf ("\\")).LastIndexOf("\\"));

    strPath += @"/01.html";

    //设置helpProvider控件的pNamespace属性,设置帮助文件的路径

    helpProvider1.HelpNamespace = strPath;

    //设置SetShowHelp()方法指定是否显示指定控件的帮助信息

    helpProvider1.SetShowHelp(this, true);      

}

按F1后打开指定的帮助文件

③使用Timer控件设置时间间隔

Timer控件可以定期引发事件,此控件是为Windows窗体环境设计。时间间隔的长度由Interval属性定义,其值以毫秒为单位。若启用了该组件,则每个时间间隔引发一个Tick事件,在Tick事件中添加要执行的代码。

namespace Timer

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }



        private void Form1_Load(object sender, EventArgs e)

        {

            timer1.Interval = 1000;

        }



        private void timer1_Tick(object sender, EventArgs e)

        {

            textBox1.Text = DateTime.Now.ToString();

        }



        private void button1_Click(object sender, EventArgs e)

        {

            if (button1.Text == "开始")

            {

                timer1.Enabled = true;

                button1.Text = "停止";

            }

            else

            {

                timer1 .Enabled = false;

                button1.Text = "开始";

            }

        }



        private void textBox1_TextChanged(object sender, EventArgs e)

        {



        }

    }

}

④使用ProgressBar控件显示程序运行进度条

ProgressBar控件通过水平放置的方案中显示适当数目的矩形,指示工作的进度。工作完成时,进度条被填满。进度条用于帮助

private void button1_Click(object sender, EventArgs e)

{

    button1 .Enabled = false;

    progressBar1.Minimum = 0;

    progressBar1 .Maximum = 100;

    progressBar1 .Step = 1;

    for (int i = 0; i < 100; i++)

    {

        progressBar1.PerformStep();

        textBox1 .Text = "进度条"+progressBar1.Value.ToString ();

    }

}

  • 15
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值