201_COS_Some input validation techniques

advertisement
Input of Data and Validation Techniques
A number of ways exist for determining if the data a user has entered fits within a certain range or
type.
We can limit the ways the user can enter data by using the controls that are already available, such
as combo and list boxes, check and radio buttons and scroll bars.
Inputting and validating Numbers (integers)
Let’s look at using a vertical scrollbar.
‘Scroll bars’ are controls that allow the user to change a variable using the mouse or cursor keys
rather than typing a value into a text box.
Drag 2 labels and a vertical scrollbar (vscrollbar) onto the Form.
The first label gives instructions.
The second label we name lblNumber, autosize: false, backcolor:yellow.
The vertical scrollbar we give the following properties:
Name: vsbNumber
Minimum and Maximum define the starting and finishing values of the scroll bar.
Set the properties as: Minimum to 4 and Maximum to 12.
The Value property holds the current value of the scroll bar.
SmallChange sets the minimum amount the bar will move when the up or down arrows are clicked.
Set SmallChange and LargeChange to 1.
Double click the vertical scrollbar.
And add the code into the Scroll method as shown below.
private void vsbNumber_Scroll(object sender, ScrollEventArgs e)
{
lblNumber.Text = vsbNumber.Value.ToString();
}
Using a ListBox for controlling Input
Set up the Form as below:
double click the form and add code to get:
private void Form1_Load(object sender, EventArgs e)
{
//load numbers 4 to 12 into the listbox
for (int i = 4; i < 13; i++)
{
lstNumber.Items.Add(i);
}
On the Form double click the button and add code to get:
private void button1_Click(object sender, EventArgs e)
{
lblNumber.Text = lstNumber.SelectedItem.ToString();
}
Using a Textbox to Input a Number (and a string of numbers to check against)
(array plus Textbox)
We set up a string and if the number entered in the textbox matches one of the valid numbers the
program continues. Otherwise the user is asked to resubmit the number choice.
The code:
public partial class Form1 : Form
{
string[] valid_number = { "4", "5", "6", "7", "8" };
int number;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (!valid_number.Contains(textBox1.Text))
{
MessageBox.Show("Please enter a number from 4 to 8 only", "Error");
textBox1.Clear();//clear the textbox
textBox1.Focus();//cursor back in textbox
}
else
{
//convert string in textbox to a number
number = int.Parse(textBox1.Text);
//Display number in second textbox
textBox2.Text = number.ToString();
}
}
Validating the data input into a text box using the Validating event of a TextBox
public partial class Form1 : Form
{
int numberEntered;//declare variable to hold the number entered
public Form1()
{
InitializeComponent();
}
private void txtNumber_Validating(object sender, CancelEventArgs e)
{
//code checks to see if text entered is an integer
// if NOT prompts user to enter an integer, clears and focuses on the textbox
// if text entered is an integer it is checked to see if it is between 4 and 12
// if it is NOT between 4 and 12 prompt user to enter a number between 4 and 12
//
if (int.TryParse(txtNumber.Text, out numberEntered))
{
if (numberEntered < 4 || numberEntered > 12)
{
MessageBox.Show("You have to enter a number between 4 and 12
inclusive");
txtNumber.Clear();
txtNumber.Focus();
}
}
else
{
MessageBox.Show("You need to enter an integer");
txtNumber.Clear();
txtNumber.Focus();
}
}
private void button1_Click(object sender, EventArgs e)
{
lblNumber.Text = numberEntered.ToString();
}
}
Validating textbox input using the textChanged event and isLetter
Set up the form as above. First textbox is called txtName and second is txtNext
Select txtName and in events doubleclick textChanged and add code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//this event runs when the text is changed in txtName
private void txtName_TextChanged(object sender, EventArgs e)
{
string context = this.txtName.Text;
bool isletter = true;
//for loop checks for letters as characters are entered
for (int i = 0; i < context.Length; i++)
{
if (!char.IsLetter(context[i]))// if current character not a letter
{
isletter = false;//make isletter false
break; // exit the for loop
}
}
// if not a letter clear the textbox and focus on it
// to enter name again
if (isletter==false)
{
txtName.Clear();
txtName.Focus();
}
}
}
Validating input using keypress and error provider
Set up the form as above. First textbox is called txtName and second is txtNext
From the toolbox drag error provider onto the form.
Select txtName and in events doubleclick Keypress event.
Then select txtName again and in events doubleclick leave.
Finished code:
//letters are accepted and backspace to revise the name
if (!char.IsLetter(e.KeyChar) && Convert.ToInt32(e.KeyChar) != 8)
// 8 is code for backspace
{
//message in error provider
errorProvider1.SetError(txtName, "Only letters allowed");
e.Handled = true;// keypress event handled
txtName.Focus();//cursor back in txtName
}
else
{
errorProvider1.Clear();
}
}
//when user leaves txtname we focus on the next textbox
//and disable txtName to prevent access
private void txtName_Leave(object sender, EventArgs e)
{
txtNext.Focus();
txtName.Enabled = false;
}
Download