200 likes | 209 Views
Learn how to design forms, manage events, and work with controls in Visual Studio Form Designer to create interactive user interfaces easily. Explore hands-on tasks and best practices for efficient UI development.
E N D
Visual Studio Form Designer • For designing forms, changing the properties or adding events to forms and form elements can be achieved with the Visual Studio Form Designer • Toolbox • Properties • Let’s have a look!
Visual Studio Form Designer • Design this form:
Event Management • In order to address handler methods we need a delegate and an event defined for form objects • EventHandlerobject is a pre-defined delegate for working with form events • Every form element and the form itself have pre-defined events like Click, MouseClick, Keydown, Keypressed
Example: Button Click Event firstButton = new Button(); firstButton.Text = "Click Me!"; firstButton.Location = new Point(50, 50); firstButton.Size = new Size(150, 50); firstButton.Click += newEventHandler(firstButton_Click); void firstButton_Click(object sender, EventArgs e) { MessageBox.Show("Button clicked!"); } Adding an event handler to button Handler method for button click event
ExaminingDesignerGeneratedCode • All element initializations like buttons or the form itself occur under InitializeComponent()method • Designer seperates the actual class and its design using the partial classdeclaration • User class: Form1.cs • Designer generated class: Form1.Designer.cs
Some of Common Controls Controls Containers • Button • CheckBox • ComboBox • DateTimePicker • Label • ListBox • RadioButton • TextBox • ToolTip • … • Panel • GroupBox • SplitContainer • TabControl • …
Some General Properties and Events for Form Controls • Location (Point) • Text • Name • TabIndex • Visible • Enabled • Dock • … Events Properties • Click • MouseClick • MouseOver • MouseLeave • KeyDown • KeyPress • Paint • TextChanged • …
Message Boxes • Can be used for giving info to users or getting input from them • Returns a DialogueResult object when a button is clicked
Hands On - Coordinate Displayer • Display the x and y locations of the mouse position on a tooltip
Hands On: Creating a Layout • Create a layout with two columns and a StatusStrip • On the left column insert a ListBox • On the right column insert a TextBox • As mouse enters a component show which component entered on the StatusStrip • Add items to ListBox, when user clicks an item it should be added to the TextBox
Hands On: Text Formatter • Create a form with an editable text box • Users should be able to change the style of the text in the textbox using comboboxes, radiobuttons, etc
Working with Multiple Forms • Multiple forms can be used in an application • In order to call another form we call Show()method of the Form class MyForm frm2 = newMyForm(); frm2.Show(); • As you have the reference to the new form you can change the properties of it using that reference frm2.label1.Text = "New form opened"; frm2.Text = "Thisis a newform";
Working with Multiple Forms - MDI Forms • MDI forms can show other form instances inside themselves • In order to create an mdi form: • Set the “isMdiContainer” property of the main form to true • Then create a new form as usual and set its “MdiParent” property to true and show the form FrmChildfrmChild = newFrmChild(); frmChild.MdiParent = this; frmChild.Show();
Hands On : Multiple Forms • Get the name and surname of the user via a form and show the information with the date and time in a new form • When main form is closing ask the user if he/she is sure to exit
Menus • Two types of menu: • MenuStrip Menu residing at the top of forms, contains MenuItem objects • ContextMenu Menu appears when mouse right clicked on a component
Best Practices: Forms and Events • In order to reach fields of a child form from the main form, just use child form’s reference • If a message will be sent from the child form to the main form, use events • Child form will have an event to fire • In the main form an event handler should be registered to the the child form
Hands On: Customer Registration • Create one main form and a child form • In the main form insert a menu with an “Add Customer” menu item • The child form will include text boxes for name and surname of the customer • When a new customer is saved it should be listed in a list box in your main form
Timer Control • It is for calling methods in pre-defined time intervals (so are like threads) • Properties: • Interval : time intervals for methods to be executed in milliseconds • Enabled: for timer to execute, this property should be set True • Tick Event: Event fired when an interval is complete