Académique Documents
Professionnel Documents
Culture Documents
TextBox
TextBox t1= new TextBoX(); t1.Text=Hello; t1.Name=Input; t1.Multiline=true Appending text: t1.Text+=some text; t1.AppendText(" Appended text");
Accepts return and AcceptsTab In a Multiline TextBox control, you need to press CTRL+ENTER to create a new line. The AcceptsReturn property sets TextBox control to move to new line by simply pressing ENTER key. By default, AcceptsReturn property of a TextBox control is false. t1.AcceptsReturn = true;
TextBox
If a TextBox control is set to multiline, the AcceptsTab property is used to set so the TextBox control accepts TAB key.
If this property is not set, pressing TAB key simply move to the next control on a Form. By
default, AcceptsTab property value of a TextBox control is false.// accepts TAB key t1.AcceptsTab = true; Passoword character casing t1.PasswordChar = '$'; t1.ReadOnly = true ///for output to be displayed in textbox
TextBox properties
WordWrap
If WordWrap property is true, the text in the TextBox control automatically wraps to the next line if required.
If this property is set to true, horizontal scroll bars are not displayed regardless of the
ScrollBars property setting. t1.WordWrap = true;
ScrollBars A Multiline TextBox control can have scrollbars. The ScrollBars property of TextBox control is used to show scrollbars on a control. The ScrollBars property is represented by a ScrollBars enumeration that has four values - Both, Vertical, Horizontal, and None. control and they will be visible when the scrolling is needed on a TextBox control. dynamicTextBox.ScrollBars = ScrollBars.Both;
Textchanged event
t1.textchanged=new EventHandler(t1_TextChanged); Event handler Private void t1_TextChanged(object s,EventArgs e) { TextBox t2=(TextBox)s this.text=t2.Text; }
Key event
Private void t1_keyDown(object s,EventArgs e) { If(e.code==Keys.Enter) { Message.Box(You pressed enter); } }
TextBox Events
Event Description Enter Leave Validating Validated KeyDown KeyPress KeyUp
RadioButtons
Radio buttons traditionally display themselves as a label with a tiny circle to the left of it, which can be either selected or not.
To group radio buttons together so they create one logical unit you must
use a GroupBox control or some other container. When you first place a GroupBox onto a form and then place the RadioButton controls you need within the borders of the GroupBox , the RadioButton controls will automatically change their state to reflect that
Property Description
Appearance A radio button can be displayed either as a label with a circular check When it is displayed as a button, the control appears depressed when selected, and not depressed otherwise. AutoCheck When true , a black point is displayed when the user clicks the radio button. When false , the radio button must be manually checked in code from the Click event handler. CheckAlign -This is used to change the alignment of the check box portion of the radio button.
RadioButton rBut1 = new RadioButton(); rBut1.Text = Red"; rBut1.Checked = true; rBut1.CheckedChanged += new EventHandler(rBut_CheckedChanged); Controls.Add(rBut1);
}
else { r1.Text = "UnChecked"; } } }
RadioButton Events
CheckedChanged Click
Example
using System; using System.Windows.Forms; public class Exercise : Form { //Global declaration of controls RadioButton radSmall; RadioButton radMedium;
RadioButton radLarge;
public Exercise()
GroupBox grpPizzaSize;
//constructor that calls Intialise Component method
{ InitializeComponent(); }
private void InitializeComponent() { grpPizzaSize = new GroupBox(); radSmall = new RadioButton(); radMedium = new RadioButton();
The Caption of a Radio Button radSmall.Text = "Small"; radMedium.Text = "Medium"; radLarge.Text = "Large"; Event handler string ChosenMovie = ""; if (radSmall.Checked) { ChosenMovie = radSmall.Text; } else if (radLarge.Checked) { ChosenMovie = radLarge.Text; }
CheckBox Properties
Property Description CheckState -Unlike the radio button, a check box can have three states: Checked , Indeterminate , and Unchecked . When the state of the check box is Indeterminate , the control check next to the label is usually grayed, indicating that either the current value of the check is not valid; for some reason cannot be determined (e.g., the check indicates the read - only state of files, and two are
selected, of which one is read - only and the other is not); or has no meaning under the current
circumstances. ThreeState- When false , the user will not be able to change the CheckState state to Indeterminate .
CheckBox Events
Event
Description Occurs whenever the Checked property of the check box changes. Occurs whenever the CheckedState property changes. As Checked and Unchecked are both possible values of the CheckedState property, this event is sent whenever the Checked property changes. In addition, it is also sent when the state changes from Checked to Indeterminate.
CheckedChanged CheckStateChanged
AutoCheck Property
Gets or set a value indicating whether the Checked or CheckState values and
the CheckBox's appearance are automatically changed when the CheckBox is clicked.
ListBox Control.
enabled you to display a list of items from which the user can select one or multiple items from the list. The default is for the ListBox to list the items vertically however setting the multicolumn property to true the items are listed horizontally
{ InitializeComponent(); }
private void InitializeComponent() { lbxFamily = new ListBox(); Controls.Add(lbxFamily); } } //create listbox object
pressing SHIFT and clicking the mouse or pressing SHIFT and one of the arrow keys extends the selection from the previously selected item to the current item. Pressing CTRL and clicking the mouse selects or deselects an item in the list. MultiExtended permits the use of the Shift and Ctrl keys.
listBox1.Sorted = true; listBox1.SelectionMode = SelectionMode.MultiExtended; / Select three initial items from the list. listBox1.SetSelected(0,true); listBox1.SetSelected(2,true); listBox1.SetSelected(4,true);
Exercise
listBox1.SelectedIndexChanged+=new
EventHandler(listBox1_SelectedIndexChanged);
MessageBox.Show (listBox1.SelectedItem.ToString());
}
if (listBox1.SelectedIndex = = 1) { loadListBox(); }
We can clear all selected items by calling ClearSelected method. listBox1.ClearSelected(); The FindString method is used to find a string or substring in a ListBox int index = listBox1.FindString(textBox1.Text)
Properties
CheckedIndices-A collection of checked indices of the CheckedListBox control. CheckedItems-A collection of checked items. CheckOnClick-Specifies whether to check the box of an item if the item is selected. ThreeDCheckBoxes-Specifies whether the checkbox should be flat(twodimensional appearance) or normal(three-dimensional appearance).
private void WhatIsChecked_Click(object sender, System.EventArgs e) { // Display in a message box all the items that are checked. // First show the index and check state of all selected items. for each(int indexChecked in checkedListBox1.CheckedIndices)
// Next show the object title and check state for each item selected. For each(object i in checkedListBox1.CheckedItems) { // Use the IndexOf method to get the index of an item.
MENUS
A menu on a form is created with a MainMenu object, which is a collection of MenuItem objects. Can add menus to Windows Forms at design time by adding the MainMenu control and then adding menu items to it using the Menu Designer. Menus can also be added programmatically by adding one or more MainMenu controls to a form and adding MenuItem objects to the collection.
A submenu which contains another submenu can be identified by the arrow at its right side.
MENU CREATION
Each text menu item you add is represented by ToolStripMenuItem. The separator is represented by ToolStripSeparator. The ToolStripSeparator is only used to separate related menus. Each ToolStrip MenuItem is added to the MenuStrips Item property
this.Controls.Add(MainMenu);
//Add the File menu Item and add it to the MenuStrip above. ToolStripMenuItem obj1 = new ToolStripMenuItem("File"); MainMenu.Items.Add(obj1); //Item property of menu strip
//Create the New menu Items and add it to the File Menu above. We are also going to create the click event for the item. ToolStripMenuItem New = new ToolStripMenuItem("New"); New.Click += new EventHandler(New_Click); obj1.DropDownItems.Add(New);
Property
Checked
Tells whether the item is checked. CheckOnClick Tells whether an item will be checked or unchecked when it is clicked. CheckState- Tells whether the item is checked or unchecked.
Property
ShortcutKeys-The shortcut keys associated with the ToolStripMenuItem. ShowShortcutKeys-Tells whether the shortcut keys for an item is shown in the menu. Text-The text or label of the menu item. ToolTipText-The text inside the tool tip that appears when you hover your mouse on the menu item.
ShortcutKeys property. Select a menu item and then go to the Properties Window and find the ShortcutKeys property.