问题
I would like to add an in form numberpad to my form to make the program more touch friendly. I have multiple text boxes on this form that change focus when the enter key is pressed.
I tried SendKeys.Send("#"), but when I click the button it just changes focus to the button and does nothing inside the text box I was trying to type in.
Is there a guide or something for this? I have looked and all I can find are on screen keyboards that work outside of the form, but not inside.
回答1:
Expanding a little bit on the idea from Hans Passant you could use this as a starting point.
Form
In your form add the textboxes you need and a PictureBox. The picturebox will have an image with the characters your users need to be able to type.
Set the property Image to an image file (browse for it) (or create your own)
Set the property SizeMode to AutoSize so the complete picture is shown.
Next goto the events and add an eventhandler for MouseClick.
Code
Add the following code to the handler:
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
// how many rows and columns do we have
// in the picture used
const int maxrows = 4;
const int maxcols = 3;
// based on each position (numbered from left to right, top to bottom)
// what character do we want to add the textbox
var chars = new [] {'1','2','3','4','5','6','7','8','9', '+', '0', '-'};
// on which row and col is clicked, based on the mouse event
// which hold the X and Y value of the coordinates relative to
// the control.
var row = (e.Y * maxrows) / this.pictureBox1.Height;
var col = (e.X * maxcols) / this.pictureBox1.Width;
// calculate the position in the char array
var scancode = row * maxcols + col;
// if the active control is a TextBox ...
if (this.ActiveControl is TextBox)
{
// ... add the char to the Text.
// add error and bounds checking as well as
// handling of special chars like delete/backspace/left/right
// if added and needed
this.ActiveControl.Text += chars[scancode];
}
}
The code is I think self explanatory. Keep in mind that no error checking whatsoever is being done here.
Result
This is what the end result will look like:
来源:https://stackoverflow.com/questions/31843865/in-form-numberpad-c-sharp