Addressing several objects on a form in 1 line of code

Post your Gambas programming questions here.
Post Reply
annisu
Posts: 1
Joined: Wednesday 15th January 2020 5:01am

Addressing several objects on a form in 1 line of code

Post by annisu »

Greetings!
I have this code:
Separator1.Foreground = Color.Green
Separator2.Foreground = Color.Green
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:
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.
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: Addressing several objects on a form in 1 line of code

Post by stevedee »

annisu wrote: Wednesday 15th January 2020 5:11am ...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:-
viewtopic.php?t=664


Edit: I just tried this code based on Quin's:-
Public Sub Button1_Click()
Dim mycontrol As Control
  For Each mycontrol In Me.Children
    If mycontrol Is Separator Then Object.SetProperty(mycontrol, "BackGround", Color.green)
  Next

End
...which works fine. However, I couldn't see any change when I tried to set the foreground, but that may be a separate problem.
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Addressing several objects on a form in 1 line of code

Post by cogier »

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: -
If mycontrol Is Separator Then mycontrol.BackGround = Color.green
User avatar
Quincunxian
Posts: 171
Joined: Sunday 25th June 2017 12:14am
Location: Western Australia

Re: Addressing several objects on a form in 1 line of code

Post by Quincunxian »

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.
 If ControlElement Is TextLabel Then Object.SetProperty(ControlElement, "Foreground", InColour)
Cheers - Quin.
I code therefore I am
User avatar
Cedron
Posts: 156
Joined: Thursday 21st February 2019 5:02pm
Location: The Mitten State
Contact:

Re: Addressing several objects on a form in 1 line of code

Post by Cedron »

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.
.
.
        SetSeparatorColors( Color.Green )
.
.
'========================================================
Public Sub SetSeparatorColors( ArgColor as Integer )

	
        Separator1.Foreground = ArgColor
        Separator2.Foreground = ArgColor
        Separator3.Foreground = ArgColor

End
'========================================================
.... and carry a big stick!
User avatar
grayghost4
Posts: 174
Joined: Wednesday 5th December 2018 5:00am
Location: Marengo, Illinois usa

Re: Addressing several objects on a form in 1 line of code

Post by grayghost4 »

it appears that foreground color does not work properly.

viewtopic.php?f=4&t=650&sid=6fc41c5bb40 ... 8aaac43ad7
cage
Posts: 123
Joined: Monday 2nd September 2019 5:47am
Location: Phoenix Arizona

Re: Addressing several objects on a form in 1 line of code

Post by cage »

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.
User avatar
grayghost4
Posts: 174
Joined: Wednesday 5th December 2018 5:00am
Location: Marengo, Illinois usa

Re: Addressing several objects on a form in 1 line of code

Post by grayghost4 »

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
User avatar
grayghost4
Posts: 174
Joined: Wednesday 5th December 2018 5:00am
Location: Marengo, Illinois usa

Re: Addressing several objects on a form in 1 line of code

Post by grayghost4 »

grayghost4 wrote: Tuesday 4th February 2020 5:13am 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 ???
Post Reply