Button, Label and TextBox Controls

Button:

used for taking acceptance from a user to perform an action.

Label:

used for displaying static text on the UI.

TextBox

used for taking text input from the user, this control can be used in 3 different ways:

  • Single Line Text (d)
  • Multi Line Text (Text Area)
  • Password Field

  • The default behavior of the control is single line text; to make it multiline set the property Multiline of the control as true. By default the text area will not have any scroll bars to navigate up and down or left and right, to get them set the ScrollBars property either as Vertial or Horizontal or Both, default is none.


    Note: by default the WordWrap property of the control is set as true disabling horizontal scroll bar so set it as false to get horizontal scroll bar.


    To use the control as a password field either set the PasswordChar property of control with a character we want to use as Password Character like * or # or $ or @ etc., or else set the UseSystemPasswordChar property as true which indicates the text in the control should appear as the default password character.

    To work with the above 3 controls add a new form in the project, design it as following, set the Name property of the TextBox’s as txtName, txtPwd, txtCPwd, txtPhone and txtAddress and set the Name property of the Button’s as btnSave, btnClear and btnClose.



    Setting Tab Order of Form Controls

    While working with a windows application we navigate between controls using the Tab key of key board. By default Tab key navigates the focus between controls in the same order how they are placed on form. If we want to set the sequence of tab on our own, it can be done by setting the “Tab Order”. To do this go to View Menu and select Tab Order which shows the current tab order, now click on each control in a sequence how we want to move the tab, again go to view menu and select Tab Order.


    In the above form check the following business rules

  • Check the user name, password and confirm password fields to be mandatory.
  • Check password characters to be ranging between 8 to 16 characters.
  • Check confirm password to be matching with password.
  • Put the save button in disable state and enable it only if the above 3 rules are satisfied.
  • Check phone TextBox to accept numeric values and back spaces only.
  • Allow users to close the form if required at any time.

  • To perform the first 4 validations first set the enabled property of save button as false so that it will be in disabled state, then set the MaxLength property of both password textbox's to 16 so that they will accept only 16 chars, then define a Validating event procedure for user name textbox and bind that event procedure with both the password textbox's also. Now write the following code under the event procedure:


       TextBox tb = sender as TextBox;
    if (tb.Text.Trim().Length == 0) {
    	MessageBox.Show("Cannot leave empty.");
    	e.Cancel = true;
    	return;
    }
    if (tb.Name != "txtName") {
    	if (tb.Text.Trim().Length < 8) {
    		MessageBox.Show("Password should be between 8 to 16 chars");
    		e.Cancel = true;
    		return;
    	}
    }
    if (tb.Name == "txtCPwd") {
    	if (txtPwd.Text.Trim() != txtCPwd.Text.Trim()) {
    		MessageBox.Show("Confirm password should match with password");
    		return;
    	}
    	btnSave.Enabled = true;
    }
        

  • Validating event occurs when the focus is leaving the control and validates the content entered in the control.
  • Some events are associated with properties with them e.g.: Validating, KeyPress etc., if we want to consume the properties of an event under its event procedure we can make use of the parameter "e" of the event procedure which refers to properties of current executing event.
  • In above case Cancel is a property of validating event when set as true restricts the focus not to leave the control.
  • To close the form even from mandatory fields go to properties of Close Button and set its 'Causes Validation' property as false so that code under that button gets executed before the execution of any other controls validating event. Now write the following code under Close Button Click Event

  • txtName.CausesValidation = false;
    txtPwd.CausesValidation = false;
    txtCPwd.CausesValidation = false;
    this.Close();
    

    When we set CausesValidation = false for TextBox it will restrict validating event of the control not to occur so that focus leaves the textbox and form gets closed.


    If we want the Phone No TextBox to accept only numeric values and back spaces define a KeyPress event procedure for Phone No TextBox and write following code in it:


        if (char.IsDigit(e.KeyChar) == false && Convert.ToInt32(e.KeyChar) != 8) {
    	MessageBox.Show("Enter numerics only");
    	e.Handled = true;
    }
    

  • KeyPress event occurs when we press and release a key while the focus is in the control.
  • KeyChar property of KeyPress gets the key value corresponding to the key pressed.
  • Handled property when set as true will restrict the key value to enter into the control.
  • Char.IsDigit(char) will return true if the given char is a numeric or else returns false.
  • Convert.ToInt32(char) will return ascii value of the given character

  • Code under Save Button Click Event Procedure

    	MessageBox.Show("Data saved to database");
    

    Code under Clear Button Click Event Procedure

    	MessageBox.Show("Data saved to database");
    

    Code under Clear Button Click Event Procedure


    foreach(Control ctrl in this.Controls) {
    	if(ctrl.GetType().Name == "TextBox") {
    		TextBox tb = ctrl as TextBox;
    		tb.Clear();
    	}
    }
    btnSave.Enabled = false;
    txtName.Focus();