Wednesday, September 1, 2010
virtual keyboard in C#
//Add a user control in the project and write the code below.
//Create all buttons on the control and make same event (Click Event) for all the buttons except 3 buttons (like backspace,clear and caps lock)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Forms;
namespace Virtual_KeyBoard
{
public partial class ucKeyboard : UserControl
{
public Form1 frmLogin = null;
Random rnd = new Random();
Button[] numericButtons = new Button[10];
Button[] alphaButtons = new Button[36];
public ucKeyboard()
{
InitializeComponent();
}
public bool Flag = false; int i = 0;
public void NumericButtonLocation()
{
foreach (Control ctrl in this.Controls)
{
if (ctrl is Button && ctrl.TabIndex <10)
{
if (ctrl.Text != "Caps Lock" && ctrl.Text != "BackSpace" && ctrl.Text != "Clear")
numericButtons[ctrl.TabIndex] = ctrl as Button;
}
}
for (int j = 0; j < 9; j++)
{
int selectedButtonIndex = rnd.Next(1,9);
int next = rnd.Next(1, 9);
swapNumeric(selectedButtonIndex, next);
}
}
private void swapNumeric(int btn1X, int btn1Y)
{
string temp = numericButtons[btn1X].Text;
numericButtons[btn1X].Text = numericButtons[btn1Y].Text;
numericButtons[btn1Y].Text = temp;
}
private void swapAlphabets(int btn1, int btn2)
{
string temp = alphaButtons[btn1].Text;
alphaButtons[btn1].Text = alphaButtons[btn2].Text;
alphaButtons[btn2].Text = temp;
}
public void AlphabetButtonLocation()
{
foreach (Control ctrl in this.Controls)
{
if (ctrl is Button && ctrl.TabIndex >10 && ctrl.TabIndex < 36)
{
alphaButtons[ctrl.TabIndex] = ctrl as Button;
}
}
for (int j = 0; j < 27; j++)
{
int selectedButtonIndex = rnd.Next(11, 35);
int next = rnd.Next(11, 35);
swapAlphabets(selectedButtonIndex, next);
}
}
private void button_clear_Click(object sender, EventArgs e)
{
frmLogin.nVirtualBox.Text = string.Empty;
}
private void button_backspace_Click(object sender, EventArgs e)
{
if (frmLogin.nVirtualBox.Text != "")
frmLogin.nVirtualBox.Text = frmLogin.nVirtualBox.Text.Remove(frmLogin.nVirtualBox.Text.Length - 1, 1);
else
return;
}
private void btnVirtualKey_Click(object sender, EventArgs e)
{
Button b = sender as Button;
if (!Flag)
{
frmLogin.display(b.Text.ToLower());
}
else
{
frmLogin.display(b.Text.ToUpper());
}
}
private void btnCapsLock_Click(object sender, EventArgs e)
{
if (i == 0)
{
Flag = true;
i++;
for (int j = 0; j < this.Controls.Count; j++)
{
Control ctl = (Button)this.Controls[j];
if (ctl != btnCapsLock && ctl != button_backspace && ctl != button_clear)
{
this.Controls[j].Text = this.Controls[j].Text.ToUpper();
}
}
}
else
{
i = 0;
Flag = false;
for (int j = 0; j < this.Controls.Count; j++)
{
Control ctl = (Button)this.Controls[j];
if (ctl != btnCapsLock && ctl != button_backspace && ctl != button_clear)
{
this.Controls[j].Text = this.Controls[j].Text.ToLower();
}
}
}
}
private void ucKeyboard_Load(object sender, EventArgs e)
{
NumericButtonLocation();
AlphabetButtonLocation();
}
}
}
// Now in the form (Form1) put the code below
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Virtual_KeyBoard
{
public partial class Form1 : Form
{
public static Form1 frm = null;
public TextBox nVirtualBox;
//Creat instance of form if no one is created otherwise set that instance available.
public Form1 getreference()
{
if (frm == null)
frm = new Form1();
return this;
}
public Form1()
{
InitializeComponent();
}
//set a virtualTextbox for any of the selected textbox on runtime.
public void display(string str)
{
if (nVirtualBox != null)
{
nVirtualBox.Text += str;
}
}
// To load again the new keyboard with different locations of all buttons.
// it will attached with the uckeyboard load event.
private void ucKeyboard1_Load(object sender, EventArgs e)
{
ucKeyboard1.NumericButtonLocation();
ucKeyboard1.AlphabetButtonLocation();
this.ucKeyboard1.frmLogin = this;
}
private void txtboxfirst_Enter(object sender, EventArgs e)
{
nVirtualBox = sender as TextBox;
if (nVirtualBox == null)
nVirtualBox = txtboxfirst;
}
private void ucKeyboard1_Load_1(object sender, EventArgs e)
{
this.ucKeyboard1.frmLogin = this;
}
}
}
Subscribe to:
Post Comments (Atom)
Nice
ReplyDeleteI am new to development, currently I am assigned with this task.... I was able to understand other things, but am not clear of txtboxfirst_enter()... can u explain how it is triggered...
ReplyDeleteNice goood job
ReplyDelete