C# Form Design: Creating Menus and Handling Events, Slides of Programming for Engineers

Code examples for creating a form in c# with menus and handling events such as menu clicks and color changes. It includes creating a mainmenu, menuitem, and setting event handlers for menu clicks and color changes.

Typology: Slides

2011/2012

Uploaded on 07/25/2012

hun_i
hun_i 🇮🇳

3.7

(3)

54 documents

1 / 40

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Graphical User Interface-II
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28

Partial preview of the text

Download C# Form Design: Creating Menus and Handling Events and more Slides Programming for Engineers in PDF only on Docsity!

Graphical User Interface-II

PictureBoxes

  • Class PictureBox
    • Displays an image
      • Image set by object of class Image.
        • The Image property sets the Image object to use
        • SizeMode property sets how the image is displayed

1 // Fig. 12.33: Form1.h 2 // Using a PictureBox to display images. 3 4 #pragma once 5 6 7 namespace PictureBoxTest 8 { 9 using namespace System; 10 using namespace System::ComponentModel; 11 using namespace System::Collections; 12 using namespace System::Windows::Forms; 13 using namespace System::Data; 14 using namespace System::Drawing; 15 using namespace System::IO; 16 17 ///

18 /// Summary for Form 19 /// 20 /// WARNING: If you change the name of this class, you will need to 21 /// change the ’Resource File Name’ property for the managed 22 /// resource compiler tool associated with all .resx files 23 /// this class depends on. Otherwise, the designers will not 24 /// be able to interact properly with localized resources 25 /// associated with this form.

26 ///

27 public __gc class Form1 : public System::Windows::Forms::Form 28 { 29 public: 30 Form1(void) 31 { 32 InitializeComponent(); 33 } 34 35 protected: 36 void Dispose(Boolean disposing) 37 { 38 if (disposing && components) 39 { 40 components->Dispose(); 41 } 42 __super::Dispose(disposing); 43 } 44 private: System::Windows::Forms::Label * promptLabel; 45 private: System::Windows::Forms::PictureBox * imagePictureBox; 46 47 private: static int imageNum = - 1 ; 48 49 private:

1 // Fig. 12.34: Form1.cpp 2 // PictureBox demonstration. 3 4 #include "stdafx.h" 5 #include "Form1.h" 6 #include <windows.h> 7 8 using namespace PictureBoxTest; 9 10 int APIENTRY _tWinMain(HINSTANCE hInstance, 11 HINSTANCE hPrevInstance, 12 LPTSTR lpCmdLine, 13 int nCmdShow) 14 { 15 System::Threading::Thread::CurrentThread->ApartmentState = 16 System::Threading::ApartmentState::STA; 17 Application::Run(new Form1()); 18 return 0 ; 19 }

Mouse Event Handling

  • Class MouseEventArgs
    • Contain coordinates of the mouse pointer
    • The mouse pressed
    • Number of clicks
    • Number of notches the wheel turned
    • Passing mouse event
    • Mouse event-handling methods take an object and

MouseEventArgs object as argument

  • The Click event uses delegate EventHandler and event arguments

EventArgs

1 // Fig. 12.36: Form1.h 2 // Using the mouse to draw on a form. 3 4 #pragma once 5 6 7 namespace PainterTest 8 { 9 using namespace System; 10 using namespace System::ComponentModel; 11 using namespace System::Collections; 12 using namespace System::Windows::Forms; 13 using namespace System::Data; 14 using namespace System::Drawing; 15 16 ///

17 /// Summary for Form 18 /// 19 /// WARNING: If you change the name of this class, you will need to 20 /// change the ’Resource File Name’ property for the managed 21 /// resource compiler tool associated with all .resx files 22 /// this class depends on. Otherwise, the designers will not 23 /// be able to interact properly with localized resources 24 /// associated with this form. 25 ///

26 public __gc class Form1 : public System::Windows::Forms::Form 27 { 28 public: 29 Form1(void) 30 { 31 InitializeComponent(); 32 } 33 34 protected: 35 void Dispose(Boolean disposing) 36 { 37 if (disposing && components) 38 { 39 components->Dispose(); 40 } 41 __super::Dispose(disposing); 42 } 43 44 private: static bool shouldPaint = false; // whether to paint 45 46 private: 47 ///

48 /// Required designer variable. 49 /// 50 System::ComponentModel::Container * components;

68 // draw circle whenever mouse button moves (and mouse is down) 69 private: System::Void Form1_MouseMove(System::Object * sender, 70 System::Windows::Forms::MouseEventArgs * e) 71 { 72 if ( shouldPaint ) { 73 Graphics *graphics = CreateGraphics(); 74 graphics->FillEllipse( new SolidBrush( 75 Color::BlueViolet ), e->X, e->Y, 4 , 4 ); 76 } // end if 77 } 78 }; 79 }

1 // Fig. 12.37: Form1.cpp 2 // Mouse event handling demonstration. 3 4 #include "stdafx.h" 5 #include "Form1.h" 6 #include <windows.h> 7 8 using namespace PainterTest; 9 10 int APIENTRY _tWinMain(HINSTANCE hInstance, 11 HINSTANCE hPrevInstance, 12 LPTSTR lpCmdLine, 13 int nCmdShow) 14 { 15 System::Threading::Thread::CurrentThread->ApartmentState = 16 System::Threading::ApartmentState::STA; 17 Application::Run(new Form1()); 18 return 0 ; 19 }

Keyboard Event Handling

Keyboard Events, Delegates

and Event Arguments

Key Events (Delegate

KeyEventHandler , event

arguments KeyEventArgs )

KeyDown Raised when key is initially pushed down.

KeyUp Raised when key is released.

Key Events (Delegate

KeyPressEventHandler ,

event arguments

KeyPressEventArgs )

KeyPress Raised when key is pressed. Occurs repeatedly

while key is held down, at a rate specified by

the operating system.

Class KeyPressEventArgs

Properties

KeyChar Returns the ASCII character for the key

pressed.

Handled Indicates whether the KeyPress event was

handled (i.e., has an event handler associated

with it).

Class KeyEventArgs Properties

Alt Indicates whether the Alt key was pressed.

Control Indicates whether the Control key was pressed.

Shift Indicates whether the Shift key was pressed.

Keyboard events, delegates and event arguments. (1 of 2)

12.10 Keyboard Event Handling

Handled (^) Indicates whether the event was handled (i.e., has an event handler associated with it). KeyCode (^) Returns the key code for the key, as a Keys enumeration. This does not include modifier key information. Used to test for a specific key. KeyData (^) Returns the key code as a Keys enumeration, combined with modifier information. Used to determine all information about the key pressed. KeyValue (^) Returns the key code as an int, rather than as a Keys enumeration. Used to obtain a numeric representation of the key pressed. Modifiers (^) Returns a Keys enumeration for any modifier keys pressed ( Alt , Control and Shift ). Used to determine modifier key information only. Fig. 12.38 Keyboard events, delegates and event arguments.

Keyboard events, delegates and event arguments. (2 of 2)

26 public __gc class Form1 : public System::Windows::Forms::Form 27 { 28 public: 29 Form1(void) 30 { 31 InitializeComponent(); 32 } 33 34 protected: 35 void Dispose(Boolean disposing) 36 { 37 if (disposing && components) 38 { 39 components->Dispose(); 40 } 41 __super::Dispose(disposing); 42 } 43 private: System::Windows::Forms::Label * charLabel; 44 private: System::Windows::Forms::Label * keyInfoLabel; 45 46 private: 47 ///

48 /// Required designer variable. 49 /// 50 System::ComponentModel::Container * components;

52 // Visual Studio .NET generated GUI code 53 54 // display the name of the pressed key 55 private: System::Void Form1_KeyPress(System::Object * sender, 56 System::Windows::Forms::KeyPressEventArgs * e) 57 { 58 charLabel->Text = String::Concat( S"Key pressed: ", 59 ( e->KeyChar ).ToString() ); 60 } 61 62 // display modifier keys, key code, key data and key value 63 private: System::Void Form1_KeyDown(System::Object * sender, 64 System::Windows::Forms::KeyEventArgs * e) 65 { 66 keyInfoLabel->Text = String::Concat( 67 S"Alt: ", ( e->Alt? S"Yes" : S"No" ), S"\n", 68 S"Shift: ", ( e->Shift? S"Yes" : S"No" ), S"\n", 69 S"Ctrl: ", ( e->Control? S"Yes" : S"No" ), S"\n", 70 S"KeyCode: ", __box( e->KeyCode ), S"\n", 71 S"KeyData: ", __box( e->KeyData ), S"\n", 72 S"KeyValue: ", e->KeyValue ); 73 } 74