Using the same click event

New to Gambas? Post your questions here. No question is too silly or too simple.
Post Reply
pusherman
Posts: 16
Joined: Sunday 5th November 2023 11:45am

Using the same click event

Post by pusherman »

Hi all,

If you have an entry in the file menu, for example Menu_File_OpenFile and you also have a button the form, for example BtnOpenFile, they will have their own click events even though they will do the same thing. I know that you could call the same routine from within those events but it would save some code if both shared the same click event. Is this possible?

Thanks
Pusherman
User avatar
BruceSteers
Posts: 1579
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Using the same click event

Post by BruceSteers »

You can set their Group Property to the same thing in the IDE

if the Group property is set the event will be of the Group name
So if you set both the menu and the button to be group MyFunctions
they will both trigger..
Public Sub MyFunctions_Click()

End

Untitled.png
Untitled.png (300.45 KiB) Viewed 4084 times
You can tell what was pressed using the Last keyword.
You can also make many buttons/menus use the same event
For example below i set many buttons/menus to a single Group/Functions event...
For the example i have a menu named mnuBegin and button named btnBegin , both Group is MyFunctions
It's most likely you will not need to know if it's the menu or button and just run the function.

Public Sub MyFunctions_Click()

Dim bIsButton as Boolean =  Last Is Button

  Select Mid(Last.Name, 4)  ' the name less the first 3 chars
    Case "Begin"
      Print "Begin " & if(bIsButton, "Button was clicked", "Menu was selected")
  End Select

End




also it is easy if you are creating buttons/menu's via code as you can give them the same name.
eg..
Public Sub Form_Open()

Dim m as Menu = New Menu(Menu1) As "MyFunction"
m.Text = "Begin"
m.Name = "Begin"

Dim b as Button = New Button(Panel1) As "MyFunction"
b.Text = "Begin"
b.Name = "Begin"
End


Public Sub MyFunction_Click()

Dim bIsButton as Boolean =  Last Is Button

  Select Last.Name  ' the name is the same for matching button/menu
    Case "Begin"
      Print "Begin " & if(bIsButton, "Button was clicked", "Menu was selected")
  End Select

End

If at first you don't succeed , try doing something differently.
BruceS
pusherman
Posts: 16
Joined: Sunday 5th November 2023 11:45am

Re: Using the same click event

Post by pusherman »

Thanks for replying.

When I look in the Group property (where your arrow is indicating) I get a drop down box with nothing in it. I can't manually edit that property.

How do you create the group that goes into that property field?

Pusherman
User avatar
BruceSteers
Posts: 1579
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Using the same click event

Post by BruceSteers »

pusherman wrote: Thursday 9th November 2023 7:51pm Thanks for replying.

When I look in the Group property (where your arrow is indicating) I get a drop down box with nothing in it. I can't manually edit that property.

How do you create the group that goes into that property field?

Pusherman
I think it's a bug in the IDE/GTK Toolkit
Just click the Group box and type a name and then press return
then click another property and you will see the text appear in the Group box
It works but you cannot see the text as you type :(


or run the IDE with QT as i think it's only a gtk problem

$ env GB_GUI=gb.qt5 gambas3
If at first you don't succeed , try doing something differently.
BruceS
pusherman
Posts: 16
Joined: Sunday 5th November 2023 11:45am

Re: Using the same click event

Post by pusherman »

That works!

Thanks for your time :)
User avatar
cogier
Site Admin
Posts: 1127
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Using the same click event

Post by cogier »

I have run into this issue on several occasions, and any amount of typing in the Group property box gets me nowhere. I discovered that if you close the IDE and then go to the .src folder of your program and edit the Form in question you can add the Group component. Here is a .form file with 2 buttons on the Form:-

Code: Select all

# Gambas Form File 3.0

{ Form Form
  MoveScaled(0,0,105,55)
  { Button1 Button
    MoveScaled(40,13,16,4)
  }
  { Button2 Button
    MoveScaled(40,25,16,4)
  }
}
If you edit the file introducing a Group called MyGroup like this: -

Code: Select all

# Gambas Form File 3.0

{ Form Form
  MoveScaled(0,0,105,55)
  { Button1 Button MyGroup
    MoveScaled(40,13,16,4)
  }
  { Button2 Button MyGroup
    MoveScaled(40,25,16,4)
  }
}
If you now open the IDE and look at the Group property for both buttons, you will see that the property is now populated.
pusherman
Posts: 16
Joined: Sunday 5th November 2023 11:45am

Re: Using the same click event

Post by pusherman »

Thanks for replying

Sometimes it works and others not. Doing it manually like you suggest is another work-around.
Post Reply