十三、C# WINDOW窗体技术及基础控件(3)

1、GroupBox控件

  • GroupBox控件是由System.Windows.Forms.GroupBox类提供的,作用是为其他空间提供可识别的分组
  • 可在同一页面,显示多组单选RadioButton(如果不使用此控件,单个窗体内只能一组单选RadioButton)
  • 通常,使用分组框按功能细分窗体,例如:一个学生在选择班级和系别是,为了细分窗体,可用两个GroupBox控件来设置,用Text属性来达到分组提示的目的。

在这里插入图片描述

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

        private void btmsubmit_Click(object sender, EventArgs e)
        {
            string mystr = "";
            foreach (Control ourctrl in groupBox2.Controls)   //controls代表控件集合
            {
                if (ourctrl is RadioButton)  //判断控件类型
                {
                    if (((RadioButton)ourctrl).Checked)   //遍历后的结果是普通控件,强制转换为RadioButton后在进行是否选中的判断
                    {
                        mystr += "您属于:" + ourctrl.Text;

                    }
                }
            }
            foreach (Control ourctrl in groupBox1.Controls)   //controls代表控件集合
            {
                if (ourctrl is RadioButton)  //判断控件类型
                {
                    if (((RadioButton)ourctrl).Checked)   //遍历后的结果是普通控件,强制转换为RadioButton后在进行是否选中的判断
                    {
                        mystr += "\n您的种族是:" + ourctrl.Text;

                    }
                }

            }
           

            MessageBox.Show(mystr, "选择结果是");
        }
    }
}

2、TabControl控件

  • TabControl控件是由System.windows.Forms.TabControl类提供的,作用就是将相关的组件组合到一系类选项卡页面上。

  • Multiline属性用来设置是否显示多行选项卡,如果false,而有多个选项卡不能一次显示出来,就提供组箭头查看剩余的选项卡
    在这里插入图片描述
    在这里插入图片描述

  • Appearance属性是指显示选项卡是绘制成按钮还是绘制成常规想想卡,该属性有三个值分别是Normal(绘制成常规选项)、Buttons(绘制成常规按钮)、FaltButton(绘制成平滑按钮)

  • TabControl控件管理TabPage集合

  • 添加/删除TabPage集合

    • 右击【添加选项卡】或选择【移除选项卡】注意:移除的是当前选中的选项卡,而不是你右击的选项卡
      在这里插入图片描述
      在这里插入图片描述

    • TabPages属性来添加和删除
      在这里插入图片描述
      在这里插入图片描述

    • TabPages的text属性是用来设置选项卡上显示的内容

    • SelectedTab属性可以确定当前的选项卡。每次选择新选项卡时,都会除服SelectedIndexChanged事件,通过SelectedIndex属性和SelectedTab属性确认当前选择,就可以根据选项卡进行特定的处理。

在这里插入图片描述

  • 设置TabControl时,务必看清TabPages还是选择了TabControl控件属性
  • 自带属性ctl+tab(可以用ctrl+tab进行选项卡切换,不用单独设置,自带这种属性)
using System;
using System.Windows.Forms;

namespace _28_TabControl控件
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            MessageBox.Show("你点击了"+tabControl1.SelectedTab.Text+"\n索引值是"+tabControl1.SelectedIndex.ToString(),"你选择的结果");
        }
    }
}

3、MenuStrip控件和ContexMenuStrip控件

3.1 MenuStrip控件

  • 菜单是用户获取影城程序中主要功能和使用程序的主要途径,如新建文件、打开文件等。
  • MenuStrip控件由System.windows.Forms.MenuStrip类提供,却带了以前的MainMenu控件,是应用程序菜单结构的容器。在建立菜单时,要给MenuStrip控件添加ToolStripMenu对象这个操作可在设置时完成,也可在代码中完成。
    在这里插入图片描述
    在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3.2 ContextMenuStrip控件

ContextMenuStrip控件是由System.Windows.Forms.ContextMenuStrip类提供。也是ToolStripMenu对象的容器,用来创建窗体右击显示的菜单。主要事件就是响应Click事件
在这里插入图片描述
添加contextMenuStrip
在这里插入图片描述
在这里插入图片描述

3.3 ToolTip—工具提示控件

鼠标悬停到某一菜单,提示菜单作用
添加ToolTip:将控件放到需要提示的位置

在这里插入图片描述

添加ToolTip内容
选择ToolTip放置位置的控件,
在这里插入图片描述

4、ToolStrip控件

  • 工具栏是另一种获取应用程序主要功能的常用方法,比起菜单栏更直观。

  • ToolStrip控件使用System.Windows.Forms.ToolStrip类提供的,作用是创建抑郁自定义的常用工具栏,让这些工具栏支持高级用户界面和布局功能,如停靠,漂浮,带文本和图像的按钮,下拉按钮等。

  • ToolStrip控件的属性管理者控件的显示位置和显示方式,是MenuStrip控件的基础(Toolstrip属于MenuStrip的一种)
    在这里插入图片描述

      - AllowItemRecorder::值为True,在运行时允许Alt+拖动移动控件位置,否则不能移动
      - Dock:确定ToolStrip显示的位置,当None时可以拖动到需要的位置。
      ![在这里插入图片描述](https://img-blog.csdnimg.cn/ea5c9fb1333b461dbcbd2846e63771b9.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBATXd5bGRuamUyMDAz,size_20,color_FFFFFF,t_70,g_se,x_16)
    
      - LayoutStyle:ToolStrip中添加的控件的显示方式,一般配合Doke一起使用
    

在这里插入图片描述

	 - GripStyle:设置手柄的可见性

在这里插入图片描述
- Items:子集合
- ShowItemToolStrip:设置是否显示工具栏中控件提示(鼠标悬停工具栏控件上是否显示提示)

  • 在ToolStrip中可以使用许多控件,如工具栏 包含按钮、组合框、文本框。除了这些之外,工具栏还可以包含其他控件
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

5、StatusStrip控件

  • 状态栏用于显示用户状态的简短信息。
  • StatusStrip控件:由System.Windows.Forms.StatusStrip类提供,作用是在应用程序中标识对话框底部的一栏,通常用于显示应用程序当前状态的简短信息。
  • 在StatusStrip中可以使用ToolStrip中介绍的控件中的三个—ToolStripDropDownButton、ToolStripProgressBarhe和ToolStripSplitButton
  • 还有一个控件就是StatusStrip专用的,即StatusStripStatusLable,作用就是使用文本和图像向用户显示应用程序当前的状态信息。

在这里插入图片描述
在这里插入图片描述
以下代码有问题,获取的不是行号

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

        private void timer1_Tick(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = DateTime.Now.ToString();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = DateTime.Now.ToString();
        }

        private void textBox1_Click(object sender, EventArgs e)
        {
            //文本中的字符排列从第一个字符(索引值为0)开始不断持续的增加,通过索引值推算行列
            //获取当前行第一个字符所在的索引值
            int index = textBox1.GetFirstCharIndexOfCurrentLine();
            //计算行号
            int line = textBox1.GetFirstCharIndexFromLine(index)+1;
            //计算列数
            int column = textBox1.SelectionStart - index + 1;
            toolStripStatusLabel4.Text = "第" + line.ToString() + "行,第" + column.ToString() + "列";
        }

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            //文本中的字符排列从第一个字符(索引值为0)开始不断持续的增加,通过索引值推算行列
            //获取当前行第一个字符所在的索引值
            int index = textBox1.GetFirstCharIndexOfCurrentLine();
            //计算行号
            int line = textBox1.GetFirstCharIndexFromLine(index) + 1;
            //计算列数
            int column = textBox1.SelectionStart - index + 1;
            toolStripStatusLabel4.Text = "第" + line.ToString() + "行,第" + column.ToString() + "列";
        }
    }
}

任务实施

实现一个简易的窗口登录

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

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

        private void Form1_Load(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = "欢迎使用本系统,使用前请登录";
            toolStripStatusLabel2.Text = "我们的网站: 正在建设中,敬请期待";
            toolStripStatusLabel3.Text = DateTime.Now.ToString();
            //隐藏MenuStrip控件
            menuStrip1.Visible = false;
            toolStrip1.Visible = false;

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            toolStripStatusLabel3.Text = DateTime.Now.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string name = textBox1.Text;
            string pwd = textBox2.Text;
            if (name == "darly" && pwd == "darly")
            {
                MessageBox.Show("欢迎登录本系统", "登录成功");
                this.panel1.Visible = false;
                this.menuStrip1.Visible = true;
                toolStripStatusLabel1.Text = "欢迎" + name + "登录本系统";
                toolStripMenuItemShow.Enabled = true;
                toolStripMenuItemHid.Enabled = false;
            }
            else
            {
                MessageBox.Show("用户名或密码错误,请重新输入", "登录失败");
                textBox1.Clear();
                textBox2.Clear();
                textBox1.Focus();
            }
        }

        private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int maxval = 0;
            foreach (Form myform in this.MdiChildren)
            {
                int index =Convert.ToInt32( myform.Text.Substring(4, 1));
                if (index > maxval)
                {
                    maxval = index;
                }
            }
            registerForm myRegisterForm = new registerForm();
            myRegisterForm.MdiParent = this;
            myRegisterForm.Text = "登记信息" + Convert.ToString(++maxval);
            myRegisterForm.BringToFront();
            myRegisterForm.Show();
        }

        private void 关闭当前窗体CToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form myactiveForm = this.ActiveMdiChild;  //确定当前窗口
            myactiveForm.Close();
        }

        private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("退出请注意保存好所有文件\n请确认是否退出,退出请按是,取消请按否", "退出提示", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes) this.Close();
          
        }

        private void toolStripMenuItemCas_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void toolStripMenuItemVer_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void toolStripMenuItemHor_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void toolStripMenuItemShow_Click(object sender, EventArgs e)
        {
            toolStrip1.Visible = true;
            toolStripMenuItemHid.Enabled = true;
            toolStripMenuItemShow.Enabled = false;
        }

        private void toolStripMenuItemHid_Click(object sender, EventArgs e)
        {
            toolStrip1.Visible = false;
            toolStripMenuItemHid.Enabled =false;
            toolStripMenuItemShow.Enabled = true;
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            int maxval = 0;
            foreach (Form myform in this.MdiChildren)
            {
                int index = Convert.ToInt32(myform.Text.Substring(4, 1));
                if (index > maxval)
                {
                    maxval = index;
                }
            }
            registerForm myRegisterForm = new registerForm();
            myRegisterForm.MdiParent = this;
            myRegisterForm.Text = "登记信息" + Convert.ToString(++maxval);
            myRegisterForm.BringToFront();
            myRegisterForm.Show();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            Form myactiveForm = this.ActiveMdiChild;  //确定当前窗口
            myactiveForm.Close();
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("退出请注意保存好所有文件\n请确认是否退出,退出请按是,取消请按否", "退出提示", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes) this.Close();
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void toolStripMenuItemAbout_Click(object sender, EventArgs e)
        {
            MessageBox.Show("本软件有我要自学网出品","系统介绍");
        }
    }
}

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 任务实施
{
    public partial class registerForm : Form
    {
        public registerForm()
        {
            InitializeComponent();
        }

        private void registerForm_Load(object sender, EventArgs e)
        {

        }

        string race;
        string language;
        private void button1_Click(object sender, EventArgs e)
        {
            foreach (Control outctrl in panel1.Controls)
            {
                if (outctrl is RadioButton)
                    if (((RadioButton)outctrl).Checked)
                        race = outctrl.Text;
            }
            foreach (Control outctrl in panel2.Controls)
            {
                if (outctrl is RadioButton)
                    if (((RadioButton)outctrl).Checked)
                        language = outctrl.Text;
            }
            MessageBox.Show("你登记的信息是:\n姓名:" + textBox1.Text + "\n单位:" + textBox2.Text + "性别:" + comboBox1.SelectedItem + "\n年龄:" + numericUpDown2.Value + "\n种族:" + race + "\n最喜欢的语言:" + language, "感谢你的注册");

        }
    }
}

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值