分享

StatusStrip状态栏控件

 潘仕城 2021-02-17

StatusStrip控件:状态栏控件。通常处于窗体的最底层,用于显示窗体上的对象的相关信息,或者显示应用程序的信息。

包含:StatusLabel、progressBar、DropDownButton、splitButton控件。可以显示文本、图标或者同时显示这两者。

使用例子: this.toolStripStatusLabel2.Text = DateTime.Now.ToShortDateString();//在任务栏上显示系统的当前日期

C# winform设置状态栏(statusStrip)

设置状态栏中的项显示在右下角以及添加分隔符

方法一:

在状态栏所有项目(StatusLabel、ProgressBar、DropDownButton等)前添加一个空白的StatusLabel (Text属性为空),并将其Spring属性设为True。

  

Spring属性的作用是设置该项是否填满剩余空间,设为True以后,当程序运行时后面的项就都挤到右边,实现靠右对齐了。

如果更进一步,需要一部分项靠左,一部分靠右,那就在两部分中间插入空白StatusLabel,同时设其Spring属性为True。

这种方法比较简单,不用手工添加代码。首选!

方法二:

这个方法是我无意中发现的。

设置StatusStrip控件的LayoutStyle属性为HorizontalStackWithOverflow 或 StackWithOverflow。

然后在代码中修改状态栏上某项的Alignment为Right,这次就有靠右的效果了。

例如: this.toolStripStatusLabel1.Alignment = ToolStripItemAlignment.Right;

注意如果是多个项,那靠左对齐的从左往右排列,靠右对齐的从右往左排列。

添加分隔符:

statusStrip.Items.Insert(2, new ToolStripSeparator());

添加分隔符

设它的bordersides属性

实例代码如下

插入timer控件设置属性如下图

  

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 _12._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)

        {

            //获取当前行第一个字符所在的索引值

            int index = textBox1.GetFirstCharIndexOfCurrentLine();

            //计算行号

            int line = textBox1.GetFirstCharIndexFromLine(index) + 1;

            //计算列数

            int column = textBox1.SelectionStart - index + 1;

            //10 11 12 13 14 15 16

            toolStripStatusLabel3.Text = "第" + line + "行,第" + column + "列";

        }

        private void textBox1_KeyUp(object sender, KeyEventArgs e)

        {

            //获取当前行第一个字符所在的索引值

            int index = textBox1.GetFirstCharIndexOfCurrentLine();

            //计算行号

            int line = textBox1.GetFirstCharIndexFromLine(index) + 1;

            //计算列数

            int column = textBox1.SelectionStart - index + 1;

            //10 11 12 13 14 15 16

            toolStripStatusLabel3.Text = "第" + line + "行,第" + column + "列";

        }

    }

}

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多