1 / 20

Graphical User Interfaces with Windows Forms

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.

fmartinez
Download Presentation

Graphical User Interfaces with Windows Forms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Graphical User Interfaces with Windows Forms

  2. 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!

  3. Visual Studio Form Designer • Design this form:

  4. 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

  5. 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

  6. 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

  7. Some of Common Controls Controls Containers • Button • CheckBox • ComboBox • DateTimePicker • Label • ListBox • RadioButton • TextBox • ToolTip • … • Panel • GroupBox • SplitContainer • TabControl • …

  8. 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 • …

  9. Message Boxes • Can be used for giving info to users or getting input from them • Returns a DialogueResult object when a button is clicked

  10. Hands On - Coordinate Displayer • Display the x and y locations of the mouse position on a tooltip

  11. 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

  12. 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

  13. 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";

  14. 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();

  15. 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

  16. 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

  17. 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

  18. 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

  19. Hands On (Cont’d)

  20. 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

More Related