-1

I have this C# code and I want to change the Text of a label named label1. When I am using these functions to detect what key is pressed and change the label1.Text from within the function, the text is unchanged. How can I change the label1.Text from within this function?

private void Form1_Load(object sender, EventArgs e)
{
    _hookID = SetHook(_proc);
    label1.Font = new Font("Arial", 24, FontStyle.Bold);
}

private static const int WH_KEYBOARD_LL = 13;
private static LowLevelKeyboardProc _proc = HookCallback;
private static IntPtr _hookID = IntPtr.Zero;

private static IntPtr SetHook(LowLevelKeyboardProc proc)
{
    using (Process curProcess = Process.GetCurrentProcess())
    using (ProcessModule curModule = curProcess.MainModule)
    {
        return SetWindowsHookEx(WH_KEYBOARD_LL, proc,
            GetModuleHandle(curModule.ModuleName), 0);
    }
}

private delegate IntPtr LowLevelKeyboardProc(
    int nCode, IntPtr wParam, IntPtr lParam);

private static IntPtr HookCallback(
    int nCode, IntPtr wParam, IntPtr lParam)
{

    if (nCode >= 0 && wParam == (IntPtr)WM_KEYUP)
    {
        int vkCode = Marshal.ReadInt32(lParam);
        Form1 f = new Form1();
        f.label1.Text = "Changed Label"; //<-------------------- change label here
    }

    return CallNextHookEx(_hookID, nCode, wParam, lParam);
}

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook,
    LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
    IntPtr wParam, IntPtr lParam);

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
Dour High Arch
  • 21,513
  • 29
  • 75
  • 90
Vil Ignoble
  • 41
  • 1
  • 2
  • 10
  • Not sure what are you trying to do here, but you create a new instance of Form1 then use that instance to set the label there. However that new instance is never shown so you don't see anything. If you are trying to change the current instance of Form1 you need a reference to it inside that static method. – Steve Jan 02 '15 at 22:53
  • All I am trying to do is make the label1 text change I have an instance of form 1 in the function, but it does not chnage the label1.Text – Vil Ignoble Jan 02 '15 at 22:55
  • With Windows Forms you'll have to prompt the UI to be updated. http://stackoverflow.com/questions/1360944/force-gui-update-from-ui-thread – jtimperley Jan 02 '15 at 22:57
  • Just tried that it is still not changing, the text will chnage if called from form1_load but not fromwithin the keyup function what blocks it? – Vil Ignoble Jan 02 '15 at 23:01

1 Answers1

1

You have several mistakes:

private static LowLevelKeyboardProc _proc = HookCallback;
private static IntPtr _hookID = IntPtr.Zero;

private static IntPtr HookCallback(

You're trying to assign something that won't exist until runtime inside a static class method. That is not going to work; you need to do something like:

private LowLevelKeyboardProc _proc;
private IntPtr _hookID;

private IntPtr HookCallback(...

private void Form1_Load(object sender, EventArgs e)
{
    _proc = new LowLevelKeyboardProc(HookCallback);
    _hookID = SetHook(_proc);

Learn what static means and do not use it unless you absolutely have to. It doesn't do what you seem to think it does.

Next:

Form1 f = new Form1();
f.label1.Text = "Changed Label"; 

You are creating a completely new copy of your form, f, changing a label inside the copy, then throwing away the copy without displaying or doing anything at all with it. You want this:

label1.Text = "Changed Label"; 
Dour High Arch
  • 21,513
  • 29
  • 75
  • 90