How do I use 'created' controls?

New to Gambas? Post your questions here. No question is too silly or too simple.
Post Reply
Doctor Watson
Posts: 52
Joined: Wednesday 22nd August 2018 7:55am

How do I use 'created' controls?

Post by Doctor Watson » Tuesday 27th April 2021 6:04am

Well, here’s another beginners question.
I’m trying for the first time to ‘create’ a Control and use it, like this:
' Gambas class file
Public MyButton As New Button(Me)

Public Sub Form_Open()
    
  With MyButton
    .top = 10
    .left = 50
    .width = 60
    .height = 30
    .text = "Click !"
  End With
  
End

Public Sub MyButton_Click()
Quit
End
Everything looks fine. The button appears where it should. Gambas doesn’t throw any errors at me.
So why doesn’t clicking MyButton work?
I can’t find any really clear and simple examples on how to use created controls.
Old african saying:
You eat an elephant one small bite at a time.

User avatar
BruceSteers
Posts: 470
Joined: Thursday 23rd July 2020 5:20pm

Re: How do I use 'created' controls?

Post by BruceSteers » Tuesday 27th April 2021 8:12am

You need to set the handler name using As

Dim myButton As Button = New Button(Me) As "MyButton"

Probably best to not create the button in the public namespace and put on the form open function.

So...

Public MyButton As Button

Public Sub Form_Open()

With MyButton = New Button(Me) As "MyButton"

Etc....
Wishing well
Bruce

If at first you don't succeed , try it differently.

Doctor Watson
Posts: 52
Joined: Wednesday 22nd August 2018 7:55am

Re: How do I use 'created' controls?

Post by Doctor Watson » Tuesday 27th April 2021 9:33am

Thanks Bruce.
Now I'm getting somewhere .....
But it would be a lot easier for a beginner if there were some examples to be found, wouldn't it.
Another bite of the elephant gone ;)
Old african saying:
You eat an elephant one small bite at a time.

User avatar
cogier
Site Admin
Posts: 689
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: How do I use 'created' controls?

Post by cogier » Tuesday 27th April 2021 11:36am

But it would be a lot easier for a beginner if there were some examples to be found, wouldn't it.
Have a look at this example here.

The use of Quit is not recommended for GUI programs, you should use Me.Close.

User avatar
BruceSteers
Posts: 470
Joined: Thursday 23rd July 2020 5:20pm

Re: How do I use 'created' controls?

Post by BruceSteers » Tuesday 27th April 2021 12:34pm

Doctor Watson wrote:
Tuesday 27th April 2021 9:33am
Thanks Bruce.
Now I'm getting somewhere .....
But it would be a lot easier for a beginner if there were some examples to be found, wouldn't it.
Another bite of the elephant gone ;)
You are using the New keyword.
See the wiki for New
http://gambaswiki.org/wiki/lang/new

the examples you have not yet found are there.

Less speed more read my friend ;)
Wishing well
Bruce

If at first you don't succeed , try it differently.

Post Reply