Graphical User Interface in C Sharp - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Graphical User Interface in C Sharp

Description:

Demonstrate how to generate a form in C Sharp. Illustrate the key components of a form ... Each event has to be attached' to an event hander :- // btnAdd ... – PowerPoint PPT presentation

Number of Views:218
Avg rating:3.0/5.0
Slides: 15
Provided by: bobed
Category:

less

Transcript and Presenter's Notes

Title: Graphical User Interface in C Sharp


1
Graphical User Interface in C Sharp
2
Objectives
  • Demonstrate how to generate a form in C Sharp
  • Illustrate the key components of a form
  • Constructor
  • InitializeComponents Method
  • Dispose Method
  • Demonstrate some controls available
  • Demonstrate the Event Handler

3
Form Creation
  • To create a Windows based application select a
    Windows Application rather than a Console
    Application.
  • At least two cs files are created
  • Program.cs (this starts the application)
  • Form1.cs (contains the form design and
    code)also contains
  • Form1.designer.cs - code to define controls on
    form
  • Form1.resx - non visible resources on a form

4
(No Transcript)
5
Program.cs Contents
  • using System
  • using System.Collections.Generic
  • using System.Windows.Forms
  • namespace WindowSCalculatorDemo
  • static class Program
  • /// ltsummarygt
  • /// The main entry point for the
    application.
  • /// lt/summarygt
  • STAThread
  • static void Main()
  • Application.EnableVisualStyles()
  • Application.SetCompatibleTextRendering
    Default(false)
  • Application.Run(new Form1())

6
Form1.cs Contents
  • using System
  • using System.Collections.Generic
  • using System.ComponentModel
  • using System.Data
  • using System.Drawing
  • using System.Text
  • using System.Windows.Forms
  • namespace Formsdemo1
  • public partial class Form1 Form
  • public Form1()
  • InitializeComponent()
  • private void Form1_Load(object sender,
    EventArgs e)

7
Form1.Designer.cs part 1
  • partial class Form1
  • /// ltsummarygt
  • /// Required designer variable.
  • /// lt/summarygt
  • private System.ComponentModel.IContainer
    components null
  • /// ltsummarygt
  • /// Clean up any resources being used.
  • /// lt/summarygt
  • /// ltparam name"disposing"gttrue if managed
    resources should be disposed otherwise,
    false.lt/paramgt
  • protected override void Dispose(bool disposing)
  • if (disposing (components !
    null))
  • components.Dispose()

8
Form1.Designer.cs part 2
  • region Windows Form Designer generated code
  • /// ltsummarygt
  • /// Required method for Designer support
    - do not modify
  • /// the contents of this method with the
    code editor.
  • /// lt/summarygt
  • private void InitializeComponent()
  • this.SuspendLayout()
  • //
  • // Form1
  • //
  • this.AutoScaleDimensions new
    System.Drawing.SizeF(6F, 13F)
  • this.AutoScaleMode
    System.Windows.Forms.AutoScaleMode.Font
  • this.ClientSize new
    System.Drawing.Size(292, 266)
  • this.Name "Form1"
  • this.Text "Form1"
  • this.Load new System.EventHandler(t
    his.Form1_Load)
  • this.ResumeLayout(false)

9
Initialize with Button and textbox
  • private void InitializeComponent()
  • this.button1 new System.Windows.Form
    s.Button()
  • this.textBox1 new
    System.Windows.Forms.TextBox()
  • this.SuspendLayout()
  • //
  • // button1
  • //
  • this.button1.Location new
    System.Drawing.Point(116, 47)
  • this.button1.Name "button1"
  • this.button1.Size new
    System.Drawing.Size(75, 23)
  • this.button1.TabIndex 0
  • this.button1.Text "Press Me"
  • this.button1.UseVisualStyleBackColor
    true
  • //
  • // textBox1
  • //
  • this.textBox1.Location new
    System.Drawing.Point(105, 152)
  • this.textBox1.Name "textBox1"

10
Control Class
  • Size and location
  • Height, Width, Top, Bottom,Left, Right
  • Dock property
  • Anchor
  • Appearance
  • BackColor, ForeColor,Font name, size, Text
  • User Interaction
  • KeyDown,KeyPress,KeyUp
  • Enter,GotFocus,Leave,Validating,Validated,LostFocu
    s

11
Listbox
  • From ListControl Class
  • SelectedIndex integer value to the currently
    selected item
  • DisplayMember
  • ValueMember
  • Obj ListBox1.ItemsListBox1.SelectedIndex
  • FindString, FindStringExact
  • SelectedIndexChanged event
  • SelectedValueChanged event

12
Event Handlers
  • Each control (and the form itself) generates an
    event when it is used in some way!
  • Each event has to be attached to an event
    hander -
  • //
  • // btnAdd
  • //
  • this.btnAdd.Location new
    System.Drawing.Point(37, 194)
  • this.btnAdd.Name "btnAdd"
  • this.btnAdd.Size new
    System.Drawing.Size(75, 23)
  • this.btnAdd.TabIndex 3
  • this.btnAdd.Text "Add"
  • this.btnAdd.UseVisualStyleBackColor
    true
  • this.btnAdd.Click new
    System.EventHandler(this.btnAdd_Click)
  • this.btnAdd.Click new
    System.EventHandler(this.incrementer)

13
  • Application Properties
  • Assembly name
  • Namespace
  • Startup object

14
Application Class
Write a Comment
User Comments (0)
About PowerShow.com