简体   繁体   中英

State of Num Caps ScrollLock in StatusStrip WindowsForms

Tell me, please, how do I show in StatusStrip when CAPS_LOCK key is enabled. I tried to follow the examples: one and two but nothing is displayed in my app. I created a new project, added StripStatusLabel element and tried to bring any information to it. It is strange that display is obtained only in the initialisation method:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        toolStripStatusLabel1.Text = "111";
    }
}

BUT in other method it's doesn't work.

using System.Diagnostics;
namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            //toolStripStatusLabel1.Text = "111";
        }
        public void Form2_KeyDown(object sender, KeyEventArgs e)
        {
            Debug.Write("123");
            toolStripStatusLabel1.Text = "222";
        }
    }
}

Windows Forms. NetFramework 4.5 PS sorry for silly question :)


UPDATE: enter image description here

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;
using System.Diagnostics;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            KeyDown += tst;
        }

        public void TextBoxTest()
        {
            textBox1.Text = "onetwo";
        }

        private void tst(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode & Keys.KeyCode) == Keys.CapsLock)
            {
                if (Control.IsKeyLocked(Keys.CapsLock))
                    toolStripStatusLabel1.Text = "Caps";
            }
        }
    }
}

But output don't work. Tell me please what I doing wrong

// Caps Lock

toolStripStatusLabel1.Text=IsKeyLocked(Keys.CapsLock).toString();  

// Num Lock

toolStripStatusLabel1.Text=IsKeyLocked(Keys.NumLock).toString();

Set KeyPreview property of your form to set true write this code is key_down event of your form

Before this you can't put text in function other than IntializeComponent because KeyPreview property for your form is set to false make sure it is sure to make key down event working

I solved it:

protected override void WndProc(ref Message m)
{
    base.WndProc(ref m);
    capsStatusLabel.ForeColor = IsKeyLocked(Keys.CapsLock) ? statusStrip1.ForeColor : statusStrip1.BackColor;
    numStatusLabel.ForeColor = IsKeyLocked(Keys.NumLock) ? statusStrip1.ForeColor : statusStrip1.BackColor;
}

Thanks all!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM