C# 0 The Complete Reference


PART II C h a p t e r   2 6



Download 4,07 Mb.
Pdf ko'rish
bet1064/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   1060   1061   1062   1063   1064   1065   1066   1067   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt


PART II

C h a p t e r   2 6 :  

U s e   S y s t e m . W i n d o w s . F o r m s  

857


Abor t

Cancel


Ignore

No

None



OK

Retr y


Yes

Your program can examine the return value to determine the course of action the user desires. 

For example, if the message box prompts the user before overwriting a file, your program can 

prevent the overwrite if the user clicks Cancel, and it can allow the overwrite if the user clicks OK.

The following program adds a stop button and a message box to the preceding example. 

In the stop button handler, a message box is displayed that asks the user if he or she wants 

to stop the program. If the user clicks Yes, the program is stopped. If the user clicks No, the 

program continues running.

// Add a stop button.

using System;

using System.Windows.Forms;

using System.Drawing;

class ButtonForm : Form {

  Button MyButton;

  Button StopButton;

  public ButtonForm() {

    Text = "Adding a Stop Button";

    // Create the buttons.

    MyButton = new Button();

    MyButton.Text = "Press Here";

    MyButton.Location = new Point(100, 200);

    StopButton = new Button();

    StopButton.Text = "Stop";

    StopButton.Location = new Point(100, 100);

    // Add the button event handlers to the window.

    MyButton.Click += MyButtonClick;

    Controls.Add(MyButton);

    StopButton.Click += StopButtonClick;

    Controls.Add(StopButton);

  }


  [STAThread]

  static void Main() {

    ButtonForm skel = new ButtonForm();

    Application.EnableVisualStyles();

    Application.Run(skel);

  }


  // Handler for MyButton.

  void MyButtonClick(object who, EventArgs e) {

    if(MyButton.Top == 200)

      MyButton.Location = new Point(10, 10);

    else

www.freepdf-books.com




858

 

P a r t   I I :  



E x p l o r i n g   t h e   C #   L i b r a r y

      MyButton.Location = new Point(100, 200);

  }

  // Handler for StopButton.



  void StopButtonClick(object who, EventArgs e) {

    // If user answers Yes, terminate the program.

    DialogResult result = MessageBox.Show("Stop Program?",

                            "Terminate",

                            MessageBoxButtons.YesNo);

    if(result == DialogResult.Yes) Application.Exit();

  }

}

Let’s look closely at how the message box is used. Inside the 



ButtonForm

 constructor, a 

second button is added. This button contains the text “Stop,” and its event handler is linked 

to


Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   1060   1061   1062   1063   1064   1065   1066   1067   ...   1096




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish