C#学习之路2

文章目录

1.窗体属性

更换窗体的图标
窗体样式
Icon: 更换图标

外观
FormBorderStyle

属性意义
FormBorderStyle.None0无边框
FormBorderStyle.FixedSingle1固定的单行边框
FormBorderStyle.Fixed3D2固定的三维样式边框
FormBorderStyle.FixedDialog3固定的对话框样式的粗边框
FormBorderStyle.Sizable4可调整大小的边框
FormBorderStyle.FixedToolWindow5不可调整大小的工具窗口边框
FormBorderStyle.SizableToolWindow6可调整大小的工具窗口边框

布局
StartPosition

属性意义
CenterParent窗体在其父窗体中居中
CenterScreen窗体在当前显示窗口中居中,其尺寸在窗体大小中指定。
Manual窗体的位置由 Location 属性确定。
WindowsDefaultBounds窗体定位在 Windows 默认位置,其边界也由 Windows 默认决定。
WindowsDefaultLocation窗体定位在 Windows 默认位置,其尺寸在窗体大小中指定。

Size:设置窗体的大小

外观
BackgroundImage:设置窗体的背景图片

2.控件绑定

视图 >工具箱
之后拖动选择的控键到窗体中即可
外观
Test:设置控键文本内容

代码如下:
Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            //设置启动窗体From1
            Application.Run(new Form1());
        }
    }
}

Form1.cs

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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        //此处为button的点击,并调用From2的显示的功能
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm = new Form2();//实例话Form2
            frm.Show();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            this.Hide();//隐藏原有窗体1
        }
    }
}

2.窗体的事件

1.click(单击)事件
private void button2_Click(object sender, EventArgs e)
{
    MessageBox.Show("点击了窗体");
}
2.Load(加载)事件
private void Form1_Load(object sender, EventArgs e)
{
    //if语句判断是否点击了“是”按钮
    if (MessageBox.Show("是否查看窗体!","",MessageBoxButtons.YesNo,MessageBoxIcon.Information)==DialogResult.Yes)
    {

    }
}
3.FormClosing(关闭)事件
public Form1()
{
    InitializeComponent();
    //对FormClosing事件进行注册
    this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
}
.............

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
    MessageBox.Show("过程2");
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if (MessageBox.Show("是否关闭窗体?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
    {
        MessageBox.Show("过程1");
    }
    else
    {
        e.Cancel = true;//撤销事件关闭
    }
}

3.MDI窗体

1.设置子窗体
    

//设置MDI窗体
//  1、窗口样式-》IsMdiContainer:True
public Form1()
        {
            InitializeComponent();
// 2、设置子窗体
            Form2 frm2 = new Form2();
            frm2.Show();
            frm2.MdiParent = this;
            Form3 frm3 = new Form3();
            frm3.Show();
            frm3.MdiParent = this;
            Form4 frm4 = new Form4();
            frm4.Show();
            frm4.MdiParent = this;
        }
.....

2.排列MDI子窗体

控件选择:MenuStrip

//排列MDI窗体1
private void 加载子窗体ToolStripMenuItem_Click(object sender, EventArgs e)
{
    Form2 frm2 = new Form2();
    frm2.Show();
    frm2.MdiParent = this;
    Form3 frm3 = new Form3();
    frm3.Show();
    frm3.MdiParent = this;
    Form4 frm4 = new Form4();
    frm4.Show();
    frm4.MdiParent = this;
}

private void 水平平铺ToolStripMenuItem_Click(object sender, EventArgs e)
{
    LayoutMdi(MdiLayout.TileHorizontal);//使用MdiLayout枚举实现窗体的水平平铺

}

private void 垂直平铺ToolStripMenuItem_Click(object sender, EventArgs e)
{
    LayoutMdi(MdiLayout.TileVertical);  //使用MdiLayout枚举实现窗体的垂直平铺
}

private void 层叠排列ToolStripMenuItem_Click(object sender, EventArgs e)
{
    LayoutMdi(MdiLayout.Cascade);  //使用MdiLayout枚举实现窗体的层叠排列
}
3.继承窗体

继承的窗体可以更改自身的文本等相关值设置

且窗体上添加TextBox控件、Button控件和Label控件。在button控件的click事件添加代码,实现Lable控件中输入的内容。

Form1控件相关如下:
在这里插入图片描述

在这里插入图片描述

Form1.cs

private void button1_Click(object sender, EventArgs e)
{
    //1.创建继承窗体
    string str = textBox1.Text.ToString();
    label1.Text = str;

}

Form2控件继承操作如下:
Form2.cs

public partial class Form2 : WindowsFormsApplication1.Form1
{

........
}

在这里插入图片描述

Program.cs

...........
//设置启动窗体From1
Application.Run(new Form2());

4.控件

1.控件的分类和作用
控件分类作用
文本类控件可以在控件上显示文本
选择类控件主要为用户提供选择的项目
分组控件可以将窗体中的其他控件进行分组处理
菜单控件为系统制作功能菜单,将应用程序命令分组,使它们更容易访问
工具栏控件提供了主菜单中常用的相关工具
状态栏控件用于显示窗体上的对象的相关信息,或者可以显示应用程序的信息
2.控件命名规范
控件名称开头缩写
TextBoxtxt
Buttonbtn
ComboBoxcbox
Labellab
DataGridViewdgv
Panelpl
TabControltcl
ErrorProviderepro
ImageListilist
ListBoxlb
Timertmr
CheckBoxchb
LinkLabelrlbl
RichTextBoxrtbox
CheckedListBoxclbox
RadioButtonrbtn
NumericUpDownnudown
HelpProviderhpro
ListViewlv
TreeViewtv
PictrueBoxpbox
Notifylconnicon
DateTimePickerdtpicker
MonthCalendarmcalen
ToolTipttip
3.文本控件
标签控件(Label控件)
1.设置标签文本
label1.Text = "用一生下载你";
2.显示/隐藏控件
label1.Visible = true;
按钮控件(Button 控件)
1.响应按钮的单击事件
private void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show("单击了按钮,引发了Click事件");
    }
2.将按钮设置为窗体的“接受”按钮
private void button1_Click(object sender, EventArgs e)
{
    MessageBox.Show("引发了接受按钮");
}

private void Form1_Load(object sender, EventArgs e)
{
    this.AcceptButton = button1;
}
3.将按钮设置为窗体的“取消”按钮
private void button1_Click(object sender, EventArgs e)
{
    MessageBox.Show("单击了取消按钮");

}

private void Form1_Load(object sender, EventArgs e)
{
    this.CancelButton = button1; //将 Button2按钮设置为窗体的“取消”按钮
   
}
文本框控件(TextBox 控件)
1.创建只读文本框
private void Form1_Load(object sender, EventArgs e)
{
            //this.CancelButton = button1; //将 Button2按钮设置为窗体的“取消”按钮
            textBox1.ReadOnly = true;  //将文本框设置为只读
            textBox1.Text = "用-生下载你";   //设置其Text属性
}
2.创建密码文本框
private void Form1_Load(object sender, EventArgs e)
{
    
    textBox1.PasswordChar = '@';//设置文本框的PasswordChar属性为字符@
    textBox2.UseSystemPasswordChar = true;//设置文本框的UseSystemPasswordChar属性为true

}
3.创建多行文本框
private void Form1_Load(object sender, EventArgs e)
{
    //this.CancelButton = button1; //将 Button2按钮设置为窗体的“取消”按钮
    textBox1.Multiline = true;  //设置文本框的Mutiline属性使其多行显示
    //设置文本框的Text属性
    textBox1.Text ="昨夜星辰昨夜风,画楼西畔桂堂东。身无彩凤双飞翼,心有灵犀一点通。";
    textBox1.Height = 100;  //设置文本框的高


}
4.突出显示文本框中的文本
private void Form1_Load(object sender, EventArgs e)
{
    textBox1.Multiline = true; //设置文本框的Multiline 属性使其多行显示
    textBox1.Text = "昨夜星辰昨夜风,画楼西畔桂堂东。身无彩凤双飞翼,心有灵犀一点通。";
    textBox1.Height = 100;//设置文本框的高
    textBox1.SelectionStart = 5; //从文本框中索引为5的位置开始选择
    textBox1.SelectionLength = 5;  //选择长度是5个字符
}
5.响应文本框的文本更改事件
private void textBox1_TextChanged(object sender, EventArgs e)
{
    label1.Text = textBox1.Text;  //Label控件显示的文字随文本框中的数据而改变
}
有格式文本控件( RichTextBox控件)
1.在RichTextBox控件中显示滚动条
属性值说明
Both只有当文本超过控件的宽度或长度时,才显示水平滚动条或垂直滚动条,或两个滚动条都显示
None从不显示任何类型的滚动条
Horizontal只有当文本超过控件的宽度时,才显示水平滚动条。必须将WordWrap属性设置为false,才会出现这种情况
Vertical只有当文本超过控件的高度时,才显示垂直滚动条
ForcedHorizontal当WordWrap属性设置为false 时,显示水平滚动条。在文本未超过控件的宽度时,该滚动条显示为浅灰色
ForcedVertical始终显示垂直滚动条。在文本未超过控件的长度时,该滚动条显示为浅灰色
ForcedBoth始终显示垂直滚动条。当WordWrap属性设置为false 时,显示水平滚动条。在文本未超过控件的宽度或长度时,两个滚动条均显示为灰色
private void Form1_Load(object sender, EventArgs e)
{
    richTextBox1.Multiline = true;//将Multiline属性设为true实现多行显示
    //设置ScrollBars属性实现只显示垂直滚动条
    richTextBox1.ScrollBars = RichTextBoxScrollBars.Vertical;
}
2.在RichTextBox控件中设置字体属性
private void Form1_Load(object sender, EventArgs e)
{
    richTextBox1.Multiline = true;  //将Mutiline属性设为true实现多行显示
    richTextBox1.ScrollBars = RichTextBoxScrollBars.Vertical;//设置ScrollBars属性实现只显示垂直滚动条
    //设置SelectionFont属性实现控件中的文本为楷体,大小为12,字样是粗体
    richTextBox1.SelectionFont = new Font("楷体", 12, FontStyle.Bold);
    //设置SelectionColor实现控件中的文本颜色为蓝色
    richTextBox1.SelectionColor = System.Drawing.Color.Blue;

}
3.将RichTextBox控件显示为超链接样式
private void Form1_Load(object sender, EventArgs e)
    {
        richTextBox1.Multiline = true; //将Mutiline属性设为true实现多行显示
        richTextBox1.ScrollBars = RichTextBoxScrollBars.Vertical;//设置ScrollBars属性实现只显示垂直滚动条
        //设置控件的Text属性
        richTextBox1.Text = "欢迎登录http://www.cccxy.com编程体验网";
    }
 
private void richTextBox1_LinkClicked(object sender, LinkClickedEventArgs e) { 
        //在控件的LinkClicked事件中编写如下代码实现内容中的网址带下划线
        System.Diagnostics.Process.Start(e.LinkText);
}
4.在RichTextBox控件中设置段落格式
private void Form1_Load(object sender, EventArgs e)
{
    richTextBox1.Multiline = true;//将Mutiline属性设为true实现多行显示
   
    richTextBox1.ScrollBars = RichTextBoxScrollBars.Vertical; //设置ScrolBars属性实现只显示垂直滚动条
    //控件的SelectionBullet 属性设为true,使控件中的内容以项目符号列表的格式排列
    richTextBox1.SelectionBullet = true;
}
private void Form1_Load(object sender, EventArgs e)
    {
        richTextBox1.Multiline = true; //将Mutiline属性设为true实现多行显示
        //设置ScrollBars属性实现只显示垂直滚动条
            richTextBox1.ScrollBars = RichTextBoxScrollBars.Vertical;
        //设置SelectionIndent属性的值为8,使控件中数据的左边缘与控件的左边缘之间的距离为8像素
        richTextBox1.SelectionIndent = 8;
        //设置SelectionRightIndent属性为12,使控件中文本的右边缘与控件右边缘的距离为12像素
        richTextBox1.SelectionRightIndent = 12;

    }
4.选择类控件
下拉组合框控件(ComboBox控件)
1.创建只可以选择的下拉框
 private void Form1_Load(object sender, EventArgs e)
    {
        //设置DropDownStyle属性,使控件呈现下拉列表的样式
        comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
        comboBox1.Items.Add("用一生下载你");    //向控件中添加数据
        comboBox1.Items.Add("芸烨湘枫");//向控件中添加数据
        comboBox1.Items.Add("一生所爱");//向控件中添加数据

    }
2.响应下拉组合框的选项值更改事件
private void Form1_Load(object sender, EventArgs e)
{
        //设置DropDownStyle属性,使控件呈现下拉列表的样式
        comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
        comboBox1.Items.Add("用一生下载你");    //向控件中添加数据
        comboBox1.Items.Add("芸烨湘枫");//向控件中添加数据
        comboBox1.Items.Add("一生所爱");//向控件中添加数据

}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    //在控件的SelectedValueChanged事件中,使Label控件的Text属性等于控件的选择项
    label1.Text = comboBox1.Text;

}
复选框控件( CheckBox控件)
1.判断复选框是否选中
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
    //使用if语句判断控件是否被选中
    if (checkBox1.CheckState == CheckState.Checked)
    {
        MessageBox.Show("CheckBox控件被选中"); // 如果 被选中弹出相应提示
    }
    else
    {
        //否则
        //提示该控件的选择被取消
        MessageBox.Show("CheckBox控件选择被取消");
    }
}
2.响应复选框的选中状态更改事件
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
            // 在CheckStateChanged事件中编写代码,实现当控件的选择状态发生改变时,弹出提示框
    MessageBox.Show("控件的选择状态发生改变");
}
单选按钮控件( RadioButton控件)
1.判断单选按钮是否选中
private void Form1_Load(object sender, EventArgs e)
{
     //设置两个单选按钮的Checked属性为false
     radioButton1.Checked = false;
     radioButton2.Checked = false;
}

private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
    //控件的Click事件中通过if语句判断控件的Checked属性的返回值是否为true
    if (radioButton1.Checked == true)
        MessageBox.Show("RadioButton1控件被选中");

}

private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
    //控件的Click事件中通过if语句判断控件的Checked属性的返回值是否为true
    if (radioButton2.Checked == true)
    {
        MessageBox.Show("RadioButton2控件被选中");
    }

}
2.响应单选按钮选中状态更改事件
private void Form1_Load(object sender, EventArgs e)
{
    //设置两个单选按钮的Checked属性为false
    radioButton1.Checked = false;
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
     {
            //在控件的CheckedChanged事件中编写代码,实现当控件的选择状态改变时弹出提示
            MessageBox.Show("RadioButton1控件的选中状态被更改");
        }
private void button1_Click(object sender, EventArgs e)
        {
            radioButton1.Checked = true;   //选中单选按钮
        }
private void button2_Click(object sender, EventArgs e)
        {
            radioButton1.Checked = false; //取消 单选按钮的选中状态
        }
数值选择控件( NumericUpDown控件)
1.获取NumericUpDown控件中显示的数值
private void Form1_Load(object sender, EventArgs e)
{
    //设置两个单选按钮的Checked属性为false
    radioButton1.Checked = false;


    numericUpDown1.Maximum = 20; //设置控件的最大值为20
    numericUpDown1.Minimum = 1; //设置控件的最小值为1


}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
            //实现当控件的值改变时,显示当前的值
      label1.Text = "当前控件中显示的数值: " + numericUpDown1.Value;
}
2.设置NumericUpDown控件中数值的显示方式
private void Form1_Load(object sender, EventArgs e)
{
    //设置两个单选按钮的Checked属性为false
    radioButton1.Checked = false;


    numericUpDown1.Maximum = 20;
    //设置控件的最大值为20
    numericUpDown1.Minimum = 1;
    //设置控件的最小值为1
    //设置控件的DecimalPlaces属性,使控件中的数值的小数点后显示两位数
    numericUpDown1.DecimalPlaces = 2;


}
列表控件(ListBox 控件)
1.在ListBox控件中添加和移除项
private void button1_Click(object sender, EventArgs e)
        {
            //radioButton1.Checked = true;   //选中单选按钮
            if (textBox1.Text == "")
            {
                //使用if语句判断文本框中是否输入数据
                MessageBox.Show("请输入要添加的数据");//弹出提示
            }
            else//否则
            {
                listBox1.Items.Add(textBox1.Text);//使用Add方法向控件中添加数据
                textBox1.Text = "";//清空文本框
            }
        }
private void button2_Click(object sender, EventArgs e)
    {
        //radioButton1.Checked = false; //取消 单选按钮的选中状态
        if (listBox1.SelectedItems.Count == 0)
        //判断是否选择项目
        {
            MessageBox.Show("请选择要删除的项目");
            //如果没有选择项目,弹出提示
        }
        else
        //否则
        {
            listBox1.Items.Remove(listBox1.SelectedItem);
            //使用Remove方法移除选中项
        }

        }
2.创建总显示滚动条的列表控件
private void Form1_Load(object sender, EventArgs e)
{
    //HorizontalScrollbar属性设置为true,使其能显示水平方向的滚动条
    listBox1.HorizontalScrollbar = true;
    //ScrollAlwaysVisible属性设置为true,使其能显示垂直方向的滚动条
    listBox1.ScrollAlwaysVisible = true;
}
private void button1_Click(object sender, EventArgs e)
{
     if (textBox1.Text == "") //判断文本框中是否输入数据
     {
                MessageBox.Show("添加项目不能为空");//如果没有输入数据弹出提示
     }else{
                //否则
                listBox1.Items.Add(textBox1.Text);//使用Add方法向控件中添加数据
                textBox1.Text =""; //清空文本框
            }
}
3.在ListBox控件中选择多项

SelectionMode 枚举成员及说明

枚举成员说明
MultiExtended可以选择多项,并且用户可使用Shift键、Ctrl 键和箭头键来进行选择
MultiSimple可以选择多项
None无法选择项
One只能选择一项
private void Form1_Load(object sender, EventArgs e)
{
			 //SelectionMode属性值为SelectionMode枚举成员MultiExtended,实现在控件中可以选择多项
            listBox1.SelectionMode = SelectionMode.MultiExtended;
        }

private void button1_Click(object sender, EventArgs e)
{
            //显示选择项目的数量
            label1.Text = "共选择了: " + listBox1.SelectedItems.Count.ToString() + "项";
}

private void button2_Click(object sender, EventArgs e)
{
    if (textBox1.Text == "")//判断文本框中是否输入数据
    { MessageBox.Show("添加项目不能为空");//如果没有输入数据弹出提示
    } else { //否则
        listBox1.Items.Add(textBox1.Text); //使用Add方法向控件中添加数据
        textBox1.Text = "";
                //清空文本框


    }
}
5.分组类控件
容器控件(Panel 控件)
private void Form1_Load(object sender, EventArgs e)
{
     panel1.Visible = false; //隐藏panel控件
            richTextBox1.Text = "姓名:芸烨湘枫\n性别:女ln 年龄: 27\n 民族:汉\n职业:医疗"; //设置RichTextBox控件的Text属性
}

private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("请输出姓名");
                //如果没有输入数据弹出提示
                textBox1.Focus();
                //使光标焦点处于文本框中
            }
            else
            //否则
            {
                if (textBox1.Text.Trim() == "芸烨湘枫")
                //判断文本框中是否输入“芸烨湘枫"
                {
                    panel1.Show();//如果输入“芸烨湘枫”,则显示Panel控
                }
                else//否则
                {
                    MessageBox.Show("查无此人");
                    //弹出提示
                    textBox1.Text = "";
                    //清空文本框


                }
            }
        }
分组框控件( GroupBox控件)
private void Form1_Load(object sender, EventArgs e)
{            
groupBox1.Text = "诗词";//设置控件的Text属性,使其显示“诗词”
}
选项卡控件( TabControl控件)
1.改变选项卡的显示样式

1)在选项卡的标签部位显示图标

private void Form1_Load(object sender, EventArgs e)
{
            tabControl1.ImageList = imageList1;  //设 置控件的ImageList 属性为imageList1
            //第一个选项卡的图标是imageList1中索引为0的图标
            tabPage1.ImageIndex = 0; 
            tabPage1.Text = "选项卡1";//设置控件第一个选项卡的 Text属性
            //第二个选项卡的图标是imageList1中索引为0的图标
            tabPage2.ImageIndex = 0;
            tabPage2.Text = "选项卡2";//设置控件第二个选项卡的Text属性
}

2)将选项卡显示为按钮

private void Form1_Load(object sender, EventArgs e)
{
            tabControl1.ImageList = imageList1;
            //设置控件的ImageList属性为imageList1
            //第一个选项卡的图标是imageList1中索引为0的图标
            tabPage1.ImageIndex = 0;
            //第二个选项卡的图标是imageList1中索引为0的图标
            tabPage2.ImageIndex = 0;
            //将控件的Appearance属性设置为Buttons,使选项卡具有三维按钮的外观
            tabControl1.Appearance = TabAppearance.Buttons;
}
2.在选项卡中添加控件
private void Form1_Load(object sender, EventArgs e)
{
            tabControl1.ImageList = imageList1;
            //设置控件的ImageList属性为imageList1
            //第一个选项卡的图标是imageList1中索引为0的图标
            tabPage1.ImageIndex = 0;
            //第二个选项卡的图标是imageList1中索引为0的图标
            tabPage2.ImageIndex = 0;
            Button btn1 = new Button();
            //实例化- -个Button类,动态生成-一个按钮
            btn1.Text = "新增按钮";
            //设置按钮的Text属性
            tabPage1.Controls.Add(btn1);
            //使用Add方法,将这个按钮添加到选项卡1中
        }
3.添加和移除选项卡

(1)以编程方式添加选项卡

private void Form1_Load(object sender, EventArgs e)
{   tabControl1.ImageList = imageList1;
    //设置控件的ImageList属性为imageList1
    //第一个选项卡的图标是imageList1中索引为0的图标
    tabPage1.ImageIndex = 0;
    //第二个选项卡的图标是imageList1中索引为0的图标
    tabPage2.ImageIndex = 0;
    //声明一个字符串变量,用于生成新增选项卡的名称
    string Title = "新增选项卡" + (tabControl1.TabCount + 1).ToString();
    TabPage MyTabPage = new TabPage(Title);
    //使用TabControl控件的TabPages属性的Add方法添加新的选项卡
    tabControl1.TabPages.Add(MyTabPage);
}

(2)以编程方式移除选项卡

private void button1_Click(object sender, EventArgs e)
{
    //声明一个字符串变量,用于生成新增选项卡的名称
    string Title = "新增选项卡" + (tabControl1.TabCount + 1).ToString();
    TabPage MyTabPage = new TabPage(Title); //实例化 TabPage
                                            //使用TabControl控件的TabPages属性的Add方法添加新的选项卡
    tabControl1.TabPages.Add(MyTabPage);

}

private void button2_Click(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex == 0)
            //判断是否选择了要删除的选项卡
            {
                MessageBox.Show("请选择要删除的选项卡"); //如果没有选择, 弹出提示
            }
            else {
                //使用TabControl控件的TabPages属性的Remove方法删除指定的选项卡
                tabControl1.TabPages.Remove(tabControl1.SelectedTab);
            }
}

private void button3_Click(object sender, EventArgs e)
{
     tabControl1.TabPages.Clear();   //使用Clear方法删除所有的选项卡
}
6.菜单、工具栏和状态栏控件
菜单控件( MenuStrip控件)

MenuStrip控件是程序的主菜单。MenuStrip 控件取代了先前版本的MainMenu控件。MenuStrip 控件支持多文档界面、菜单合并、工具提示和溢出。可以通过添加访问键、快捷键、选中标记、图像和分隔条,来增强菜单的可用性和可读性。

工具栏控件( ToolStrip控件)

单击工具栏中向下的箭头,添加工具栏项目时,在下拉菜单中有8种不同的类型,下面分别介绍。

  • Button: 包含文本和图像中可让用户选择的项。
  • Label: 包含文本和图像的项,不可以让用户选择,可以显示超链接。
  • SplitButton: 在Button的基础上增加了一个下拉菜单。
  • DropDownButton: 用于下拉菜单选择项。
  • Separator: 分隔符。
  • ComboBox: 显示- 个ComboBox的项。
  • TextBox: 显示一个 TextBox的项。
  • ProgressBar: 显示一 个ProgressBar的项。
状态栏控件( StatusStrip控件)

StatusSrip控件通常处于窗体的最底部,用于显示窗体上对象的相关信息,或者显示应用程序的信息。
通常,StatusStrip 控件由ToolStripStatusLabel 对象组成,每个这样的对象都可以显示文本、图标或同时显示这两者。StatusStrip 还可以包含ToolStripDropDownButton、 ToolStripSplitButton 和ToolStripProgressBar控件。

private void Form1_Load(object sender, EventArgs e)
{
            //在任务栏.上显示系统的当前日期
            this.toolStripStatusLabel1.Text = DateTime.Now.ToShortDateString();
 }
 private void button1_Click(object sender, EventArgs e)
{
            //进度条
            this.toolStripProgressBar1.Minimum = 0;
            //进度条的起始数值
            this.toolStripProgressBar1.Maximum = 5000;
            //进度条的最大值
               this.toolStripProgressBar1.Step = 2;
            //进度条的增值
            for (int i = 0; i <= 4999; i++)
                //使用for循环读取数据
             this.toolStripProgressBar1.PerformStep();
            //按照Step属性的数量增加进度栏的当前位置
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值