How To Create A Checkbox In Visual Basic 2.5.2.2 (For A Powerpoint) With A Check Box At Run Time



Similar documents
Everything you wanted to know about Visual Basic 6 Colors

Named Memory Slots. Properties. CHAPTER 16 Programming Your App s Memory

Source Code Translation

Part A: Introduction to Excel VBA

Dreamweaver Tutorials Creating a Web Contact Form

Reviewing documents with track changes in Word 2013

College of Arts & Sciences Degree Works Template Management User Guide for Advisors

Installation & User Guide

UNIVERSITY OF CALGARY Information Technologies WEBFORMS DRUPAL 7 WEB CONTENT MANAGEMENT

Access 2010: The Navigation Pane

MS WORD 2007 (PC) Macros and Track Changes Please note the latest Macintosh version of MS Word does not have Macros.

Connect to an Oracle Database from within Visual Basic 6 (Part 1)

Introduction to Visual Basic and Visual C++ Introduction to Control. TextBox Control. Control Properties. Lesson 5

Using WINK to create custom animated tutorials

Bitrix Site Manager 4.1. User Guide

Content Author's Reference and Cookbook

Working with sections in Word

Hypercosm. Studio.

Lots and Lots of Symbols, Free. Introduction. Tutorial: Getting Symbols. New Commands in AutoCAD 2010: Part 8. by Ralph Grabowski

MicroStrategy Quick Guide: Creating Prompts ITU Data Mart Support Group, Reporting Services

Microsoft Office 2010 Publisher

Microsoft Access 2010 Overview of Basics

One Dimension Array: Declaring a fixed-array, if array-name is the name of an array

Livezilla How to Install on Shared Hosting By: Jon Manning

Create a Simple Website. Intel Easy Steps Intel Corporation All rights reserved.

Posting Your Initial Entries. Login

Acrobat X Pro Accessible Forms and Interactive Documents

VISUAL BASIC Controls. Version 1 Trg. Div., NIC HQRs

Creating tables of contents and figures in Word 2013

To Begin Customize Office

Introduction to Visual Basic

Multi-user Collaboration with Autodesk Revit Worksharing

Using Excel 2000 to Create a Weighted-Grade Grade Book

Visual Basic Programming. An Introduction

The VB development environment

Introduction to Word 2007

Database Automation using VBA

Charms Recording Studio USER GUIDE For PC/Mac As a Parent/Student/Member

Apache Configuration

Installation & User Guide

InTime. Contractor Guides

Umbraco v4 Editors Manual

Sample Table. Columns. Column 1 Column 2 Column 3 Row 1 Cell 1 Cell 2 Cell 3 Row 2 Cell 4 Cell 5 Cell 6 Row 3 Cell 7 Cell 8 Cell 9.

2014 V1.0. LiveText e-portfolios

Digital Marketing EasyEditor Guide Dynamic

Advanced Programming with LEGO NXT MindStorms

FrontPage 2003: Forms

Audio Only Broadcast through Flash Media Live Encoder On Windows

Opening, Importing and Saving Images

How to Make the Most of Excel Spreadsheets

Creating a Poster Presentation using PowerPoint

Once you have obtained a username and password you must open one of the compatible web browsers and go to the following address to begin:

ECDL. European Computer Driving Licence. Spreadsheet Software BCS ITQ Level 2. Syllabus Version 5.0

Support/ User guide HMA Content Management System

Personal Computer Checklist (Google Chrome) RealPage, Inc.

Create an Access Database from within Visual Basic 6.

ACS Version Check Layout Design

SMS for Outlook. Installation, Configuration and Usage Guide

Kentico CMS 7.0 User s Guide. User s Guide. Kentico CMS

CMS Training Manual. A brief overview of your website s content management system (CMS) with screenshots. CMS Manual

Microsoft FrontPage 2003

Hands-On Lab. Building a Data-Driven Master/Detail Business Form using Visual Studio Lab version: Last updated: 12/10/2010.

HOUR 3 Creating Our First ASP.NET Web Page

SharePoint 2007 Get started User Guide. Team Sites

Last edited on 7/30/07. Copyright Syncfusion., Inc

DATA 301 Introduction to Data Analytics Microsoft Excel VBA. Dr. Ramon Lawrence University of British Columbia Okanagan

Intermediate PowerPoint

WP Popup Magic User Guide

Connect to an Oracle Database from Visual Basic 6 (Part 2)

Manual English KOI Desktop App 2.0.x

Enterprise Asset Management System

Unleashing Hidden Powers of Inventor with the API Part 1. Getting Started with Inventor VBA Hello Inventor!

Drupal Training Guide

Designing and Implementing Forms 34

U of S Course Tools. Customizing Tool Availability For Instructors

Using the Adventist Framework with your netadventist Site

Presentations and PowerPoint

Kentico CMS 5.5 User s Guide

Access 2000 and Visual Basic 6

Microsoft Word Quick Reference Guide. Union Institute & University

How to Filter and Sort Excel Spreadsheets (Patient-Level Detail Report)

Code::Blocks Student Manual

NEW FEATURE OVERVIEW SPRINGCM S APPROVAL WORKFLOW TOOL (AKA ROUTING AND APPROVAL WORKFLOW)

UOFL SHAREPOINT ADMINISTRATORS GUIDE

Excel Using Pivot Tables

Webforms on a Drupal 7 Website 3/20/15

Ansur Test Executive. Users Manual

Advanced Excel 10/20/2011 1

Microsoft Word Revising Word Documents Using Markup Tools

Brock University Content Management System Training Guide

How to test and debug an ASP.NET application

Quosal Form Designer Training Documentation

Salesforce CRM Enterprise Addin Guide

Transcription:

Create Visual Basic 6 Controls At Runtime Did you know that it's possible to create controls at runtime? That's right. If you wish, you can add additional controls to your form at runtime. This capability gives your Visual Basic program the ultimate in flexibility---allowing you to dynamically control the appearance of your form at runtime---not only the placement of controls, but also the type and number of controls that appear on the form. Many Visual Basic programmers are aware that it's possible to create controls at runtime by first creating a control array, and then adding additional members to the control array at runtime by using the Load Statement. This is the first method I'll be examining in this article. In addition, I'll also show you how can create controls at runtime entirely from scratch, without the need to first create a control array. Method 1---Using a Control Array to create controls To create a control at run time using this method you first must create a control array for the control you wish to dynamically create. In other words, if you want to create checkboxes at run time, you first must create a control array of checkboxes. For those of you unfamiliar with the term, a control array is a collection of controls on a form, all having the same name, and possessing unique Index property values. It's possible to create a Control array that has just a single 'member'---and that's what I'll do now. I'll begin by placing a Checkbox and a Command button on a form. http://www.johnsmiley.com/cis18.notfree/smiley007/smiley007.htm (1 of 12)3/28/2004 11:41:37 AM

In the Command Button, I'll be placing code to dynamically create a checkbox on the form at run time. More on that in a few minutes. First, I need to tell Visual Basic that the Checkbox is a member of a Control Array---I do that merely by changing its Index property from the default blank value to a number---in this case 0. http://www.johnsmiley.com/cis18.notfree/smiley007/smiley007.htm (2 of 12)3/28/2004 11:41:37 AM

Once the Index property has been set to 0, the Checkbox is now a member of the Check1 Checkbox Control Array---which makes creating a new checkbox at runtime very easy. All we need to do is tell Visual Basic that we want to create a new checkbox, using the existing Checkbox as a template. We do this by executing the Visual Basic Load Statement within the Click Event Procedure of the Command Button---like this. Private Sub Command1_Click() Load Check1(1) Check1(1).Caption = "New Checkbox" End Sub The Load statement Load Check1(1) tells Visual Basic to create a new member of the Check1 checkbox array---and to create it with an Index property of 1. This statement Check1(1).Caption = "New Checkbox" gives the Checkbox, whose index property is equal to 1, a unique caption to make it 'stand out' form the original checkbox placed on the form at design time. If we now run the program, and then click on the Command Button we'll see this screenshot http://www.johnsmiley.com/cis18.notfree/smiley007/smiley007.htm (3 of 12)3/28/2004 11:41:37 AM

Oops something's wrong. What I didn't tell you is that when you create a control at runtime, by default, the Visible property of the new control is set to False. To see the new control, we need to explicitly set its Visible property to True. Let's modify the code in the Click event procedure to look like this Private Sub Command1_Click() Load Check1(1) Check1(1).Caption = "New Checkbox" Check1(1).Visible = True End Sub Now let's run the program again, and click on the Command Button once more http://www.johnsmiley.com/cis18.notfree/smiley007/smiley007.htm (4 of 12)3/28/2004 11:41:37 AM

Something is still wrong---there's still just the single checkbox! The problem is this: when the new control is created, the properties of the new control are identical to the properties of the 'template' control used to create it---with the exception of the Index property which we set with the Load statement, and the Visible property which we know is initialized to False. Because of that, the new checkbox is on the form--- it just so happens to be sitting 'under' the first control, since it has identical Top, Left, Height and Width properties. All we need to do to see the new control is to move it away from the first control---and we can do that by adding a line of code to the Click Event procedure to change the Top property of the new control. Like this Private Sub Command1_Click() Load Check1(1) Check1(1).Caption = "New Checkbox" Check1(1).Visible = True Check1(1).Top = Check1(0).Top + Check1(0).Height End Sub This line of code http://www.johnsmiley.com/cis18.notfree/smiley007/smiley007.htm (5 of 12)3/28/2004 11:41:37 AM

Check1(1).Top = Check1(0).Top + Check1(0).Height tells VB to take the current value of the Top property of the existing checkbox, and to add to that the value of its Height Property (remember, the first control has an Index property of 0). The result of this addition is a Top property for the new control that is just under the first control. If we now run the program, and click on the Command Button, we'll see this screen shot Works like a charm! I should also point out that if we want to give the user the impression that we are creating controls totally from scratch, we can place the template control on the form---and then set its Visible property to False---that way, at run time, the controls are created---seemingly out of nowhere. To review, here s a summary of the steps necessary to create a new control using the Control Array method. 1. Create a control array of the control type you wish to create at runtime. If you want to create a textbox at runtime, create a Textbox control array. If you want to create a Command Button, create a Command Button Control array. Remember, to create a Control Array, all you need to do is to change the Index property http://www.johnsmiley.com/cis18.notfree/smiley007/smiley007.htm (6 of 12)3/28/2004 11:41:37 AM

of the control to something other than its default empty value. 2. Use the Load Statement, with a unique Index property, to create the new control. 3. Change the Visible property of the new control to True in order to make it visible. 4. Change the coordinate properties (Left or Top) to bring the new control out from under the original. Method 2---Creating controls from scratch using the Controls Collection There's a second method to create a control at runtime, and that's to use the Add Method of the Controls collection. This method is easier to use than the Control Array method, but harder to understand since it requires some familiarity with the Visual Basic Controls Collection. (I discuss the Controls Collection in my latest book, Learn To Program Objects with Visual Basic 6.) In short, each control that is placed on the form either at Design time or runtime is made a member of the intrinsic Visual Basic Collection called the Controls Collection. For those of you not familiar with Visual Basic Collections, a Collection is similar to a one dimensional array. Each control on the form has a reference placed on the Controls collection when it is placed there at design time. In the same way, a control that is placed on the form at run time (the way we just did using the Control Array Method) also has a reference placed on the Controls Collection. It's also possible to create a control at runtime by adding a reference to a control directly to the Controls Collection. Doing so avoids the necessity of first having to create a 'template' control on the form at design time---the reason for that is that VB maintains templates for all of the controls in the hidden Visual Basic Global Object called VB (again, more on this in my Objects Book). Suffice to say that all that is required to create a control at runtime using this method is to execute four lines of code, like this Private Sub Command1_Click() Dim ctlname As Control http://www.johnsmiley.com/cis18.notfree/smiley007/smiley007.htm (7 of 12)3/28/2004 11:41:37 AM

Set ctlname = Form1.Controls.Add("VB.TextBox", "Text1", Form1) ctlname.visible = True ctlname.top = Check1(0).Top + Check1(0).Height End Sub This line of code Dim ctlname As Control declares something known as an Object Variable---which is nothing more than a variable that contains, as a value, a reference to an Object (in this case, a Textbox). You can declare an Object variable as a specific control type (such as Textbox) or elect to declare it as the more generic Control type, which is what we did here. This line of code Set ctlname = Form1.Controls.Add("VB.TextBox", "Text1", Form1) tells Visual Basic to add a Textbox control called Text1 to the Controls Collection of Form1, and to use the Object Variable ctlname to 'point to it'. Once this control has been added to the Controls Collection, thereafter whenever we refer to the control using code we must refer to it by the Object Variable Name. That's why, when we then set the Visible property of the new textbox to True, and adjust its Top property, we use the Object Variable name instead. ctlname.visible = True ctlname.top = Check1(0).Top + Check1(0).Height More on the Add Method. The Add Method has three arguments---the first is the name of the template for the control you are creating, the second is the name of the control as it will appear in the Controls Collection, and the third argument is the control's container (ordinarily the form, but it could be the name of a Frame Control if you wanted the control to be placed 'within' a Frame on the form). http://www.johnsmiley.com/cis18.notfree/smiley007/smiley007.htm (8 of 12)3/28/2004 11:41:37 AM

The Textbox control is called Textbox, the Command Button control is called CommandButton. If you open the Visual Basic Object Browser (View-Object Browser from the Visual Basic Menu Bar) and select the VB Global Object in the library listbox http://www.johnsmiley.com/cis18.notfree/smiley007/smiley007.htm (9 of 12)3/28/2004 11:41:37 AM

the resulting display will show you the names for the controls that you can create at runtime http://www.johnsmiley.com/cis18.notfree/smiley007/smiley007.htm (10 of 12)3/28/2004 11:41:37 AM

Again, for more information on the Visual Basic Object Browser, check out my Objects book. If we now run the program, and click on the command button, we'll see this screenshot http://www.johnsmiley.com/cis18.notfree/smiley007/smiley007.htm (11 of 12)3/28/2004 11:41:37 AM

As you can see, the new Textbox control has been placed on the form. Summary The ability to add controls to a form at run time can produce incredibly dynamic forms. http://www.johnsmiley.com/cis18.notfree/smiley007/smiley007.htm (12 of 12)3/28/2004 11:41:37 AM