Commodore
Commodore
607 views

Triggering Keyboard Events Programmatically in WinForm

Jump to solution

I currently have a rich textbox control on my Windows Form which allows the user to overwrite text when the Insert Key is pressed.  Is there a way to trigger the insert key control so that the user does not have to manually hit the insert key in order to overwrite text? I looked at the following link:  https://docs.microsoft.com/en-us/dotnet/framework/winforms/how-to-simulate-mouse-and-keyboard-events-in-code

 

The C# code to accomplish this is :

private void Form1_DoubleClick(object sender, EventArgs e)

{

    SendKeys.Send("{INSERT}");

}

 

In Managed COBOL I tried the following when the form loads:

set self::System.Windows.Forms.SendKey("INSERT")

 

I also tried the same code for the control as follows:

set rchTextBox1::KeyUp to type System.Windows.Forms.SendKey("INSERT")

 

Neither code compiles as they give me errors.

0 Likes
1 Solution

Accepted Solutions
Micro Focus Expert
Micro Focus Expert

I believe you would have to send the INSERT key every time you enter the RichTextBox. Of course if the user manually presses Insert then it will toggle the behavior.

The following works for me for a form on which there are 2 RichTextBox controls.

       method-id RichTextBox1_Enter final private.
       procedure division using by value sender as object e as type System.EventArgs.
               invoke type SendKeys::Send("{INSERT}")
       end method.

       method-id RichTextBox2_Enter final private.
       procedure division using by value sender as object e as type System.EventArgs.
               invoke type SendKeys::Send("{INSERT}")
       end method.
      

View solution in original post

0 Likes
4 Replies
Micro Focus Expert
Micro Focus Expert

I believe you would have to send the INSERT key every time you enter the RichTextBox. Of course if the user manually presses Insert then it will toggle the behavior.

The following works for me for a form on which there are 2 RichTextBox controls.

       method-id RichTextBox1_Enter final private.
       procedure division using by value sender as object e as type System.EventArgs.
               invoke type SendKeys::Send("{INSERT}")
       end method.

       method-id RichTextBox2_Enter final private.
       procedure division using by value sender as object e as type System.EventArgs.
               invoke type SendKeys::Send("{INSERT}")
       end method.
      

View solution in original post

0 Likes
Commodore
Commodore
This worked, thank you!
0 Likes
Micro Focus Expert
Micro Focus Expert

I created a video awhile back which covers the basics of using the .NET Framework with Visual COBOL including how to convert a simple C# example to OO COBOL.

You can find this video here:

You mentioned that you were getting errors on the syntax that you tried.

I would like to explain why you received errors.

         set self::System.Windows.Forms.SendKey("INSERT")

the predefined object SELF refers to the currently running object which in a Windows Forms application would be the instance of the current form or control itself. You can use the self:: syntax only to access the properties or the methods that actually exist within the current form or control or its parent classes.

System.Windows.Forms.SendKeys is a class which is part of the .NET Framework and its documentation can be found here.  This explains that the SendKeys class contains a static method called Send that is used to simulate a keystroke.

You invoke a method in Visual COBOL using the invoke statement. If you are invoking a static method such as Send then you would need to preface the class name with the type keyword to indicate that the name is a class and not an object. In OO COBOL the double-colon operator :: is used to separate a class or object from its method and property names. in C# the period is used for this.

Invoking the Send method in the SendKeys class and passing the parameter {INSERT}       

C# example
      SendKeys.Send("{INSERT}");

COBOL example:

invoke type SendKeys::Send("{INSERT}")

For your second try you used:

set rchTextBox1::KeyUp to type System.Windows.Forms.SendKey("INSERT")

KeyUp is an event which can trigger an event handler method when it is fired. You cannot set an event to a value but you can fire an event using the invoke statement. You can use the set statement to set a property value in an object or class or to set another data item or object to the return value of a property or method.

There are a number of demo programs available in the Samples Browser under the category Language Reference for .NET COBOL that may help you become better acquainted with the syntax used.

Commodore
Commodore
Thank you for the resources as it is much appreciated!
0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.