Return a value from a class

Post your Gambas programming questions here.
User avatar
cogier
Site Admin
Posts: 1126
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Return a value from a class

Post by cogier »

If you used a class to make a MyMessage that had 2 Buttons, how do you get a value back to indicate that Button1 was clicked. I can do this with global variables but is there a simple way to do: -

iValue = MyMessage("Delete file?","No","Yes")

in the same way that Gambas does: -

iValue = Message("Delete file?","No","Yes")
User avatar
BruceSteers
Posts: 1577
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Return a value from a class

Post by BruceSteers »

Gambas just seems to do this...

 iBusy = Application.Busy
  Application.Busy = 0
  Me.ShowModal()
  Application.Busy = iBusy

  Return $iButton + 1

With this to set return value...
Public Sub Button_Click()
  
  $iButton = Last.Tag
  Me.Close
  
End

Here is the Gambas message function if you want to check out it's workings...
https://gitlab.com/gambas/gambas/-/tree ... rc/Message

I found a good way was to copy those 3 files and the Message folder into your project but rename Message.class to Msg.Class or something to give your command a different name so it does not conflict with gambas built in.
Last edited by BruceSteers on Tuesday 15th December 2020 11:52am, edited 1 time in total.
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1577
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Return a value from a class

Post by BruceSteers »

Check this out Charlie...

https://gitlab.com/bsteers4/gambas/-/co ... ancel-zero

It's a customised Gambas Message box that always returns Zero on Cancel (last) button

Well you have to first set...

Message.CancelIsZero = True

Then it will :)
If at first you don't succeed , try doing something differently.
BruceS
User avatar
cogier
Site Admin
Posts: 1126
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Return a value from a class

Post by cogier »

Thanks Bruce,

I notice that as of 15/12/2020 16:46, Benoît has updated 'Message' within the last 2 hours. This follows my concern regarding the closing of the Message Form by clicking the 'X' and the last button being returned. So for those who did not see this on the Gambas Mailing list using the following code and clicking the 'X' will return the last button, in this case, 'Yes'!
Public Sub Form_Open()

   ''Run this and close the Message by clicking the "X", NOT Yes or No.
   If Message.Question("Delete hard drive?", "No", "Yes") = 2 Then Print "Deleting hard drive"

End
I copied the new code with a few name changes, as you suggested, and now the 'X' in the Message Form has been disabled with: -
Public Sub Form_Close()

  If $iButton < 0 Then Stop Event

End


It is interesting to note that there are 2 classes used to be able to return the button pressed value. I was hoping it would be easier than that.
User avatar
BruceSteers
Posts: 1577
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Return a value from a class

Post by BruceSteers »

cogier wrote: Tuesday 15th December 2020 5:04pm Thanks Bruce,

I notice that as of 15/12/2020 16:46, Benoît has updated 'Message' within the last 2 hours. This follows my concern regarding the closing of the Message Form by clicking the 'X' and the last button being returned. So for those who did not see this on the Gambas Mailing list using the following code and clicking the 'X' will return the last button, in this case, 'Yes'!
Public Sub Form_Open()

   ''Run this and close the Message by clicking the "X", NOT Yes or No.
   If Message.Question("Delete hard drive?", "No", "Yes") = 2 Then Print "Deleting hard drive"

End
I copied the new code with a few name changes, as you suggested, and now the 'X' in the Message Form has been disabled with: -
Public Sub Form_Close()

  If $iButton < 0 Then Stop Event

End


It is interesting to note that there are 2 classes used to be able to return the button pressed value. I was hoping it would be easier than that.
I don't get why you want your buttons that way round anyway?
for as long as i remember it's been the default that the first button is the default and the last is the negative cancel so your buttons are simply the wrong way round. swap them round and all your issues go away.

But no, he's changed it all now...
https://gitlab.com/gambas/gambas/-/comm ... 233b2a7844

No keyboard control anymore :( (well not on gtk as you cannot see what button is focused) so mouse only.

And now the cancel button is always default, instead of 1st button.

Simple how he's cancelled window closing , $iButton is set to -1 on form load and only clicking a button changes it.

As for the 2 class files You shouldn't really need the Message.class file. all that does is provide the means to use Message.Info(), Message.Question(), etc.
You could delete Message.Class and rename FMessage.class (and form) and just use it directly.
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1577
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Return a value from a class

Post by BruceSteers »

cogier wrote: Tuesday 15th December 2020 5:04pm Thanks Bruce,
Public Sub Form_Open()

   ''Run this and close the Message by clicking the "X", NOT Yes or No.
   If Message.Question("Delete hard drive?", "No", "Yes") = 2 Then Print "Deleting hard drive"

End
Did you try hitting Escape with that new commit of Benoits? bye bye hard drive ;)
If at first you don't succeed , try doing something differently.
BruceS
ocoquet
Posts: 36
Joined: Friday 18th December 2020 7:56am

Re: Return a value from a class

Post by ocoquet »

Hello,
Simple way: you can just add a value (true or false) in Button1.tag and test it in another class.

Regards.
Olivier Coquet
Gambas Dev
Le Forum développeur Gambas
User avatar
cogier
Site Admin
Posts: 1126
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Return a value from a class

Post by cogier »

BruceSteers wrote: Wednesday 16th December 2020 4:16am Did you try hitting Escape with that new commit of Benoits? bye bye hard drive ;)
I think the plan is that the 'Cancel' button is placed last, apparently this is a Linux 'standard'. The focus has been changed from the first to the last button. So I think it should be written as below. Pressing 'Escape' will now trigger the correct response as will pressing 'Enter'.
If Message.Question("Delete hard drive?", "Yes", "No") = 1 Then Print "Deleting hard drive"
ocoquet wrote: Friday 18th December 2020 8:07am Hello,
Simple way: you can just add a value (true or false) in Button1.tag and test it in another class.
I understand, but I wanted to be able to this.
iValue = MyMessage("Delete file?","No","Yes")
User avatar
BruceSteers
Posts: 1577
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Return a value from a class

Post by BruceSteers »

cogier wrote: Friday 18th December 2020 3:34pm I think the plan is that the 'Cancel' button is placed last, apparently this is a Linux 'standard'. The focus has been changed from the first to the last button. So I think it should be written as below. Pressing 'Escape' will now trigger the correct response as will pressing 'Enter'.
If Message.Question("Delete hard drive?", "Yes", "No") = 1 Then Print "Deleting hard drive"
Well yes , it's the standard, and with the buttons that way round the close window button enabled isn't a problem either.
now you have to move focus to Yes button, pressing escape or enter will give same negative answer. there is not default positive any more.
ocoquet wrote: Friday 18th December 2020 8:07am Hello,
Simple way: you can just add a value (true or false) in Button1.tag and test it in another class.
cogier wrote: Friday 18th December 2020 3:34pm I understand, but I wanted to be able to this.
iValue = MyMessage("Delete file?","No","Yes")
Just No Charlie , behave yourself ;) lol
If at first you don't succeed , try doing something differently.
BruceS
ocoquet
Posts: 36
Joined: Friday 18th December 2020 7:56am

Re: Return a value from a class

Post by ocoquet »

in attachment, this make exactly what you want !
Don't forget to make public each control in the msg window :P

Olivier
here are the src code:

for main window:
' Gambas class file


Public Sub Button1_Click()

TextBox1.text = MyMessage("un test", "ok", "truc")

End

Public Function MyMessage(msg As String, ok_btn As String, cancel_btn As String) As Integer

msg_frm.btn_ok.text = ok_btn
msg_frm.btn_cancel.text = cancel_btn
msg_frm.msg_lbl.text = msg

Return msg_frm.ShowModal()

End


and for msg window

Public Sub btn_ok_Click()

Me.Close(1)

End

Public Sub btn_cancel_Click()

Me.Close(2)

End
Attachments
crashtest-0.0.1.tar.gz
Make exactly what you want
(11.81 KiB) Downloaded 245 times
Olivier Coquet
Gambas Dev
Le Forum développeur Gambas
Post Reply