Files
VS_HCI/WindowsFormsPopupKeyboard/WindowsFormsApplication3/Form1.cs
2024-02-23 11:27:55 +00:00

60 lines
2.5 KiB
C#

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 WindowsFormsPopupKeyboard
{
public partial class Form1 : Form
{
Keyboard keyboard;
public Form1()
{
InitializeComponent();
}
/******************************************
* I had to hook up the textBox_GotFocus event handler method manuall in the Form's
* Designer.cs file using:
*
* this.textBox1.GotFocus += new System.EventHandler(this.textBox_GotFocus);
*
*****************************************/
private void textBox_GotFocus(Object sender, EventArgs e)
{
TextBox tb = (TextBox)sender; // take a copy of the object reference for the particular textbox pressed
label1.Text = "TextBox " + tb.Name + " GotFocus " + DateTime.Now.ToString(); // just some debugging to see which textbox has focus
if (keyboard == null) // check if the keyboard is already created
{
keyboard = new Keyboard(); // no keyboard so create an instance of one
keyboard.FormClosed += delegate
{
keyboard = null; // when the keyboard is closed, dispose of the keyboard instance
this.ActiveControl = label1; // when the keyboard is closed, reset focus to the dummy label else
// it will give focus to a textbox which would trigger the event to
// make another keyboard appear.
};
}
keyboard.setTextBoxForOutput(tb); // tell the keyboard which textbox to send its characters too
keyboard.Show(); // show the keyboard
keyboard.Left = this.Left + tb.Left + tb.Width + 30; // re-position the keyboard form to be next to
keyboard.Top = this.Top + tb.Top + tb.Height + 11; // the textbox that recieves its input.
}
private void Form1_Load(object sender, EventArgs e)
{
this.ActiveControl = label1; // when the form loads, direct focus to a label so that a textbox
// doesn't get focus by default and trigger the GotFocus event (which
// would show the keyboard before we needed it)
}
}
}