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
Using the same click event
- BruceSteers
- Posts: 1609
- Joined: Thursday 23rd July 2020 5:20pm
- Location: Isle of Wight
- Contact:
Re: Using the same click event
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..
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.
also it is easy if you are creating buttons/menu's via code as you can give them the same name.
eg..
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
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
BruceS
Re: Using the same click event
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
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
- BruceSteers
- Posts: 1609
- Joined: Thursday 23rd July 2020 5:20pm
- Location: Isle of Wight
- Contact:
Re: Using the same click event
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
BruceS
Re: Using the same click event
That works!
Thanks for your time
Thanks for your time
- cogier
- Site Admin
- Posts: 1129
- Joined: Wednesday 21st September 2016 2:22pm
- Location: Guernsey, Channel Islands
Re: Using the same click event
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:-
If you edit the file introducing a Group called MyGroup like this: -
If you now open the IDE and look at the Group property for both buttons, you will see that the property is now populated.
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)
}
}
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)
}
}
Re: Using the same click event
Thanks for replying
Sometimes it works and others not. Doing it manually like you suggest is another work-around.
Sometimes it works and others not. Doing it manually like you suggest is another work-around.