Addressing several objects on a form in 1 line of code

Post

Posted
Rating:
#1 (In Topic #365)
Trainee
Greetings!
I have this code:

Code (gambas)

  1. Separator1.Foreground = Color.Green
  2. Separator2.Foreground = Color.Green
  3. Separator3.Foreground = Color.Green
And so on. There are 15 of those separators. This looks excessive and ugly.

If it were VB5, I'd solve this problem easily:

Code (gambas)

  1. Separator$.Foreground = Color.Green

But sadly this doesn't seem to work on Gambas…

Is there a way to call all the objects on the form with the same name but a different number?

Your help is much appreciated.
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Regular
stevedee is in the usergroup ‘Regular’

annisu said

…Is there a way to call all the objects on the form with the same name but a different number?

Yes, you probably need to use a Control Group.

Take a look at Quin's answer in this thread:-
https://forum.gambas.one/viewtopic.php?t=664


Edit: I just tried this code based on Quin's:-

Code (gambas)

  1. Public Sub Button1_Click()
  2. Dim mycontrol As Control
  3.   For Each mycontrol In Me.Children
  4.     If mycontrol Is Separator Then Object.SetProperty(mycontrol, "BackGround", Color.green)
  5.   Next
  6.  

…which works fine. However,  I couldn't see any change when I tried to set the foreground, but that may be a separate problem.
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Guru
cogier is in the usergroup ‘Guru’
Hi annisu and welcome to the forum and you are the 100th member "ta da!!"

I'm with Steve on this. I can't change the Foreground but the code works fine for the Background. I think you can simplify one line though to: -

Code (gambas)

  1. If mycontrol Is Separator Then mycontrol.BackGround = Color.green
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Expert
Quincunxian is in the usergroup ‘Expert’
I think the reason that this is not working is simply that it is not implemented in QT
Gambas sometimes has a property (Foreground) listed in the panel for a control (Separator) that has no effect.
I've come up against this a few times.
If there is no 'hook' in the underlying QT object for the property then neither setting it manually or using SetObject will work.

Also: In my example that Stevedee linked to there is an error  :oops:
The line to control elements of a Lable should read TextLable as below.

Code (gambas)

  1.  If ControlElement Is TextLabel Then Object.SetProperty(ControlElement, "Foreground", InColour)

Cheers - Quin.
I code therefore I am
Online now: No Back to the top

Post

Posted
Rating:
#5
Avatar
Regular
Cedron is in the usergroup ‘Regular’
Call me old fashioned, but to me the simplest solution is to shove all those lines off to a Sub or Gosub and then you have one calling line.

Code (gambas)

  1. .
  2. .
  3.         SetSeparatorColors( Color.Green )
  4. .
  5. .
  6. '========================================================
  7. Public Sub SetSeparatorColors( ArgColor as Integer )
  8.  
  9.        
  10.         Separator1.Foreground = ArgColor
  11.         Separator2.Foreground = ArgColor
  12.         Separator3.Foreground = ArgColor
  13.  
  14. '========================================================
  15.  

.... and carry a big stick!
Online now: No Back to the top

Post

Posted
Rating:
#6
Avatar
Enthusiast
GrayGhost is in the usergroup ‘Enthusiast’
it appears that foreground color does not work properly.

https://forum.gambas.one/viewtopic.php?f=4&t=650&sid=6fc41c5bb40bb6ce399a798aaac43ad7
Online now: No Back to the top

Post

Posted
Rating:
#7
Avatar
Regular
cage is in the usergroup ‘Regular’
 I have noticed this too with the newer updates.  Since QT4 is no longer used in the newer versions of Linux support for QT4 is no longer supported in Gamabs.  For a while I was using the QT4 to Qt5 switcher and it worked fine.  How ever lately I had to go to using GTK3 in order to change the foreground and background colors.  When Ubuntu 20.04 comes out QT4 will be no longer be included only QT5 and GTK3.  

Personally I use Arch and the latest update to Gambas QT4 is no longer part of Gambas.  Currently it's Gambas 3.14.3.  Another problem is some older programs that used strictly QT4 will no longer work with the latest version of Gambas.  So my suggestion is to base your programs on GTK and not QT.
Online now: No Back to the top

Post

Posted
Rating:
#8
Avatar
Enthusiast
GrayGhost is in the usergroup ‘Enthusiast’
 I am a new gambas user … I changed my program to GTk and had to change a few things … but the printer no longer prints ? I just get a blank page.

So it is back to QT for now at least
Online now: No Back to the top

Post

Posted
Rating:
#9
Avatar
Enthusiast
GrayGhost is in the usergroup ‘Enthusiast’

grayghost4 said

I am a new gambas user … I changed my program to GTk and had to change a few things … but the printer no longer prints ? I just get a blank page.

So it is back to QT for now at least

So I tried again and the reason the print was a blank page is because the printing coradanates are different
QT using   1200 dpi on my printer … But  GTK uses 70 dpi on the same printer so everything was printing off of the page

Why would they uses such different values for the same printer ???
Online now: No Back to the top
1 guest and 0 members have just viewed this.