English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I am making a quotefalls word game and need to make up to 100 text boxes at runtime. I need one event handler for all of them and I need to be able to easily iterate through all of them.

I was thinking in terms of an array of text boxes but maybe that won't work???

I read in a quote from a text file and then I need to create the grid that the letters will eventually go in for each letter in the quote.

2007-08-08 12:57:34 · 2 answers · asked by Anonymous in Computers & Internet Programming & Design

if you look here
http://msdn2.microsoft.com/en-us/library/kxt4418a(VS.80).aspx
you will see everything I am talking about toward the bottom of the page except how to deal with a ton of replicated controls.

2007-08-08 13:07:31 · update #1

2 answers

When defining the textbox, assign its index to 0. To do this right click on the text box in the design mode for the form to show the textbox property window. (where you change it's size, name, etc) The index property defaults to nothing. change it to 0. This will create the control array. The event handler will get passed the index of the control that caused the event to fire.

You simply have to create the 0th element in the array as a prototype. Then you can load the other instances of the textbox at run time and assign their Top and Left properties to place them im the appropriate place (1-99). Setting the default properties for the 0th element will set the default for the rest of the controls loaded.

for example:
dim Top as integer
dim Left as integer
dim Width as integer
dim Height as integer

Height = txtLetter.Height
Width = txtLetter.Width
Top = txtLetter.Top
for i = 1 to 99:
if (i MOD 10) = 0 then
Top = Top + Height
Left = txtLetter.Left - Width
end if
load txtLetter(i)
Left = Left + Width
txtLetter(i).Top = Top
txtLetter(i).Left = Left
next i

2007-08-08 13:07:03 · answer #1 · answered by Dave H 4 · 0 2

Control Arrays are not supported in Dot Net.

VB6 does have control arrays but VB2005 doesn't...

You can dynamically add events to a new control by assigning the new control event to a preexisting event handler.

The following code will dynamically add the Button4 click event to the prewritten Button1_click event handler when button 3 is pressed.
Prior to running this code button 4 doesn't have an event handler so clicking it has no effect until button 3 is pressed.
Once button 3 executes the prewritten Button1 click event will handle button4.click events. Button4 will now fire the Button1_Click event handler when button4 is pressed

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim myBtn As Button
myBtn = Button4

AddHandler myBtn.Click, AddressOf Button1_Click


End Sub

As you create a new button you can use the AddHandler method to assign an event handler



Another option for dealing with text boxes on a form is to use the forms KeyPreview method to examin each keypress of the active control. This is easy to impliment if you are trying to limit what a user can enter into a text box. (numeric data vs alpha data etc)

In the forms keypress event you can examin each character as it is entered with a select case statement and either accept(do nothing) or cancel (by setting the char to 0 and setting handled to true)

You can use the tag property to store a code to define how you want to validate keypresses (N for numeric, A for Alpha)
Based on the tag you would execute the relavent select case statement.

2007-08-08 13:54:26 · answer #2 · answered by MarkG 7 · 1 0

fedest.com, questions and answers