Addressing several objects on a form in 1 line of code

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 » Wednesday 15th January 2020 5:11am

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: 214
Joined: Monday 20th March 2017 6:06pm

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

Post by stevedee » Wednesday 15th January 2020 10:16am

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: 507
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 » Wednesday 15th January 2020 4:56pm

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: 93
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 » Wednesday 15th January 2020 11:40pm

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: 120
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 » Thursday 16th January 2020 2:57pm

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: 68
Joined: Wednesday 05th December 2018 5:00am
Location: Concord, CA usa

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

Post by grayghost4 » Tuesday 04th February 2020 1:50am

it appears that foreground color does not work properly.

viewtopic.php?f=4&t=650&sid=6fc41c5bb40 ... 8aaac43ad7

cage
Posts: 76
Joined: Monday 02nd September 2019 5:47am
Location: Phoenix Arizona

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

Post by cage » Tuesday 04th February 2020 3:44am

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: 68
Joined: Wednesday 05th December 2018 5:00am
Location: Concord, CA usa

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

Post by grayghost4 » Tuesday 04th 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

User avatar
grayghost4
Posts: 68
Joined: Wednesday 05th December 2018 5:00am
Location: Concord, CA usa

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

Post by grayghost4 » Thursday 13th February 2020 7:43pm

grayghost4 wrote:
Tuesday 04th 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