Sort of "concatination" question

Post

Posted
Rating:
#1 (In Topic #784)
Trainee
 I hope someone can point me in the right direction with this attempt at concatenation.
I control a number of radios. Each radio has a container to hold the display parameters for that radio. For example:
Tbfrequencyradio1, textbox to hold the frequency the radio is tuned to
Tbstationnameradio1, textbox to hold the station name
Group is gradio1

And so on for radio2 and radio3.
To alter the radio parameters, I can click on the wanted textbox and alter it. This approach means the same code repeated for each radio.
Instead I would like to call a subroutine
 changeradioinfo("radio1")
so I can do:
Changeradioinfo(whichradio as string)
And then in someway….
 tbfrequencywhichradio.text = "123•456"

This way I have only one set of code to deal with.

I have tried:
tbfrequency&whichradio ……error
Tbfrequency[whichradio] ……error
Even tried "!"
Tbfrequency!whichradio…..error
Is there a way to do this "concatination"? Or am I going about this the wrong way?
Thanks for any help
M
Online now: No Back to the top

Post

Posted
Rating:
#2
Guru
BruceSteers is in the usergroup ‘Guru’
I think i get your problem, if it is getting an object/control from a string then you can address an object/control via a string using a forms control array, just use the name of the form the textbox is in or "Me" if coding in that form.

Ie..

Code (gambas)

  1.  
  2. Dim tBox As TextBox = Me["tbfrequency" & whichradio]
  3. tBox.Text = "123•456"
  4.  
  5. ' or''
  6.  
  7. Dim tBox As TextBox = fMain["tbfrequency" & whichradio]
  8. tBox.Text = "123•456"
  9.  
  10.  

if whichradio = "Radio1" then it will get the textbox called tbfrequencyRadio1
Online now: No Back to the top

Post

Posted
Rating:
#3
Trainee
 Thanks very much ….works… now to reduce the size of my source file!
Interesting that this form of construct is case sensitive.
I had a problem initially as I had named the control "Radio1" (notice upercase R) but passed the name "radio1" lower case R, just cause I am lazy! It works for the "normal" construct:

 Fmain.tbfrequencyradio1.text is the same as  Fmain.tbfrequencyRadio1.text (the R is either UC or LC)
Anyway sorted, stops me being lazy!

On last question if I may:
is there a form of fmain["tbfrequency" & whichradio].text that works. If I try this I get an error.

This is one of my learning issues with Gambas, I find it hard to generalise the information I get, or even to find the info in the first place:roll:
Is there a hidden Gambas repository that contains this knowledge!

Thanks M
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Guru
cogier is in the usergroup ‘Guru’

firstsolo said


On last question if I may:
is there a form of.text that works. If I try this I get an error.


You can change the Font, Bold, Background and Foreground.

 

Code (gambas)

  1. With  fmain["tbfrequency" & whichradio]
  2.    .Font.Bold = True
  3.    .Font.Size = 14
  4.    .Background = Color.Yellow
  5.    .Foreground = Color.Red
Online now: No Back to the top

Post

Posted
Rating:
#5
Trainee
Hi Cogier Tried this

Code (gambas)

  1.  With FMain["tbfrequency" & whichradio]
  2.     .Font.bold = True
  3.     .tag = "tag"
  4.     .text = "Test"
  5.  
Font works, as does tag,
but text returns "unknown Symbol "text" in class "control" in……
When I "debug"  FMain["tbfrequency" & whichradio] Then the resultant debug display shows "text" as on of the entries for the control.
Still I have a working solution that saves me a lot of duplicate code, so I am very happy.
The issues with trying to do something like..

Code (gambas)

  1. temp =  FMain["tbfrequency" & whichradio].tab
  2. temp =  FMain["tbfrequency" & whichradio].text
  3.  

it just seemed more "elegant"! I guess I am just missing some fundamental Gambas concepts..

Thanks for your ideas, filed for the future

M
Online now: No Back to the top

Post

Posted
Rating:
#6
Guru
BruceSteers is in the usergroup ‘Guru’
 Using the fMain[] method get a "Control"  the Control.class does not have .Text etc.
you must do it like this…

Dim tb as TextBox = fMain["CotrolName"]

then you can…

tb.Text

fMain["CotrolName"].Text will not work
Online now: No Back to the top

Post

Posted
Rating:
#7
Guru
BruceSteers is in the usergroup ‘Guru’
if you type fMain["controlName"].
(notice the dot)

once pressing the dot a completion list listing all available properties/methods for the control pops up.


your code will work like this…

Code (gambas)

  1. Dim tb As TextBox = FMain["tbfrequency" & whichradio]
  2.   With tb
  3.    .Font.bold = True
  4.    .tag = "tag"
  5.    .text = "Test"
  6.  
Online now: No Back to the top

Post

Posted
Rating:
#8
Trainee
 Hi Bruce, thanks for the info, I do know about the "." but did not use it! I assumed that if ".tag" worked then ".text" would or should… Anyway your original post has me re-doing all my code to be simpler, so a great result.
As to why ".text" does not work but ".tag" does, I will leave for another day!

Thanks to all for the help
M
Online now: No Back to the top

Post

Posted
Rating:
#9
Guru
BruceSteers is in the usergroup ‘Guru’
 I'll try to make it make sense…
It's the way controls/objects are made.

Control.class is the parent class of all controls like TextBox.class

the TextBox.class Inherits Control.class and adds things like the .Text property

So a TextBox is a control with added extras.

the Control.class has things like .Font and .Background so you can access a TextBox font using the control array but to access the additional things like .Text you must use the TextBox identifier.

Hope that helps
Online now: No Back to the top

Post

Posted
Rating:
#10
Trainee
 It does help.
I have seen references to inheritance but skip over them as "difficult" to understand. Your explanation may well have opened the door to getting past the "baby stage" in Gambas programming and allow me to grow.

Many thanks
M
Online now: No Back to the top

Post

Posted
Rating:
#11
Guru
BruceSteers is in the usergroup ‘Guru’

firstsolo said

It does help.
I have seen references to inheritance but skip over them as "difficult" to understand. Your explanation may well have opened the door to getting past the "baby stage" in Gambas programming and allow me to grow.

Many thanks
M

Inheritance is easier than you'd think.
Here's a simple example…

Consider this class file, I'll call it TextBoxT.class …

Code (gambas)

  1. ' Gambas class file
  2.  
  3.  
  4. Property Tag2 As Variant Use $vTag2
  5. '
  6.  

now you have a TextBoxT that does everything a TextBox does but also has an extra Tag property TextBoxT.Tag2

You can add Public functions and other properties to the class all accessible using TextBoxT like any textbox.

PS.
To see the Tag2 property (or any others you make) in the IDE form designer you'd need to add them to the _Properties const..

Public Const _Properties As String = "*,Tag2"
(using * states all textbox properties)

this way if you want it in the form designer …

Code (gambas)

  1.  
  2. ' Gambas class file
  3. Export  ' need this to be visible in the form designer
  4.  
  5. Public Const _Properties As String = "*,Tag2" ' only do this if you need the properties in the form designer
  6. Public Const _DrawWith As String = "TextBox" ' the form designer render it like a textbox
  7. Public Const _Similar As String = "TextBox" ' with this in the form designer you can change a textbox into a textboxt with right click
  8.  
  9. Property Tag2 As Variant Use $vTag2
  10. '
  11.  
Online now: No Back to the top

Post

Posted
Rating:
#12
Guru
BruceSteers is in the usergroup ‘Guru’
How's this..
Class name  RadioTextBox.class

Code (gambas)

  1. '
  2. ' Gambas class file
  3.  
  4. Public Const _Properties As String = "*,RadioName"
  5. Public Const _DrawWith As String = "TextBox"
  6. Public Const _Similar As String = "TextBox"
  7.  
  8. '' Get or set the radio name, auto inserts the frequency text.
  9. Property RadioName As String
  10.  
  11. Private $sRadio As String
  12.  
  13. Static Private $RadioFreqs As New Collection  ' the list of frequencies matching radio names.
  14.  
  15.  
  16. Static Public Sub _init()
  17.  
  18.   If $RadioFreqs.Count Then Return ' if static collection is already built then return
  19.  
  20. ' build the collection adding radio frequency data to the corresponding radio names.
  21.   $RadioFreqs.Add("123*456", "Radio1")
  22.   $RadioFreqs.Add("135*632", "Radio2")
  23.  
  24.  
  25. ' this function is used when reading RadioTextbox1.RadioName
  26. Private Function RadioName_Read() As String
  27.  
  28.   Return $sRadio
  29.  
  30.  
  31. ' this function is used when writing RadioTextbox1.RadioName
  32. ' this is where we adjust things due to the change.
  33. Private Sub RadioName_Write(Value As String)
  34.  
  35.   If Not $RadioFreqs.Exist(Value) Then
  36.     Message.Error("Radio name " & Value & " does not exist!")
  37.     Return
  38.  
  39.   $sRadio = Value
  40.   Me.Text = $RadioFreqs[$sRadio]
  41.  
  42. '
  43.  

there I have a Textbox that i can do the following…

Code (gambas)

  1. '
  2. RadioTextBox1.RadioName = "Radio2"
  3. '
  4.  

the frequency is chosen using a collection of the RadioNames as keys by setting .RadioName the textbox auto updates itself.

Hopefully that will help get you started :)
All the best
Online now: No Back to the top

Post

Posted
Rating:
#13
Trainee
OK, wow thanks for the "tutorial" its very useful and adds to the previous explanation  of yours.
Spent today re-doing my software with your information and it has resulted in much better readability and a big reduction in lines of code! I like that!!
BIG THANKS!

I wonder if the forum would support "tagging" post like yours with "tutorial", sort of like what Steve Dee has done with some of his later "Did You Know" posts
<COLOR color="#FF0080">
level: Beginner
Type: Tutorial
category: Editor
subject: the TAB key
</COLOR>
Then indexing the post (I assume its a SQL DB of some sort) so it would be possible to get to find these sorts of really useful bits of teaching. The original question may have to be re-done, as like mine I did not know the right question to ask!
As I went through a large number of posts to look for an answer, I did try and copy many of the "snippets" but it got too much! There is so much great teaching here on the forum, one could write the definitive "Dummies guide to Gambas" with it!

Anyway onwards and upwards.
M
Online now: No Back to the top
1 guest and 0 members have just viewed this.