Duplication Controls Winforms - c#

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.

Related

Cannot keep OnClick event after visibilty change

Just wondering if someone else can recreate this or is it just me :)
Drop a panel on the form
Put a button on the panel
Create click of button "I am Button"
Above works fine
Add onmouseleave to panel set button visible to false
Add onmouseenter to panel set button visible to true
Now if you move mouse in and out of panel the button will become visible false. Pefect.
Now move mouse back intp panel and button becomes visible. Perfect.
Clicking on button now does nothing (Lost on Click Event?)
Breakpoint in onclick never fires.
Is this a concept that cannot be done?
'Update' It wont let me post long comment.
First of all thanks for the quick responses.
I did not include code because this was in a very large project so i recreated it with a brand new app.
C# WinForm with one panel and one button on the panel.
I did not try to click on an invible button. Not only can i see the button but can see it respond to bing clicked.
#steve. Good point, The button is well inside of the panel but i see where you are going. I just performed a new test.
If i move the mouse in and out of the panel the button will show and hide perfectly.
After this, hitting enter on the button will excute onclick but the mouse will not. (I suppose this has something to do with only one button and it is default.)
What prompts the mouse to not fire the click event is beyond me.
Very Strange.
What i am trying to achieve:
An area on the screen that when the mouse enters this area a group of buttons appear so they can be used.
when the mouse leaves this area the buttons will dissapear. As my original post stated button works fine if visiblity does not change.
I suppose the your code has the following problem:
You receive the mouseenter event on the panel and you make the button
visible
You move the mouse over the button
This last action triggers the mouseleave event on the panel.
As a consequence your button becomes invisible.
But making the button invisible triggers a mouseenter over the panel and in a blink the button is again visible and causing a mouseleave on the panel.
An infinite loop between these two events starts.
So the click event is not lost, simply it will never trigger because the form engine is busy hiding and showing the button control.
I have reproduced the situation and fixed it adding an event handler for the MouseMove event. In this event I set a variable to block the infinite loop between the two events...
This is my example to adapt to your real code, try it in LinqPad
public class Form1 : Form
{
Button b1 = new Button();
Panel p1 = new Panel();
bool stillOnPanel = false;
public Form1()
{
b1.Text = "ClickMe";
b1.Click += onClick;
b1.Visible = false;
p1.MouseEnter += onEnter;
p1.MouseLeave += onLeave;
p1.MouseMove += onMove;
p1.BorderStyle = BorderStyle.FixedSingle;
p1.Size = new Size(150,100);
this.Controls.Add(p1);
p1.Controls.Add(b1);
}
void onClick(object sender, EventArgs e)
{
Console.WriteLine("Clicked");
}
void onMove(object sender, MouseEventArgs e)
{
//Check if we are still over the panel area
stillOnPanel = b1.ClientRectangle.Contains(p1.PointToClient(Cursor.Position));
}
void onEnter(object sender, EventArgs e)
{
if (!b1.Visible)
{
b1.Visible = true;
}
}
void onLeave(object sender, EventArgs e)
{
// Do not hide the button if we are over it, let it click....
if (b1.Visible && !stillOnPanel)
{
b1.Visible = false;
}
}
}

Is there a way to check if any radio button has been clicked in a form?

I'm creating a form and I want the text of a button to change depending on if any radio button in the whole form has been clicked on, additionally some radio buttons are in group boxes so do not affect other radio buttons. The only way I can think of to do this is to input code into every method that is called for when each radio button is clicked. Is there any other way of doing it?
you can create one method for click event for one radioButton only and make all radioButtons share this click event and i will suppose that you will make Button text like radioButton text which you clicked and inside this method write this line...
private void radioButton1_Click(object sender,EventArgs e) {
button1.Text = (RadioButton(sender)).Text;
}

Insert existing Buttons into Context Toolstrip - Winforms

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.

Click on form does not click underlying control

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...

get the splitcontainer context in user control click event

Sir,
I have a split container in which in right panel i have a usercontrol.In the user control i have few buttons like view,new,edit etc.Bottom of that user control a form will open based on what link is clicked on the left side navigation pane. now when i click the user control's view button, i should open a new form below it. how to get the context of splitcontainer in the click event?also if i want to retrieve the form values to save in database when i click the save button in user control, how to do it?
A Click event has a sender parameter, which is the clicked button. You could use the name of the button to resolve which form should be opened.
To get your button in the click event:
Button clickedButton = (Button)sender;
To get the parent of your button (if it was SplitContainer, you'll have to use Parent property 3 times, because the first one will get you your UserControl, second - left panel of the SplitPanel, which doesn't have a Name property, third - your SplitPanel, and 4th, if you want, your form name)
string splitPanemName = clickedButton.Parent.Parent.Parent.Name;
...or you can just get whole SplitPanel object:
SplitPanel currentSplitPanel = (SplitPanel)clickedButton.Parent.Parent.Parent;
...or Form object:
Form currentSplitForm = (Form)clickedButton.Parent.Parent.Parent.Parent;
To do this, you have to be sure of the composition of your Form, so you can get the right controls at the right place.

Categories