1 Working with Controls at Run Time. 2 Objectives You will be able to Add controls to a Windows form at run time. Modify controls at run time.

Slides:



Advertisements
Similar presentations
 2007 Dr. Natheer Khasawneh. Chapter 13. Graphical User Interface Concepts: Part 1.
Advertisements

11 Getting Started with ASP.NET Beginning ASP.NET 4.0 in C# 2010 Chapters 5 and 6.
Chapter 18 - Data sources and datasets 1 Outline How to create a data source How to use a data source How to use Query Builder to build a simple query.
Iterator Pattern Dr. Neal CIS 480. Iterator An iterator pattern can be used when one class is a collection of things and would like to provide a standardized.
GTECH 731 Lab Session 4 Lab 3 Review, Lab 4 Intro 9/28/10 Lab 3 Review Lab 4 Overview.
Creating web service (and client application) with Visual Studio 2008 Create web service project New project->web->ASP.NET web service application (with.
Click Here to Begin the Game CHOICE 1CHOICE 2CHOICE 3 CHOICE CHOICE
1 Web Services Visual C# 2008 Step by Step Chapter 30.
Programming Paradigms Imperative programming Functional programming Logic programming Event-driven programming Object-oriented programming A programming.
Adding Controls to User Forms. Adding Controls A user form isn’t much use without some controls We’re going to add controls and write code for them Note.
1 Windows Printing. 2 Objectives You will be able to Output text and graphics to a printer. Print multipage documents. Use the standard Windows print.
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
Visual Basic 2008 Express Edition The IDE. Visual Basic 2008 Express The Start Page Recent Projects Open an existing project Create a New Project.
Dr Dat Tran - Week 4 Lecture Notes 1 MenuStrip Programming Graphical User Interfaces PG (7110) University of Canberra School of Information Sciences &
Introduction to Graphical User Interfaces. Objectives * Students should understand what a procedural program is. * Students should understand what an.
11 Updating a Database Table Textbook Chapter 14.
Data Binding to Controls Programming in C# Data Binding to Controls CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis.
Events and Timers. Event handlers ScratchC#/VS 2 Script == event handler.
Session 08: Architecture Controllers or Managers Graphical User Interface (GUI) FEN AK - IT Softwarekonstruktion.
1 Working with Menus and Dialog Boxes. 2 Objectives You will be able to Create and edit menus for Windows Forms applications. Write code to handle menu.
11 Web Services. 22 Objectives You will be able to Say what a web service is. Write and deploy a simple web service. Test a simple web service. Write.
Dr Dat Tran - Week 1 Lecture Notes 1 Windows Forms Programming Graphical User Interfaces PG (7110) University of Canberra School of Information Sciences.
Dr Dat Tran - Week 4 Lecture Notes 1 ToolStrip Programming Graphical User Interfaces PG (7110) University of Canberra School of Information Sciences &
1 Scroll Bars Providing Scrollbars. 2 Objectives You will be able to: Use Windows Graphics to display tabular information on a Windows form. Add graphics.
Computer Science 111 Fundamentals of Programming I Model/View/Controller and Data model design.
Wii mote interfacing. The product It is a wireless device, using standard Bluetooth technology to communicate The Wii Remote uses the standard Bluetooth.
BIL528 – Bilgisayar Programlama II Introduction 1.
Sample Application Multi Layered Architecture (n-tier): –Graphical User Interface (GUI): Forms, components, controls The Visual Designer in Visual Studio.
1 Chapter Eleven Handling Events. 2 Objectives Learn about delegates How to create composed delegates How to handle events How to use the built-in EventHandler.
1 Project 3: Chomp (Not graded). 2 Project 3 The game of Chomp was described in a Math Trek column in Science News:
Dynamic Dropdown Lists 1. Objectives You will be able to Use Dropdown Lists to solicit multiple choice user input in an ASPX web page. Populate a Dropdown.
An Example of Windows Forms Applications Windows-based application –Win Forms Control structures (selection and repetition) Graphics Read integers from.
Joe Hummel, PhD Dept of Mathematics and Computer Science Lake Forest College
Programming with Visual C++: Concepts and Projects Chapter 2B: Reading, Processing and Displaying Data (Tutorial)
Windows Forms Navigating database with windows forms.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
CNS 1410 Graphical User Interfaces. Obectives Students should understand the difference between a procedural program and an Event Driven Program. Students.
Discussion 4. Labs public MyPoint(double xInit, double yInit ) { MyPoint p = new MyPoint(0, 0); } ClassProblem.java recursive java.lang.StackOverflowError.
1 Creating Windows GUIs with Visual Studio. 2 Creating the Project New Project Visual C++ Projects Windows Forms Application Give the Project a Name and.
Windows Presentation Foundation (WPF). Introduction Separates appearance of user interface from behavior Appearance usually specified by XAML Behavior.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query.
Dr Dat Tran - Week 2 Lecture Notes 1 Forms Programming Graphical User Interfaces PG (7110) University of Canberra School of Information Sciences & Engineering.
Objects and Classes Continued Engineering 1D04, Teaching Session 10.
CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0.
Architecture Multi Layered Architecture (n-tier): Application: Model Controllers Database Access Graphical User Interface (GUI): Forms, components, controls.
Objects and Classes Engineering 1D04, Teaching Session 9.
1 Printing a Document. 2 Objectives You will be able to print a real multipage document.
Effective C# 50 Specific Way to Improve Your C# Item 22, 23.
1NetBeans Tutorial Using the “Properties” menu, name the List “List1” and the button “Button1”. NetBeans Tutorial6.
Lecture Set 7 Procedures and Event Handlers Part B - The Structure of an Application Event Handlers.
Chomp. How is the game played Human player goes first choose a square, all to the right and down are “eaten” computer takes a turn whoever is forced to.
1 Low Level ADO.NET Operations II Microsoft Visual C# 2008 Step by Step Chapter 25.
11 User Controls Beginning ASP.NET in C# and VB Chapter 8.
Programming with Visual Basic.NET. Quick Links Program Code The Code Window The Event Procedure Assignment Statements Using AutoList Radio Buttons Buttons.
Course ILT Using complex selection structures Unit objectives Include radio buttons and check boxes in an interface, create and call a user- defined Sub.
Computer Science Up Down Controls, Decisions and Random Numbers.
Timed Math Quiz. In This Part, You Learn How to Generate random numbers by using the Random class. Trigger events to occur at a specific time by using.
Computing with C# and the .NET Framework
Visual Basic..
Picture Viewer.
CIS16 Application Development and Programming using Visual Basic.net
Lecture Set 10 Windows Controls and Forms
Visual Basic: Week 5 Review User defined functions
Control Structures Part C – Groups of Controls
5/6/2019 Session 8.2 Postback, ViewState
Poker2.jpg Poker14.jpg int num1, num2;
Presentation transcript:

1 Working with Controls at Run Time

2 Objectives You will be able to Add controls to a Windows form at run time. Modify controls at run time.

3 Setting Up Controls at Run Time Why do this? May not know exactly what we need at design time. With a large number of controls, it might be easier to write code to set up the controls than to create them manually at design time.

4 The Controls Collection The Windows Form class has a Controls collection. Everything that we see on the form. Implements interface IList Accessible at run time as the form's Controls property. We can instantiate control objects and add them to the collection. Also modify existing controls.

5 Example: The Game of Chomp The game of Chomp was described in a Math Trek column in Science News: title/Math_Trek__Chomping_to_Win title/Math_Trek__Chomping_to_Win

6 The Game of Chomp

8 Implementing Chomp Let's create a Windows Form for a game of chomp with five rows and six columns of squares. Each square will be a button. When a button is clicked, it and all buttons above it and to its right will disappear. Create all buttons at run time. Modify them at run time as users play.

9 Getting Started Create a new C# Windows Forms project

10 An Example Button We can copy from Visual Studio's generated code.

11 An Example Button Double click on the button to add an event handler.

12 Generated Code Note statement to hook up the Event Handler. (line 43)

13 Form1 public partial class Form1 : Form { int number_of_rows = 5; int number_of_cols = 6; int button_size = 50;

14 Add_Button() Add to class Form private void Add_Button(int row, int col) { Button btn = new Button(); btn.BackColor = System.Drawing.Color.Blue; btn.Location = new System.Drawing.Point(col*button_size, row*button_size); btn.Name = "btn" + row + col; btn.Size = new System.Drawing.Size(button_size, button_size); btn.UseVisualStyleBackColor = false; btn.Click += new System.EventHandler(btn00_Click); Controls.Add(btn); } Delete the example button.

15 Form Load Event Handler

16 Form1_Load() private void Form1_Load(object sender, EventArgs e) { for (int row = 0; row < number_of_rows; ++row) { for (int col = 0; col < number_of_cols; ++col) { Add_Button(row, col); }

17 Program Running

18 Set up the "poisoned" button private void Add_Button(int row, int col) { Button btn = new Button(); if ((row == number_of_rows - 1) && (col == 0)) { btn.BackColor = System.Drawing.Color.Red; btn.Enabled = false; } else { btn.BackColor = System.Drawing.Color.Blue; btn.Enabled = true; }

19 Form with Poisoned Button

20 Initial Click Handler private void btn00_Click(object sender, EventArgs e) { Button btn = (Button)sender; int row = btn.Name[3] - '0'; int col = btn.Name[4] - '0'; MessageBox.Show("Row " + row + " Col " + col + " clicked"); }

21 Click Lower Right Corner

22 Update_Buttons private void Update_Buttons(int row, int col) { for (int i = 0; i < Controls.Count; ++i) { Control c = Controls[i]; Button btn = c as Button; if (btn == null) continue; int btn_row = btn.Name[3] - '0'; int btn_col = btn.Name[4] - '0'; if ((btn_row = col)) { btn.BackColor = Color.White; btn.Enabled = false; } Note "as"

23 Update_Buttons private void btn00_Click(object sender, EventArgs e) { Button btn = (Button)sender; int row = btn.Name[3] - '0'; int col = btn.Name[4] - '0'; //MessageBox.Show("Row " + row + " Col " + col + " clicked"); Update_Buttons(row, col); }

24 Keep track of the players public partial class Form1 : Form { int current_player = 1;... private void Form1_Load(object sender, EventArgs e) { for (int row = 0; row < number_of_rows; row++) { for (int col = 0; col < number_of_cols; col++) { Add_Button(row, col); } MessageBox.Show("Player 1 "); }

25 Keep track of the players private void btn00_Click(object sender, EventArgs e) { Button btn = (Button)sender; int row = btn.Name[3] - '0'; int col = btn.Name[4] - '0'; //MessageBox.Show("Row " + row + " Col " + col + " clicked"); Update_Buttons(row, col); current_player = current_player == 1? 2 : 1; MessageBox.Show("Player " + current_player); } Build and run

26 Initial Form

27 Check for Game Over private bool Game_Over() { for (int i = 0; i < Controls.Count; ++i) { Control c = Controls[i]; Button btn = c as Button; if (btn == null) continue; if (btn.Enabled) { return false; } return true; }

28 Check for Game Over private void btn00_Click(object sender, EventArgs e) { Button btn = (Button)sender; int row = btn.Name[3] - '0'; int col = btn.Name[4] - '0'; //MessageBox.Show("Row " + row + " Col " + col + " clicked"); Update_Buttons(row, col); if (Game_Over() ) { MessageBox.Show("Game Over! \n" + "Player " + current_player + " wins."); } else { current_player = current_player == 1? 2 : 1; MessageBox.Show("Player " + current_player); }