winform控件statusStrip1上的toolStripStatusLabel1如何靠右对齐

18 篇文章 1 订阅

以下代码为正确实现。

this.statusStrip1.LayoutStyle = System.Windows.Forms.ToolStripLayoutStyle.HorizontalStackWithOverflow;
            this.toolStripStatusLabel1.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right;
            this.toolStripStatusLabel1.Text = "xxxx © 2019-"+System.DateTime.Today.Year.ToString()+" 信息中心 设计开发";

添加转载:转帖:https://blog.csdn.net/cdhql/article/details/6119774

[C#]解决statusStrip控件上的项目不能靠右对齐的问题

在c#中用到了状态栏控件StatusStrip,但当我想把StatusStrip上某个StatusLabel靠右对齐时出了问题。  

按照MSDN中的办法,是设置ToolStripStatusLabel的Alignment属性为Right。不过我在设计界面的属性窗口中找不到Alignment。

就算加入代码toolStripStatusLabel2.Alignment = ToolStripItemAlignment.Right; 也还是没什么效果。

 

后来我找到两种方法解决这个问题:

方法一:

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

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

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

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

 

方法二:

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

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

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

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

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

 

问题是基本解决了,只是我还是不知道为什么在属性窗口里ToolStripStatusLabel没有Alignment属性。从网上搜集的帖子看,好像其他人都没这个问题啊?

有谁知道的话告诉我。对了,我用的是VS2005 版本8.0.50727.42。

以下转自:https://www.cnblogs.com/xiaowie/p/8624387.html

态栏用于显示用户状态的简短信息。

 

StatusStrip控件是由system.windows.forms.statusStrip类提供,作用是在应用程序中标识对话框底部的一栏,通常用于显示应用程序当前状态的简短信息。

 

在StatusStrip中可以使用toolstrip中介绍的控件中的三个----Toolstripdropdownbutton、toolstripprogressbar、toolstripSplitbutton

 

还有一个控件就是StatusStrip专用的,即StatusStripStatuslabel,作用就是使用文本和图像用户显示应用程序当前状态的信息。

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

namespace StatusStrip状态栏控件

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void Form1_Load(object sender, EventArgs e)

        {

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

        }

 

        private void timer1_Tick(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);

 

            //计算列数

 

            int column = textBox1.SelectionStart - index + 1;

            //如 textbox索引为 10 11 12 13 14 15 16  比如鼠标选择的是16 怎么计算是多少列呢

            //那index=10  column=selectionstart(当前选择起点16)-index(第一个索引值)+1=第7列

 

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

        }

        //键盘弹起事件

        private void textBox1_KeyUp(object sender, KeyEventArgs e)

        {

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

            int index = textBox1.GetFirstCharIndexOfCurrentLine();

 

            //计算行号

            int line = textBox1.GetFirstCharIndexFromLine(index);

 

            //计算列数

 

            int column = textBox1.SelectionStart - index + 1;

            //如 textbox索引为 10 11 12 13 14 15 16  比如鼠标选择的是16 怎么计算是多少列呢

            //那index=10  column=selectionstart(当前选择起点16)-index(第一个索引值)+1=第7列

 

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

        }

  

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值