How to design a keypad in C#?
-
I am new to C# and I am planing to design my own keypad but I don't know how/where to start, I have 4 textBoxes the keypad buttons. The first problem came into my mind was: how can I detect the cursor location (which textBox is the cursor in?). So for example if I had only one textbox then it is easy I could write inside button1 : textBox1.text = "1" and inside button2 : textBox1.text = "2" and inside button_A : textBox1.text = "A".... and so on but I have 4 textBoxes and it is confusing. Can you please provide me with an idea or what to write inside each button to print its value in the textbox which the cursor is in. Thank you professionals.
-
I am new to C# and I am planing to design my own keypad but I don't know how/where to start, I have 4 textBoxes the keypad buttons. The first problem came into my mind was: how can I detect the cursor location (which textBox is the cursor in?). So for example if I had only one textbox then it is easy I could write inside button1 : textBox1.text = "1" and inside button2 : textBox1.text = "2" and inside button_A : textBox1.text = "A".... and so on but I have 4 textBoxes and it is confusing. Can you please provide me with an idea or what to write inside each button to print its value in the textbox which the cursor is in. Thank you professionals.
edit #1 All you need to do to keep the Focus on the TextBox Control when a Button is clicked is to set the TextBox 'Capture property to 'true in the Click EventHandler. Example:
private void button1_Click(object sender, EventArgs e)
{
CurrentTextBox.Text += "A";
CurrentTextBox.Capture = true;
}See below for how to keep the 'CurrentTextBox field up to date as the user changes which TextBox they use. end edit #1 The word "keypad" suggests to me a bunch of Buttons that you click on, and things happen. I am not clear what you are doing with the TextBoxes here; please clarify. In WinForms the 'ActiveControl property of the ContainerControl Class will always return which Control has Focus in the ContainerControl (which could be Form, a Panel, etc.) [^].
Control activeFormControl = this.ActiveControl; // of the Form Control activePanelControl = panel1.ActiveControl; // of a Panel
A way I often use to keep track of which of several TextBoxes has Focus currently, or has had Focus most recently is: wire all of them up to the same 'Enter EventHandler, and:TextBox CurrentTextBox;
private void TextBoxes1To4_Enter(object sender, EventArgs e)
{
CurrentTextBox = sender as TextBox;// remove when testing is complete if(CurrentTextBox == null) { throw new ArgumentNullException("Illegal use TextBox Enter"); } // example of code that will do something when TextBox is entered switch (CurrentTextBox.Name) { case "textBox1": // do something specific to textBox1 getting Focus, being Acive // example: textBox1.Text = "Entered"; break; case "textBox2": break; case "textBox3": break; case "textBox4": break; }
}
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
-
I am new to C# and I am planing to design my own keypad but I don't know how/where to start, I have 4 textBoxes the keypad buttons. The first problem came into my mind was: how can I detect the cursor location (which textBox is the cursor in?). So for example if I had only one textbox then it is easy I could write inside button1 : textBox1.text = "1" and inside button2 : textBox1.text = "2" and inside button_A : textBox1.text = "A".... and so on but I have 4 textBoxes and it is confusing. Can you please provide me with an idea or what to write inside each button to print its value in the textbox which the cursor is in. Thank you professionals.
Clicking the button will cause the cursor to leave any textbox it is in. You can determine which textbox the cursor is in (or "was" in) by hooking up the "Leave" event for each textbox and storing the "sender" (i.e. textbox) for each event in a common field / property. (Note that all the textboxes can use the same event handler). e.g.
private TextBox _tb = null;
this.textBox1.Leave += new System.EventHandler(this.textBox_Leave);
this.textBox2.Leave += new System.EventHandler(this.textBox_Leave);
this.textBox3.Leave += new System.EventHandler(this.textBox_Leave);private void textBox_Leave( object sender, EventArgs e ) {
_tb = sender as TextBox;
}private void button1_Click( object sender, EventArgs e ) {
if ( _tb != null ) {
MessageBox.Show( "The last TextBox was: " + _tb.Name );
}
}(The "Enter" event can be used to record where the cursor currently is as long as there is no other UI activity that will move the cursor; i.e. a button click).
-
I am new to C# and I am planing to design my own keypad but I don't know how/where to start, I have 4 textBoxes the keypad buttons. The first problem came into my mind was: how can I detect the cursor location (which textBox is the cursor in?). So for example if I had only one textbox then it is easy I could write inside button1 : textBox1.text = "1" and inside button2 : textBox1.text = "2" and inside button_A : textBox1.text = "A".... and so on but I have 4 textBoxes and it is confusing. Can you please provide me with an idea or what to write inside each button to print its value in the textbox which the cursor is in. Thank you professionals.
Hi If You are new to C# it wont be easy. First of all You can not use buttons, because when You click a button that button will steal focus, so your key stroke will have no notion where to go. The only (winforms)control that not steals focus is a label, but a label has not got the right properties to act as a key. The solution is to create a custom control derived from label control. to do so You must add this class to your project:
namespace YOURPROJECTNAMESPACE
{
public sealed class MyControl : Label
{
public MyControl()
{
// Allow transparent background
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
// Default to transparent background
BackColor = Color.Transparent;
}public Image ControlImage { get; set; } protected override void OnPaint(PaintEventArgs e) { // Draw image, sized to control if (ControlImage != null) { e.Graphics.DrawImage(ControlImage, 4, 4, Width - 8, Height - 8); } // Draw text, centered vertically and horizontally var rectangle = new RectangleF(2, 0, Width, Height + 5); var format = new StringFormat { LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center }; e.Graphics.DrawString(Text, Font, Brushes.Black, rectangle, format); } }
}
After you have done this You will find in the designer toolbox MyControl. Then drag a flowLayouPanel from the toolbox. Populate this panel by dragging as many MyControls as you need. in properties give them a nice picture of a key and a character as text. One more thing to do, In the designer doubleclick each key,and in the click event this:
private void myControl1_Click(object sender, EventArgs e)
{
SendKeys.Send("{1}");
}private void myControlEnter_Click(object sender, EventArgs e)
{
SendKeys.Send("{ENTER}");
}Etc. If You have any more questions just ask, Groover
0200 A9 23 0202 8D 01 80 0205 00
-
Hi If You are new to C# it wont be easy. First of all You can not use buttons, because when You click a button that button will steal focus, so your key stroke will have no notion where to go. The only (winforms)control that not steals focus is a label, but a label has not got the right properties to act as a key. The solution is to create a custom control derived from label control. to do so You must add this class to your project:
namespace YOURPROJECTNAMESPACE
{
public sealed class MyControl : Label
{
public MyControl()
{
// Allow transparent background
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
// Default to transparent background
BackColor = Color.Transparent;
}public Image ControlImage { get; set; } protected override void OnPaint(PaintEventArgs e) { // Draw image, sized to control if (ControlImage != null) { e.Graphics.DrawImage(ControlImage, 4, 4, Width - 8, Height - 8); } // Draw text, centered vertically and horizontally var rectangle = new RectangleF(2, 0, Width, Height + 5); var format = new StringFormat { LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center }; e.Graphics.DrawString(Text, Font, Brushes.Black, rectangle, format); } }
}
After you have done this You will find in the designer toolbox MyControl. Then drag a flowLayouPanel from the toolbox. Populate this panel by dragging as many MyControls as you need. in properties give them a nice picture of a key and a character as text. One more thing to do, In the designer doubleclick each key,and in the click event this:
private void myControl1_Click(object sender, EventArgs e)
{
SendKeys.Send("{1}");
}private void myControlEnter_Click(object sender, EventArgs e)
{
SendKeys.Send("{ENTER}");
}Etc. If You have any more questions just ask, Groover
0200 A9 23 0202 8D 01 80 0205 00
Well, it can be easy: all you need to do to keep the Focus on the TextBox Control when a Button is clicked is to set the TextBox 'Capture to 'true in the Click EventHandler.
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.