View previous topic :: View next topic |
Author |
Message |
Lynn
Joined: 27 Aug 2004 Posts: 89
|
Posted: Sun Dec 26, 2004 9:06 pm Post subject: HowTo: RMB context menu from Button? |
|
|
I'd like to be able to have a context menu show up when I use the right-mouse-button on a Button control. Looking at the documentation for button, control, and menu, this looks possible, but the implementation isn't clear to me.
I'm able to extend the default ContextMenu for a TextBox, but there doesn't seem to be a context menu actually in existence for a Button. Do I have to create it, load it, and get it to show up?
Here's what will compile, but nothing happens as far as a context menu showing up when I RMB on the Search button. Do I need OnPopUp code?
ContextMenu sbcm = _searchButton.contextMenu;
sbcm = new ContextMenu();
int count = sbcm.menuItems.count;
MenuItem miAllWordsSearch = new MenuItem;
miAllWordsSearch.text = "All Words";
miAllWordsSearch.index = count;
sbcm.menuItems.add(miAllWordsSearch);
MenuItem miCaseSensitiveSearch = new MenuItem;
miCaseSensitiveSearch .text = "Medium Font";
miCaseSensitiveSearch .index = (count + 1);
sbcm.menuItems.add(miCaseSensitiveSearch );
(Sample code would be appreciated of how this works together. Will it basically be similar to how TextBox works with its default RMB context menu? I can probably hack something together by trial & error, but would appreciate guidance on the preferred DFL way to accomplish this. THANKS!) |
|
Back to top |
|
|
Lynn
Joined: 27 Aug 2004 Posts: 89
|
Posted: Sun Dec 26, 2004 10:20 pm Post subject: |
|
|
I put this together as sample code for adding a popup context menu for a Button. I added it to the "MiniCalc" tutorial code. It doesn't actually do much that is meaningful ... allows the first-number to be double, tripled, or halved from the "Plus" RMB context menu.
I thought I'd check with you if this is a reasonable approach that is consistent with "The DFL Endorsed Way". With your concurrence, I'll add it to the DFL tutorial section.
My impression is that you have in mind better ways to accomplish this, so I wanted to get your feedback and guidance prior to submitting it as a DFL tuturial example. I didn't want to post flawed sample code for others to possibly use as a template.
Code: |
// To compile:
// dmd DflTest dfl.lib -L/exet:nt/su:windows:4.0
private import std.string;
private import dfl.form, dfl.event, dfl.drawing, dfl.application;
private import dfl.button, dfl.textbox, dfl.menu;
class MiniCalcForm: Form
{
this()
{
text = "DflMiniCalc";
CreateOtherControls();
CreatePlusButtonWithRmbMenu();
acceptButton = _plusButton;
}
private void CreateOtherControls()
{
size = Size(120, 150); // Overall form size
_minusButton = new Button;
_minusButton.parent = this;
_minusButton.text = "Minus";
_minusButton.left = 60;
_minusButton.size = Size(50, 25);
_minusButton.click ~= &onMinusClick;
_firstNumberText = new TextBox;
_firstNumberText.parent = this;
_firstNumberText.text = "170";
_firstNumberText.location = Point(10, 30);
_firstNumberText.size = Size(100, 20);
_secondNumberText = new TextBox;
_secondNumberText.parent = this;
_secondNumberText.text = "252";
_secondNumberText.location = Point(10, 55);
_secondNumberText.size = Size(100, 20);
_resultNumberText = new TextBox;
_resultNumberText.parent = this;
_resultNumberText.text = "";
_resultNumberText.location = Point(10, 80);
_resultNumberText.size = Size(100, 20);
}
private void CreatePlusButtonWithRmbMenu()
{
_plusButton = new Button;
_plusButton.parent = this;
_plusButton.text = "Plus";
_plusButton.left = 10;
_plusButton.size = Size(50, 25);
_plusButton.click ~= &onPlusClick;
_plusButton.contextMenu = new ContextMenu;
MenuItem miDouble = new MenuItem;
miDouble.text = "Double";
miDouble.click ~= &pbcmDouble;
miDouble.index = 0;
_plusButton.contextMenu.menuItems.add(miDouble);
MenuItem miTriple = new MenuItem;
miTriple.text = "Triple";
miTriple.click ~= &pbcmTriple;
miTriple.index = 1;
_plusButton.contextMenu.menuItems.add(miTriple);
MenuItem miSeparator = new MenuItem;
miSeparator.text = "-";
miSeparator.index = 2;
_plusButton.contextMenu.menuItems.add(miSeparator);
MenuItem miHalf = new MenuItem;
miHalf.text = "Half";
miHalf.click ~= &pbcmHalf;
miHalf.index = 3;
_plusButton.contextMenu.menuItems.add(miHalf);
}
private void onPlusClick(Object sender, EventArgs ea)
{
GetNumbers();
_result = _num1 + _num2;
_resultNumberText.text = std.string.toString(_result);
}
private void onMinusClick(Object sender, EventArgs ea)
{
GetNumbers();
_result = _num1 - _num2;
_resultNumberText.text = std.string.toString(_result);
}
void pbcmDouble(Object sender, EventArgs ea)
{
GetNumbers();
_num1 += _num1;
_firstNumberText.text = std.string.toString(_num1);
}
void pbcmHalf(Object sender, EventArgs ea)
{
GetNumbers();
_num1 /= 2;
_firstNumberText.text = std.string.toString(_num1);
}
void pbcmTriple(Object sender, EventArgs ea)
{
GetNumbers();
_num1 += (_num1 + _num1);
_firstNumberText.text = std.string.toString(_num1);
}
private void GetNumbers()
{
char[] firstValue = _firstNumberText.text;
char[] secondValue = _secondNumberText.text;
_num1 = atoi(firstValue);
_num2 = atoi(secondValue);
}
private TextBox _firstNumberText;
private TextBox _secondNumberText;
private TextBox _resultNumberText;
private Button _plusButton;
private Button _minusButton;
private long _num1;
private long _num2;
private long _result;
}
int main()
{
Application.run(new MiniCalcForm);
return 0;
}
|
|
|
Back to top |
|
|
Lynn
Joined: 27 Aug 2004 Posts: 89
|
Posted: Sun Dec 26, 2004 10:45 pm Post subject: |
|
|
Here's some revised code for the previous MiniCalcWithRmd sample code. The "Double", "Triple", and "Half" RMB options share common onPbcmHandler code. Does this seem like a preferred approach suitable for a possible "Tutorial example", or is this perhaps inappropriate complexity?
Or is "The Endorsed DFL Way" something different for how this should be done?
Code: |
private void CreatePlusButtonWithRmbMenu()
{
_plusButton = new Button;
_plusButton.parent = this;
_plusButton.text = "Plus";
_plusButton.left = 10;
_plusButton.size = Size(50, 25);
_plusButton.click ~= &onPlusClick;
_plusButton.contextMenu = new ContextMenu;
MenuItem miDouble = new MenuItem;
miDouble.text = "Double";
miDouble.click ~= &onPbcmHandler;
miDouble.index = 0;
_plusButton.contextMenu.menuItems.add(miDouble);
MenuItem miTriple = new MenuItem;
miTriple.text = "Triple";
miTriple.click ~= &onPbcmHandler;
miTriple.index = 1;
_plusButton.contextMenu.menuItems.add(miTriple);
MenuItem miSeparator = new MenuItem;
miSeparator.text = "-";
miSeparator.index = 2;
_plusButton.contextMenu.menuItems.add(miSeparator);
MenuItem miHalf = new MenuItem;
miHalf.text = "Half";
miHalf.click ~= &onPbcmHandler;
miHalf.index = 3;
_plusButton.contextMenu.menuItems.add(miHalf);
}
void onPbcmHandler(Object sender, EventArgs ea)
{
GetNumbers();
MenuItem mi = cast(MenuItem)sender;
char[] label = mi.text;
debug char* _dbgLabel = label;
switch (label)
{
case "Double":
_num1 += _num1;
break;
case "Triple":
_num1 += (_num1 + _num1);
break;
case "Half":
_num1 /= 2;
break;
}
_firstNumberText.text = std.string.toString(_num1);
}
|
|
|
Back to top |
|
|
Chris Miller
Joined: 27 Mar 2004 Posts: 514 Location: The Internet
|
Posted: Mon Dec 27, 2004 3:32 pm Post subject: Re: HowTo: RMB context menu from Button? |
|
|
Lynn wrote: | I'm able to extend the default ContextMenu for a TextBox, but there doesn't seem to be a context menu actually in existence for a Button. Do I have to create it, load it, and get it to show up? | TextBox has a default context menu, Button doesn't, so it's null. Assign a new ContextMenu to Button.contextMenu as you figured out.
What you have looks good.. |
|
Back to top |
|
|
Lynn
Joined: 27 Aug 2004 Posts: 89
|
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|