I have a GUI and some buttons in WinForms. Now I want to create a Toolstrip Menu and move the buttons into the menu. Is it possible without copying the whole code into the new Toolstrip entries? I just want to link the buttons somehow.
Thanks
Yes and no. It is not possible to move the existing buttons into the ToolStrip but you can create new ones and rewire them:
first: look for the name of the current event handler by selecting the old button, press F4 and switch to the action menu (the lightning bolt in the properties window):
second: select the new button and pick the same event handler as for the old button:
finally you can remove the old button.
Buttons click events code can be linked directly to toolstrip buttons.
All availables events can be showed in Action->Click in design time.
EDIT:
I assume that you have a form in your project with some buttons, eg.: button1, button2.
For each Button you wrote a click action, in your code something like:
private void button1_Click(object sender, EventArgs e)
{
// YOUR CODE FOR BUTTON1
}
private void button1_Click(object sender, EventArgs e)
{
// YOUR CODE FOR BUTTON2
}
Then you can use those methods/action for toolbar's buttons.
The easiest way to do is to add buttons to tollbar and use Events (with property panel, the little bolt icon) to choose the correct method to assign to each tolbar's button Click event/action.
Related
What I know:
In visual Studio 2015 after double-clicking a button, VS will bring the cursor to generated code inside a "button1_click" event.
If a different button is double-clicked, another (completely separate) "button2_click" event is generated.
The same can be said for a TextBox being double-clicked; the user is brought to a generated event for "textbox1_textChanged" or a textbox2_textChanged."
What I want to know:
When clicking textBox1 in form view, accessing its properties, and clicking "events" and double clicking "Click" at the top, the "textbox1_Click" is generated. (This is good)
However, when clicking a SECOND textbox (textbox2), accessing its properties, Events, and double clicking its "Click" event, it brings the cursor to THE SAME EVENT that was generated for textbox1, aptly named "textbox1_Click." (This is not what I want.)
All I ultimately want is:
textbox1_Click {
textbox1.SelectAll();
}
textbox2_Click {
textbox2.SelectAll();
}
How do I create this SECOND click event for my second textbox (textbox2)?
Is it done with an if statement, possibly using sender or e?
Double-clicking the event name (or the object) automatically adds code to the Designer.cs file for your form. That's how the events are linked to their respective controls.
If your two controls are "double-clicking" to the same event handler [and if such behavior is unintentional], fastest fix is to remove the event wire-up from the second control in Designer.cs and double-click the control again.
Look for this line of code:
this.textBox2.Click += new System.EventHandler(this.textBox1_Click);
Which indicates that your second texbox is wired up to the same event as your first one.
void textbox1_Click(object sender, EventArgs e)
{
textbox2.SelectAll();
}
void textbox2_Click(object sender, EventArgs e)
{
textbox2.SelectAll();
}
And in designer
add:
this.textBox1.Click += new System.EventHandler(this.textBox1_Click);
under textbox1
and
this.textBox2.Click += new System.EventHandler(this.textBox2_Click);
under textbox2
Alternatively you can add event handlers in your code. It is useful when you create dynamic controls.
I have such a situation here.
Let's say we have three button on our Form, and one Control, which is Panel, and which is hidden when the Form is loaded. Here is it
When I click on Button 1, that Panel have to be shown under Button1, when clicking on Button2, it have to be showned under Button2 and so on. Let's say I clicked on Button2.
Now I want that same Panel to show, when Clicking on Button1 and not at the same place. I need the same panel to be showed under buttons when clicking them.For example, the same panel is showing when clicking button 3
I made this only working for only one button. I can't have 2 controls with same attributes, but I need somehow to duplicate that control.I And I think it have to be done with UserControl.
private void btn_click(Control sender, EventArgs e)
{
var btn = sender as Button;
panel1.Left = btn.Left;
}
now assign this even handler to click event for all buttons.
The var btn.... line will represent the button that was clicked, or the control that triggered the event, so from there you can set the panel's location.
I have a windows forms application. When I click on a window this activates the form and then I need to click again to call the particular control click event. For example if I click on a button this activates the form and then I need to click the button again.
Is there a way to perform the control click and window activation in one click? Preferably I would want this to work with whatever the clickable control is (menu,button, label etc)
So far I have managed to activate the win form on mouse over and then the control click works. I would like to have the win form activated on click and also run the click command on an underlying control if this has a click event.
Well, here's a way to accomplish what You want (just attach a similar method to Your from's MouseClick event):
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
var c = this.GetChildAtPoint(e.Location);
this.InvokeOnClick(c, e);
}
This method has it's drawbacks though - for example the control will be clicked even though it's disabled etc., so You have to make sure the control under the cursor is "clickable" by yourself...
I have a WinForm with 3 group boxes, one with combo boxes, and two with radio buttons. I set all of them and their children controls to "TabStop = false", but when I cycle with TAB, the currently selected radio button in each of the last two group boxes gets focused.
If there's no way to change this behavior, what would be a good event to catch and move the focus away? I can't find an "OnFocus" event.
The solution is to set one method (code below) to handle the "Enter" event of every radio button in the form (if that's what you wish).
Actually, I only did it for the radio buttons of the first group box and it worked, the second group box's radio buttons don't get focus, even though their "Enter" events are not handled. This is not the behavior you would have expected.
private void radiobuttonXGroup1_Enter(object sender, EventArgs e)
{
SomeOtherControl.Focus();
}
In the *.Designer.cs file you edit every Enter event (for each radio button) to point to one event handler (the above method).
this.radiobutton1Group1.Enter += new System.EventHandler(this.radiobuttonXGroup1_Enter);
this.radiobutton2Group1.Enter += new System.EventHandler(this.radiobuttonXGroup1_Enter);
this.radiobutton3Group1.Enter += new System.EventHandler(this.radiobuttonXGroup1_Enter);
Setting the TabStop to False on a RadioButton to prevent tabbing to the control works until you actully select the radio button without any additional overrides like suggested by #msergeant.
EDIT
The following code prevents the code from getting a tab key event:
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
radioButton1.TabStop = false;
}
Radio buttons behave differently with respect to Tab from other controls in that they work in sets or groups based on setting the tab index or placing then radio buttons in a group box.
The MSDN documentation for RadioButton.TabStop states "This API supports the .NET Framework infrastructure and is not intended to be used directly from your code". Which basically means, "This isn't going to work how you expect it to".
With that said, the Enter event will fire when the button receives the focus. You can try to use that to move focus to another control.
I have a ListBox, and its populated with items, id like to know how to:
when you rightclick in the listbox, that the rightclicked item will be selected,
a rightclick menu will be displayed with several items..
when you click on any of the items, a corresponding void will be triggered..
thanks in advance for any help, and code examples please!
This feels to me like a "homework" question, so I am going to answer by (I hope) giving you just a few pointers to solving this for yourself.
Phase One
create a sample project with a ListBox
define event handlers for MouseClick, MouseDown, and Click events.
put a Console.WriteLine("some appropriate text"); statement in each of those handlers so you can look in the Output Window in Visual Studio and see which event handler was called.
...
Phase Two
run the test program and observe the difference between what events are reported for a left mouse down and a right-mouse down (assuming your environment has the context click set to the right mouse down ... which may not be true for everyone).
focus on the one event you can intercept with a context-click.
add a context menu to the test project and set that context menu to be the context menu of the ListBox.
verify that you can now right click on an item in the ListBox and that the context menu will appear, BUT THE EVENT IS STILL HANDLED BY THE HANDLER YOU "DISCOVERED" IN STEP 2.
now go through all the Event handlers for ListBox, and figure out which one could be used to detect, given a certain location in the ListBox, which List Item was "hit."
once you can determine which List Item was right-clicked on, and you know your context menu is working, you have only the problem of making sure that the right-clicked List Item is selected : and that's pretty easy.
Figuring this out yourself will teach you several very useful things you'll be able to use later in programming to other controls.
best of luck, Bill
First, you need to subscribe to ListBox.MouseClick event. You will be able do determine what button was pressed and cursor coordinates. Then, use ListBox.IndexFromPoint method to find clicked item. You can select it using ListBox.SelectedIndex property. To display context menu use ContextMenu or ContextMenuStrip classes. Additional documentation on context menu is available in MSDN
1.when you right click in the listbox, that the right clicked item will be selected
2.a right click menu will be displayed with several items..
private void listBoxNode_MouseUp(object sender, MouseEventArgs e)
{
int location = listBoxNode.IndexFromPoint(e.Location);
if (e.Button == MouseButtons.Right)
{
listBoxNode.SelectedIndex = location; //Index selected
contextMenuStrip1.Show(PointToScreen(e.Location)); //Show Menu
}
}
3.when you click on any of the items, a corresponding void will be triggered..
private void showDetailsToolStripMenuItem_Click(object sender, EventArgs e)
{
//put your code here after clicking
//on items in context menu
}