Basic4ppc - Windows Mobile Development  

Go Back   Basic4ppc - Windows Mobile Development > Main Category > Questions & Help Needed
Home Register FAQ Members List Search Today's Posts Mark Forums Read

Questions & Help Needed Post any question regarding Basic4ppc.


Setting Control names as arrays.


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-28-2008, 02:29 AM
Junior Member
 
Join Date: Dec 2007
Posts: 27
Default Setting Control names as arrays.

A little while ago I read a Thread that wasn't specifically about this topic, but it did mention how to go about addressing Controls as arrays (eg. Ten Text boxes on the one form - a la Box(1), Box(2), ....)

I Believe that you can't do this in B4P, but in the thread that I mentioned above (which I cannot find again), I recall (Erel?) descibing how it can be achieved.

Can it be explained again or can I be pointed to the thread in question.

Big thanks.

Burd
Reply With Quote
  #2 (permalink)  
Old 01-28-2008, 03:05 AM
willisgt's Avatar
Senior Member
 
Join Date: Aug 2007
Location: Nacogdoches, Texas USA
Posts: 154
Default

There are two possibilities:

1.) Create your ten controls - let's use a textbox as an example - in the Designer. Name them textbox1, textbox2, textbox3, etc...

- or -

2.) Create the controls dynamically, like such...

Code:
Sub App_Start

	l = 5
	t = 5
	w = Form1.Width - ( l * 2 )
	h = 15

	For x = 1 To 10

		controlname = "textbox" & x
		txt = "textbox number " & x

		AddTextBox( "Form1", controlname, l, t, w, h, txt )
		
		t = t + Control( controlname ).Height + 2
	
	Next

	Form1.Show

End Sub

In either case, you can reference the controls in a manner similar to this:

Code:
Sub Button1_Click

	bkg = Rgb( Rnd( 0, 254 ), Rnd( 0, 254 ), Rnd( 0, 254 ) )
	fnt = Rgb( Rnd( 0, 254 ), Rnd( 0, 254 ), Rnd( 0, 254 ) )

	For x = 1 To 10
	
		controlname = "textbox" & x
	
		Control( controlname ).Color = bkg
		Control( controlname ).FontColor = fnt
	
	Next

End Sub
That's pretty much it!


Gary
Reply With Quote
  #3 (permalink)  
Old 01-28-2008, 08:53 AM
specci48's Avatar
Basic4ppc Expert
 
Join Date: Apr 2007
Location: Germany
Posts: 638
Default

As a small addition: If you want to use the control keyword in connection with "Optimized Compilation" you have to set the control type, too.

Instead of
Quote:
t = t + Control( controlname ).Height + 2
...
Control( controlname ).Color = bkg
Control( controlname ).FontColor = fnt
you should use this
Quote:
t = t + Control( controlname, TextBox ).Height + 2
...
Control( controlname, TextBox ).Color = bkg
Control( controlname, TextBox ).FontColor = fnt

specci48
Reply With Quote
  #4 (permalink)  
Old 01-28-2008, 03:59 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 3,134
Default

The runtime controls tutorial should help you: Runtime controls manipulation

Quote:
If you want to use the control keyword in connection with "Optimized Compilation" you have to set the control type, too.
With most properties (see Control keyword topic for the full list) it isn't required to add the control type.
The compiler will show a message when you try to compile if it is required.
Reply With Quote
  #5 (permalink)  
Old 01-28-2008, 07:06 PM
specci48's Avatar
Basic4ppc Expert
 
Join Date: Apr 2007
Location: Germany
Posts: 638
Default

Quote:
Originally Posted by Erel View Post
With most properties (see Control keyword topic for the full list) it isn't required to add the control type.
... haven't realized this yet.
As I remeber I had to add the control type to all my used contol statments when using "optimized compilation" on my progs. By the way, it was a very stupid work...

specci48
Reply With Quote
  #6 (permalink)  
Old 01-28-2008, 07:50 PM
Senior Member
 
Join Date: May 2007
Posts: 188
Awards Showcase
Beta Tester Competition Winner 
Total Awards: 2
Default controls

Burd
I think that you refer to my question in a thread.
I wanted to refer to many controls with the same action - like to change their color.
The solution is like this:
Define an array, for example : Dim boxes()
then allocate each control to an item in this array using strsplit:
boxes() = StrSplit("Form1,Panel2,TimeData2,MTime,Mspeed,Alt2 ,.................,Routetime",",")

then the action is simple:
For i = 10 To 35
Control(boxes(i)).Color = Rgb(255,255,255)
Control(boxes(i)).fontColor = Rgb(0,0,0)
Next i

The advantage of this solution is that it keeps the specific names of the controls, which you want to refer specifically with actions or values, without having to look-up every time in a list to find the control you need.

and It saves a lot of text....

edit: I just noticed that I wrote "next i " instead of just "next" (old habit from other languages), and the compiler doesn't mind !
__________________
David Erez
Ramat Hasharon, Israel

Last edited by derez : 01-28-2008 at 07:59 PM.
Reply With Quote
  #7 (permalink)  
Old 01-29-2008, 11:38 AM
Junior Member
 
Join Date: Dec 2007
Posts: 27
Default What can I say.....

Thanks folks so much. Collectively, beautifully explained (I need that). This is a GREAT help and has saved me about 500 lines of code as well!

Not only is this info much appreciated, but also this forum in bringing out the benevolent side of "software developers" where ever we are on the scale...... oh,.... I think I;m going to cry.... = (

Burd.
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Folder names in other languages sahoopes Questions & Help Needed 8 11-08-2008 12:57 PM
Case and AutoComplete control names agraham Beta Versions 1 09-18-2008 06:06 PM
Column names in a table pmu5757 Questions & Help Needed 2 05-07-2008 03:50 PM
Getting names of controls BjornF Questions & Help Needed 2 01-09-2008 03:05 PM
Setting multiple properties whilst only referring to the control once RandomCoder Basic4ppc Wishlist 1 10-13-2007 01:51 PM


All times are GMT. The time now is 02:53 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.1.0