get the splitcontainer context in user control click event - c#

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.

Related

How do I programatically show the DropDown of ContextMenuStrip?

I need a button to show the ContextMenuStrip associated with it on mouse left click instead of right click. How can I achieve this?
Try the code below
Point location = button1.PointToScreen(Point.Empty);
contextMenuStrip1.Show(location);
PointToScreen gets the actual location of your button and shows the context menu strip where the button located.
If cmenEdit is your ContextMenu and btnEdit is your button, in your BtnEdit_Click event you will have this:
cmenEdit.Show(btnEdit, new Point(0, btnEdit.Height));
ContextMenu will show under the button, and will look like this (as an example)

How to access the txt info inside a ToolTip from a programmatically created button?

I have problems to access the string inside a programmatically created button. Have a program which reads from a database and shows in a visual form, with buttons, the info of a specific process. Afterwards I want to make available this info with a "click" and show it in a window of the Form.
I create the button (boton1) programmatically, reading registers in a database (the number of buttons created depends of the registers and their values), and in the same step I create a new nutton I create their Tooltip (Tooltip1). So at the end I have a grid of buttons where I can see their tooltips if I haver the mouse over them. What I need is to be able to press these buttons and show in a textbox the info of these specific button's tooltip. The problem is because the boton1 creation is inside a method which creates the grid it's not accesible (I think this is the reason) from a method_click so the button (boton1) or Tooltip1 doesn't exists inside the button_click event, and if I create them it doesn't show when I click the Tooltip1 info but an empty string...
This is my code with the empty string, it doesn't show the info I see in the tooltips but "..." when I "click" on them.
protected void boton1_Click(object sender, EventArgs e)
{
ToolTip Tooltip1 = new ToolTip();
//This Tooltip is already created in other part of the code but without the code "running" don't exist for the button event creation
Button boton1 = sender as Button;
//Same here, already working and the tooltips seems fine but don't exist in the button_click event
//identify which button was clicked and perform necessary actions
String txtTool = tool.GetToolTip(button);
String info = Tooltip1.GetToolTip(boton1);
}

Devexpress unload usercontrol after save form

i have a main form which is containing a menu.
when i click a menu button there is an usercontrol is loading panel control in main form:
splitContainerControl1.Panel2.Controls.Add(new Moduller.userControlStokListesi() {
Dock = DockStyle.Fill
});
And there is a form on UserControl. when a user fill that form and hit the save button i want to remove that user control form from panel control.
How can i do that?
So if I understand your question correct, you want to remove the instance of Moduller.userControlStokListesi from the Panel2 ?
There are several ways to achieve this. You can remove all controls from a panel this way:
splitContainerControl1.Panel2.Controls.Clear();
You can also remove specific items:
splitContainerControl1.Panel2.Controls.RemoveByKey("the key of your control");
Or if you want the user control removes itself from the panel, you can call this snippet within the user control instance:
SplitContainerControl splitPanel = (SplitContainerControl) this.Parent;
splitPanel.Panel2.Controls.Remove(this);

Duplication Controls Winforms

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.

User Control Click - Windows Forms

I have a custom user control on my windows forms. This control has a few labels on it.
I will be dynamically displaying an array of these controls on my form which will contain different bits of data.
What I am trying to do is know which user control was selected when I click on it.
This works when I click on an empty space on the user control, however, if I click on any label on the user control it will not recognize the user control click.
Any thoughts on how I can do a full user control click, even if a label on the control is being clicked?
If this question is not clear, or you need more info, please leave a comment.
I am doing this in c#.
Thanks!
User control's click event won't fire when another control is clicked on the user control. You need to manually bind each element's click event. You can do this with a simple loop on the user control's codebehind:
foreach (Control control in Controls)
{
// I am assuming MyUserControl_Click handles the click event of the user control.
control.Click += MyUserControl_Click;
}
After this piece of code workd, MyUserControl_Click will fire when any control on the user control is clicked.
foreach (Control c in this.Controls)
{
c.Click += new EventHandler(SameAsForm_Click);
}
Keep in mind that this won't add labels' clickevents in groupboxes, panels etc to the "SameAsForm_Click"-EventHandler.

Categories