Winforms C

  • Upload
    kt8815

  • View
    256

  • Download
    0

Embed Size (px)

Citation preview

  • 8/14/2019 Winforms C

    1/80

    1

    Windows Forms

    Chng 4

  • 8/14/2019 Winforms C

    2/80

    2

    Ni dung

    ng dng Windows FormsForm

    ControlsS hp l ca d liung dng MDI

  • 8/14/2019 Winforms C

    3/80

    3

    ng dng Windows Forms

  • 8/14/2019 Winforms C

    4/80

    4

    Kiu ng dng pht trin biVisual Studio

  • 8/14/2019 Winforms C

    5/80

    5

    Kiu ng dng pht trin biVisual Studio

    WinForms L th vin cho php nh pht trin to ng dng chy

    trn nn Windows c nh ngha trong khng gian tn

    System.Windows.Forms

    Trong ca s New Project, chn Templates WindowsApplication to ng dng WinForms

    WebForms L th vin lp cho php to ng dng chy trn nn

    Web c nh ngha trong khng gian tn System.Web

    Etc.

  • 8/14/2019 Winforms C

    6/80

    6

    Visual Studio - Ca s SolutionExplorerCa s Solution

    Explorer cho bit dn hin hnh cngvi cu trc phn cp

    ca cc tp tin trongtng d n

  • 8/14/2019 Winforms C

    7/80

    7

    Visual Studio - Hp cng cToolBoxCa s ny cha ng cc

    control WinForms,WebForms, ActiveX,dng to giao din

    ca trnh ng dng

  • 8/14/2019 Winforms C

    8/80

    8

    Visual Studio - Ca s thuc tnhPropertiesCa s thuc tnh cho

    php xem v thit lpgi tr cc thuc tnhca form, controls, etc.

  • 8/14/2019 Winforms C

    9/80

    9

    ng dng WinForms

    User Interface

    Program

  • 8/14/2019 Winforms C

    10/80

    10

    Cc tp tin ca ng dngWinForms

    *.sln Tp tin Solution (vn bn)

    *.cs Tp tin cha nh ngha v form & m lnh

    bn trong *.resx Tp tin ti nguyn

    *.csproj Tp tin d n (tp tin XML)

    Cc tp tin khc

  • 8/14/2019 Winforms C

    11/80

    11

    S kin v x l s kin

    User Interface

    User action clicking the buttonWhen Save Clicked()

    { Display Message(Save Button);}

    When Print Clicked()

    { Display Message(Print Button);}

    EventEvent Handler

  • 8/14/2019 Winforms C

    12/80

    12

    Ni dung

    ng dng Windows FormsForm

    ControlsS hp l ca d liung dng MDI

  • 8/14/2019 Winforms C

    13/80

    13

    Ca s (Form)

  • 8/14/2019 Winforms C

    14/80

    14

    Form Form: ca s c lp trnh hin th d liu

    & nhn thng tin t pha ngi dng Mt form l 1 lp trong ng dng WinForms Mt s thuc tnh ph bin:

    BackColor: mu nn ForeColor: mu ch Text: tiu form Font: font ch ca form Name: tn lp ca form

    Thuc tnh c th thay i nh m lnh hocca s thuc tnh

  • 8/14/2019 Winforms C

    15/80

    15

    Form - Thuc tnh

    Thay i thuc tnh ca form bng m lnh:. = ; Th d:

    this.BackColor = Color.Red;

    Thay i thuc tnh

    ca form bngca s thuc tnh:

  • 8/14/2019 Winforms C

    16/80

    16

    Form - Phng thc

    Mt s phng thc ph bin:Show(): th hin form ln mn hnhShowDialog(): th hin form ln mn hnh dng hp thoi

    Activate() : form c kch hot (active)Hide(): n form iClose(): ng form li v gii phng ti nguyn

    Gi thc thi phng thc:. (Tham s)

    Th d:DialogForm myForm = New DialogForm();

    myForm.Show(); //Th hin Form trng thi bnh thngmyForm.ShowDialog(); //Th hin Form dng hp thoimyForm.Activate(); myForm.Hide(); myForm.Close();

  • 8/14/2019 Winforms C

    17/80

    17

    Form - S kin Mt s s kin:

    Load: Xy ra khi th hin u tin ca Form c Load lnmn hnh.

    Activated: Xy ra khi Form c kch hot.Deactivated: Xy ra khi Form mt kch hot.

    FormClosing: Xy ra khi Form ang trong qu trnh ng li. Lu : S kin Loadc s dng khi to gi tr ca cc thnh

    phn trong Form. S kin FormClosing& FormClosed thng c s dng

    thu hi ti nguyn c cp pht cho Form.

  • 8/14/2019 Winforms C

    18/80

    18

    X l s kin trn Form

    Tnevent

    EventHandlers

  • 8/14/2019 Winforms C

    19/80

    19

    V d x l s kin trn Form

    Ngn thao tc ng form ca ngi dng: X l s kin FormClosing, t thuc tnh Cancel ca

    i tng FormClosingEventArgs l true

  • 8/14/2019 Winforms C

    20/80

    20

    S lin kt gia cc form trongng dng

    To mt i tng Form Th hin Form ln mn hnh nh phng thc

    Show()

    = new ();.Show();

    Th d:

    ImageForm NewForm = new ImageForm();

    NewForm.Show();

  • 8/14/2019 Winforms C

    21/80

    21

    Ni dung

    ng dng Windows FormsForm

    ControlsS hp l ca d liung dng MDI

  • 8/14/2019 Winforms C

    22/80

    22

    Controls

  • 8/14/2019 Winforms C

    23/80

    23

    Khi nim Controls l cc thnh phn c sn to giao

    din tng tc vi ngi dng Trong WinForms, lp Controls l lp c s cho

    cc controls khc

  • 8/14/2019 Winforms C

    24/80

    24

    Thuc tnh ca controls

    Mt s thuc tnh ph bin:Thuc tnh ngha

    BackColor Mu nn

    ForeColor Mu ch

    Name Tn ca control (tn i tng)

    Enabled Control c s dng hay khng?

    Visible Control c hin th hay khng?

    Text Chui xut hin trn control

  • 8/14/2019 Winforms C

    25/80

    25

    Truy cp thnh vin ca controls

    Truy cp thnh vin ca 1 control bng m lnh:.

    Th d: button1.Enabled = true;textBox1.Clear();

    Khi controls c t trn form, controls l d liuthnh vin ca form (tn d liu thnh vinchnh l tn controls)

  • 8/14/2019 Winforms C

    26/80

    26

    Label

    ComboBox

    ListBox

    CheckedListBox

    Button

    TextBox

  • 8/14/2019 Winforms C

    27/80

    27

    Controls trn form

  • 8/14/2019 Winforms C

    28/80

    28

    Mt s controls

    Label NumericUpDownButton ListViewRadioButton TreeViewTextBox ImageListListBox MenuCheckedBox MenuStripCheckedListBox ContextMenuStripComboBox Timer GroupBoxPictureBox

  • 8/14/2019 Winforms C

    29/80

    29

    Label Dng hin th d liu cho ngi dng Mt i tng thuc lp Label c to ra khi 1

    Label c thm vo form

  • 8/14/2019 Winforms C

    30/80

    30

    Button

    Dng xc nhn hnh ng hay 1 thao tc ca ngi dng Mt i tng Button c to ra khi 1 Button c thm vo form Thuc tnh

    Image: Hnh nh trn Button.FlatStyle: Hnh dng ca Button (Flat, Popup, Standard, and

    System)

    Phng thc

    Focus(): Button nhn c tiu im S kinClick: Xy ra khi button c nhp chut vo

  • 8/14/2019 Winforms C

    31/80

    31

    RadioButton L control cho php nhn d liu Yes/No, nhng cc

    RadioButton cng 1 nhm th khng th chn ng thi Mt i tng RadioButton c to ra khi 1

    RadioButton c t ln form Thuc tnh

    Checked: Xc nh control ang trng thi no (true/false)

    S kin CheckedChanged: Xy ra khi gi tr ca thuc tnh Checked

    b thay i

  • 8/14/2019 Winforms C

    32/80

    32

    CheckedBox c s dng nhn thng tin dng Yes/No Mt i tng CheckedBox c to ra khi 1 CheckedBox

    c t ln form Cc thuc tnh, phng thc, s kin tng t RadioButton

    Th d:

  • 8/14/2019 Winforms C

    33/80

    33

    TextBox

    Dng nhn/hin th d liu t pha ngi dng Thuc tnh

    Multiline: TextBox c nhn nhiu dng vn bn hay khng(Mc nh gi tr ny l false)

    CharacterCasing: t ng chuyn i d liu nhp caTextBox thnh dng tng ng (Normal, Upper, Lower) PasswordChar: K t hin th thay th khi nhp Font: Font ch ca TextBox

    Phng thcClear(): Xa trng TextBoxFocus(): TextBox nhn c tiu im (input focus), sn sng

    nhn d liu t bn phm

  • 8/14/2019 Winforms C

    34/80

    34

    TextBox S kin

    KeyPress: Xy ra khi ngi dng nhn gi 1 phm c mAscii t bn phm

    KeyDown: Xy ra khi ngi dng nhn 1 phm t bnphm

    KeyUp: Xy ra khi ngi dng th 1 phm t bn phm Leave: Xy ra khi tiu im (input focus) va thot khi

    TextBox Validating: Xy ra trc khi tiu im thot khi TextBox.

    C th dng kim tra s hp l ca d liu nhn Th d:

  • 8/14/2019 Winforms C

    35/80

    35

    Cc ListBoxes

    S dng hin th mt danh sch cho ngi dng la chn WinForms h tr 2 loi list box:

    ListBox

    CheckedListBox

    Mt i tng ListBox/CheckedListBox c to ra mi khi1 ListBox/CheckedListBox c thm vo form

  • 8/14/2019 Winforms C

    36/80

    36

    ListBox Thuc tnh

    Items: tp cc phn t trong ListBoxSelectedIndex: xc nh ch s ca mc ang hin thi

    c chn trong ListBox, phn t u tin c ch s 0 SelectedItems: tp cc mc ang c chn

    SelectedValue: gi tr ca mc ang c chn Phng thc

    ClearSelected: xa ht cc la chn trn ListBoxSetSelected(int, bool): chn hoc khng chn 1 mc

    trong danh schGetSelected(int): tr v 1 gi tr bool ch ra phn t nht

    nh c c la chn hay khng

  • 8/14/2019 Winforms C

    37/80

    37

    ListBox S kin

    SelectedIndexChanged: xy ra khi gi trca thuc tnh SelectedIndex thay i

    SelectedValueChanged: xy ra khi gi trca thuc tnh SelectedValue thay i

    Th d: S dng thuc tnh i tng Items thm cc mc vo ListBox. Vit m lnh x l trong hm x l s kin Load ca Form. Ngoi ra thuc tnh i tng Items cn c mt s thuc tnh &

    phng thc khc: Count, Clear, Insert, RemoveAt, Item

  • 8/14/2019 Winforms C

    38/80

    38

    CheckedListBox CheckedListBox cng h tr cc thnh vin ging nh ListBox;

    bn cnh control ny cn c nhng c im ring. Thuc tnh

    CheckedIndies: tp cc ch s ca cc phn t c la chnchecked)

    CheckedItems: tp hp cc phn t c chn (checked). Th d:CheckedIndies={1,2}CheckedItems={Lm Quang H, Trn Trung Trc}

  • 8/14/2019 Winforms C

    39/80

    39

    CheckedListBox Phng thc

    SetItemChecked: thit lp mc c ch s xc nh c chn(checked).

    GetItemChecked: kim tra mc c ch s xc nh c checkedhay khng

    SetItemCheckState: xc lp trng thi checked ca 1 mc

    xc nh Th d: S dng phng thc SetItemCheckState

  • 8/14/2019 Winforms C

    40/80

    40

    ListView

    Cho php hin th mt danh sch cc phn t dngthc c bit. ListView c 4 kiu sau: Text only: mc nh Text with small icon Text with large icon Detail view: cc phn t hin th nhiu ct

    ListView k tha cc tnh nng ca CheckedListBox Thuc tnh

    CheckBoxes: mi phn t trongds c 1 checkbox ng trc

    Items: tp cc phn t trongdanh sch

    MultiSelect: danh sch c nhiula chn hay khng

  • 8/14/2019 Winforms C

    41/80

    41

    V d ListView

  • 8/14/2019 Winforms C

    42/80

    42

    TreeView Cho php hin th d liu dng phn cp

    Mt i tng TreeView c to ra mi khi 1 TreeViewc t ln form

    TreeView h tr hu ht cc thnh vin ca ListView Thuc tnh

    ImageList: ds cc nh c hin th mi nt.

    Nodes: ds cc nt.SeletedNode: nt hin thi c la chn.

    Phng thcGetNodeAt: truy xut 1 nt v tr xc nh trong TreeView.GetNodeCount: tng s nt.

    S kin BeforeSelect/AfterSelect: xy ra trc/sau khi 1 nt c chn BeforeCollapse/AfterCollapse: xy ra trc/sau khi thu hp 1 nt BeforeExpand/AfterExpand: xy ra trc/sau khi 1 m rng 1 nt

  • 8/14/2019 Winforms C

    43/80

    43

    TreeView

    V d: Hin th 10 khch hng, mikhch hng c 5 n t hng.

    Thit k cc lp Khch hng &t hng.

    To TreeView nh hnh

  • 8/14/2019 Winforms C

    44/80

    44

    TreeView

  • 8/14/2019 Winforms C

    45/80

    45

    ComboBox L control kt hp gia TextBox v ListBox Mt i tng ComboBox c to ra khi 1 ComboBox

    c t ln form Thuc tnh

    DropDownStyle: kiu ca ComboBox; gm c Simple,

    DropDown, DropDownList

    Simple DropDown DropDownList

  • 8/14/2019 Winforms C

    46/80

    46

    GroupBox L control dng nhm cc controls khc Th d:

  • 8/14/2019 Winforms C

    47/80

    47

    PictureBox Dng hin th hnh nh Mt i tng PictureBox c to ra khi 1

    PictureBox c t ln form. Thuc tnh

    Image: Xc nh i tng hnh nh cn hin th.SizeMode: xc nh cch thc nh c hin th(AutoSize, CenterImage, Normal, StretchImage)

  • 8/14/2019 Winforms C

    48/80

    48

    NumericUpDown

    Cho php hin th mt s c th tng hay gim n mtgi tr xc nh Mt i tng NumericUpDown c to ra khi mt

    NumericUpDown c t ln form. Thuc tnh

    Increment: Gi tr s mi ln NumericUpDown tng hoc gimMaximum, Minimum: gi tr ln nht (nh nht) caNumericUpDown

    Value: gi tr hin hnh ca NumericUpDown

    Phng thcDownButton, UpButton: gim/tng gi tr hin hnh ca

    NumericUpDown 1 lng bng Increment S kin

    ValueChanged: xy ra khi NumericUpDown thay i gi tr

  • 8/14/2019 Winforms C

    49/80

    49

    ImageList

    Dng lu cc hnh nh v cho php chng hinth cc controls khc nhau.

    Mt i tng ImageList c to ra khi mtImageList c t ln form.

    Thuc tnh Images: tp hp cc nh ca ImageList

    ImageSize: kch thc ca cc nh c trong ImageList

  • 8/14/2019 Winforms C

    50/80

    50

    Menu L mt loi control trong ngi dng c th la chn

    cc mc thc thi t mt danh sch cho trc Phn loi

    Menu th xung (MenuStrip): l dng menu thng dng nht Menu bt ra/Menu ng cnh (ContextMenuStrip): thng

    hin th khi ta n nt phi chut

    Trong hp

    cng c Trong ng dngMDI khi thit k

    Trong hpcng c

    Lc thc thing dng

  • 8/14/2019 Winforms C

    51/80

    51

    MenuStrip MenuStrip l lp cho php to cc menu

    Cc mc con ca 1 i tng MenuStrip lToolStripMenuItem hoc ToolStripMenuSeparator

    Thuc tnh ca ToolStripMenuItemChecked: l True nu c du trc mc menu con

    Image: biu tng nh xut hin trc mi mc menu conShortcutKeys: xc lp phm tt

    S kin ca ToolStripMenuItemClick: Xy ra khi ngi dng Click chn mc menu

    ToolStripMenuItem

    ToolStripMenuSeparator

  • 8/14/2019 Winforms C

    52/80

    52

    MenuStrip

  • 8/14/2019 Winforms C

    53/80

    53

    ContextMenuStrip

    L control cho php ngi dng truy cp cc mctrn menu nh thao tc nhp chut phi

    Sau khi ContextMenuStrip c to ra, chng sc gn vi mt control khc nh vo thuc tnhContextMenuStrip ca control

    Cch thc to ContextMenuStrip tng t cch

    thc to MenuStrip

  • 8/14/2019 Winforms C

    54/80

    54

    Timer L control p ng li s tri i ca thi gian. Mt i tng Timer c to ra khi mt Timer

    c t ln form. Thuc tnh

    Enabled: cho php qun l thi gian thc thi.

    Interval: Khong thi gian gia 2 s kin. Phng thcStart: khi ng control thi gian.Stop: ngng control thi gian.

    S kin

    Tick: Xy ra sau 1 khong thi gian c ch nhtrong Interval

  • 8/14/2019 Winforms C

    55/80

    55

    Timer Th d:

    ng h s:

    Thuc tnh: Interval = 1000Enabled = true

    S kin Tick:

  • 8/14/2019 Winforms C

    56/80

    56

    Mt s controls khc

    DateTimePickerLinkedLabelTrackBarPanel

    TabControlRichTextBoxProgressBarToolTipToolBarStatusBar

  • 8/14/2019 Winforms C

    57/80

    57

    Hp thoi (Dialog)

    c dng : Hin th thng ip n ngi dng Nhn thng tin t pha ngi dng

    Cc loi

  • 8/14/2019 Winforms C

    58/80

    58

    Cc loi hp thoi

    OpenFileDialogPageSetUpDialogFontDialogColorDialogSaveFileDialogPrintPreviewDialogPrintDialog

    Custom dialog boxes

    Common dialog boxes

  • 8/14/2019 Winforms C

    59/80

    59

    OpenFileDialog

  • 8/14/2019 Winforms C

    60/80

    60

    FontDialog

  • 8/14/2019 Winforms C

    61/80

    61

    ColorDialog

  • 8/14/2019 Winforms C

    62/80

    62

    Hp thng ip - MessageBox

    L loi hp thoi cho php hin th cc thng ipVD: MessageBox.Show([Message]);

    Phng thc tnh Show() ca lp MessageBox c 12kiu khc nhau (np chng)

    Overloadpublic static DialogResult Show(string);

    public static DialogResult Show(IWin32Window, string);

    public static DialogResult Show(string, string);

    public static DialogResult Show(IWin32Window, string, string);

    public static DialogResult Show(string, string, MessageBoxButtons);

    public static DialogResult Show(IWin32Window, string string, MessageBoxButtons);

    public static DialogResult Show(string, string, MessageBoxButtons, MessageBoxIcon);

  • 8/14/2019 Winforms C

    63/80

    63

    Hp thng ip - MessageBoxOverload

    public static DialogResult Show(IWin32Window, string, string, string, MessageBoxButtons,MessageBoxIcon);

    public static dialogResultShow(string, string, MessageBoxButtons, MessageBoxIcon,MessageBoxDefaultButton);

    public static DialogResult Show(IWin32Window, string, string, MessageBoxButtons,MessageBoxIcon, MessageBoxDefaultButton);

    public static DialogResult Show(string, string, MessageBoxButtons, MessageBoxIcon,MessageBoxDefaultButton, MessageBoxOptions);

    public static DialogResult Show(IWin32Window, string, string, MessageBoxIcon,MessageBoxDefaultButton, MessageBoxOptions);

  • 8/14/2019 Winforms C

    64/80

    64

    Hp thng ip - MessageBox

    Sinh vin t tm hiu: IWin32WindowMessageBoxButtonsMessageBoxIcon

    MessageBoxDefaultButtonMessageBoxOptions

  • 8/14/2019 Winforms C

    65/80

    65

    Ni dung

    ng dng Windows FormsFormControlsS hp l ca d liung dng MDI

  • 8/14/2019 Winforms C

    66/80

    66

    S hp l ca d liu

  • 8/14/2019 Winforms C

    67/80

    67

    S hp l ca d liu

    S hp l mc trngS hp l mc form

    Mt s s kin c dng

    EnterLeave

    ValidatingValidated

    Cc loi kim tra

    Th t cc s kin

  • 8/14/2019 Winforms C

    68/80

    68

    S hp l mc trng Kim tra s hp l tng trng S dng cc s kin Enter, Leave, Validating,

    Validated Mt s s kin bn phm: KeyPress, KeyDown,

    KeyUp S dng mt s thuc tnh ca control lc thit k:

    MaxLength, Locked, PasswordChar VD: Mt s phng thc kim tra d liu ca lp

    Char: IsNumber, IsDigit, IsLetter, IsLower, IsUpper,GetNumericValue, IsLetterOrDigit, Parse

  • 8/14/2019 Winforms C

    69/80

    69

    S hp l mc form

    Kim tra s hp l ca tt c cc trng trn form 1 thi im no

    Cho php kch hot hay khng kch hot 1 thnhphn no

  • 8/14/2019 Winforms C

    70/80

    70

    ErrorProvider Control c s dng hin th icon li khi ngi dng thao

    tc li trn mt control no trn form

    Thuc tnh BlinkRate: xc nh tc nhp nhy ca icon li

    BlinkStyle: xc nh cch thc m icon nhp nhy trncontrol(AlwaysBlink, BlinkDifferentError & NeverBlink)

    Icon: ng dn ch n icon li

    Phng thc

    GetError: nhn v chui thng bo li SetError: xc lp chui thng bo li trn mt control xc nh

  • 8/14/2019 Winforms C

    71/80

    71

    ErrorProvider Control

    VD: X l ngoi l khi k t ngi dng nhp khngphi l s.

  • 8/14/2019 Winforms C

    72/80

    72

    Ni dung

    ng dng Windows FormsFormControlsS hp l ca d liung dng MDI

  • 8/14/2019 Winforms C

    73/80

    73

    ng dng MDI

  • 8/14/2019 Winforms C

    74/80

    74

    SDI - Single Document Interface

    NewDocument

    ng dng SDIch c mt

    ca s ti mtthi im

    Khng th m nhiu form ti cng 1 thi im Nu mun m nhiu form th cn to nhiu th hinca ng dng

  • 8/14/2019 Winforms C

    75/80

    75

    MDI - Multiple Document Interface

    Nhiu ti liu ng dng MDI nhmnhiu ti liu trong mt

    n v duy nht

    ng dng MDI gip ta c th thao tc vi nhiu ca strong cng 1 thi im

  • 8/14/2019 Winforms C

    76/80

    76

    MDI - Multiple Document Interface

    Ca s con

    Ca s cha

  • 8/14/2019 Winforms C

    77/80

    77

    To ca s MDI (ca s cha)

    IsMdiContainer= true

  • 8/14/2019 Winforms C

    78/80

    78

    To ca s con ca ca s MDI

    S dng thuc tnh MdiParent.MdiParent = ;.Show();

    Th d: Gi s ca s hin hnh l 1 MDI formfrmTravelEntry f = new frmTravelEntry();f.MdiParent = this;f.Show();

  • 8/14/2019 Winforms C

    79/80

    79

    c im ca ng dng MDI ng form cha th tt c cc form con u b ng li c im ca form cha

    Hin th khi ng dng khi ng L khung cha cho cc ca s khc Hin th menu ca cc form con Ch c duy nht mt form cha C th m nhiu form con ti mt thi im

    c im ca form con Khng th di chuyn khi vng hin th ca form cha

    M form con

  • 8/14/2019 Winforms C

    80/80

    Sp xp ca s con

    TitleCascade