Disable Parent Control of a button in windows Forms - c#

so what i want to do is, on Event ButtonClick disable parent Control of this button.
In my Form i have several Panel's in which those button actually are.
I am using following code:
private void button1_Click(object sender, EventArgs e)
{
Control control = button1.Parent;
control.Enabled = false;
}
This xode is working fine, but I wanted to use this.Parent instead of button1.Parent, so that each button would be able to disable its own parent Panel(in this case will disabled Panel of button1).
When i am using this.Parent I get a System.NullReferenceException.
Knows some one why i am getting this error ?

this is your current class you want to use the sender and cast it to Control than get it's parent something like this
private void button1_Click(object sender, EventArgs e)
{
var uc = sender as Control;
uc.Parent.Enabled = false;
}

this represents the Window, which has no Parent.
You can do the following:
private void button_Click(object sender, EventArgs e){
Control control = ((Button) sender).Parent;
control.Enabled = false;
}
Button[] buttons = {button, button2....};
foreach (Button button in buttons)
button.Click += button_Click;
Alternatively, you can create a class inheriting from Button. You can then add a Click event handler that disables it's parent control. You can then use this button instead of the default one.

Related

Detect SplitContainer's select Panel

I have Windows Form named Form1 and inside I have a dynamic SplitContainer named splitcontainer.
I want to know which panel is selected when the mouse is clicked at runtime.
I tried to use mouseclick event in the splitContainer properties but I haven't succeed.
You need to bind to the MouseClick events of the panel inside the split container.
I added a container called "splitContainer1" with 2 panels, Panel1 and 2
I wired up the below events and it seems to work
private void splitContainer1_Panel1_MouseClick(object sender, MouseEventArgs e)
{
MessageBox.Show("Panel1");
}
private void splitContainer1_Panel2_MouseClick(object sender, MouseEventArgs e)
{
MessageBox.Show("Panel2");
}
After your further comments, I've edited the below to show how to manually bind the mouse click events of the 2 panels of your dynamically added container.
private void splitContainerHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
{
SplitContainer spltcnt = new SplitContainer();
spltcnt.Dock = DockStyle.Left;
spltcnt.Orientation = Orientation.Horizontal;
spltcnt.SplitterWidth = 4;
spltcnt.Visible = true;
spltcnt.Size = new System.Drawing.Size(731, 615);
spltcnt.BorderStyle = BorderStyle.Fixed3D;
spltcnt.SplitterDistance = 351;
//Manually bind the mouse click events.
spltcnt.Panel1.MouseClick += Panel1OnMouseClick;
spltcnt.Panel2.MouseClick += Panel2OnMouseClick;
Controls.Add(spltcnt);
}
private void Panel1OnMouseClick(object sender, MouseEventArgs mouseEventArgs)
{
MessageBox.Show("Panel1");
}
private void Panel2OnMouseClick(object sender, MouseEventArgs mouseEventArgs)
{
MessageBox.Show("Panel2");
}
You can of course call the mouse click handler methods anything you like.
Thanks

WinForms TabControl

I noticed that by programmatically selecting a Tab in the Tab control selects a control contained in the tab page selected.Is it possible to change this behaviour. I have a control in a tabpage that I do not want to be selected when the its tab page is selected from a button click. I have a simple form with a tab control and two tab pages. When button1 is clicked the tab page 2 is selected but so is the datagridview contained in that tab page.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.GotFocus += DataGridView1_GotFocus;
}
private void DataGridView1_GotFocus(object sender, EventArgs e)
{
//this event is called from button1_click
}
private void button1_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage2;
}
}
By default when you select a tab (or even when you start a form) the control which is the first in your tab order is automatically focused. I am assuming this is what is happening here.
You can solve this by simply unfocusing the datagridView in question. There are multiple ways to do this. Firstly you can set focus to the control that you wish to be selected instead of the dataGridView. This can be done by:
myControl.Focus = True;
Or alternatively if you want non of the controls to be selected you can set the active control to Null:
ActiveControl = NULL;
Note: ActiveControl is a property which contains the current active control.
As to where this code should be placed. That is totally dependent upon you. You can do it as soon as you change the tab in the button click event. This is what I would prefer.
I am sure there are other kludges as well to acheive the same. Hope this helps.
Here is code to select tab
private void button1_Click(object sender, EventArgs e)
{
// we can select tab by tab name
tabControl1.SelectTab("tabPage2");
tabControl1.SelectedIndex = 1;
tabControl1.TabPages[0].Hide();
tabControl1.TabPages[1].Show();
}

How to access 2 User Controls at a time?(1 from another)

I have made 2 user controls. First one contains a textbox and a button. In the second one there is a panel and a repeater control is used. Now when I am writing in the textbox 2nd control will open as a popup and and after focusing to textbox I am unable to write. I have searched a lot about this but nothing works.
CustomPopup customPopup;
Popup popup;
popup = new Popup(customPopup = new CustomPopup());
private void txtSearch_TextChanged(object sender, EventArgs e)
{
popup.Width = Width;
popup.Show(this);
popup.AutoClose = false;
}
In this way I am opening the popup from the textbox text changed event.
You can use this textbox event.
private void textBox1_Enter(object sender, EventArgs e)
{
}
this event call whenever textbox get focus

How to find in what Panel my button is placed in?

I have some questions that are situated in distinct panels. I place button "Add" in every panel. This button is supposed to add additional textbox to panel. But I don't know what to write in button_click. What panel to choose?
private void button_Click(object sender, EventArgs e)
{
}
Use Control.Parent property.
private void button_Click(object sender, EventArgs e)
{
Button button = sender as Button;
if (button == null)
return; //Some error/exception
Panel parentPanel = button.Parent as Panel;
if (parentPanel == null)
{
//Parent container is not panel
}
//Otherwise get the panel properties.
}
I'll assume all the "Add" buttons are subscribed to this same event.
The value of sender will be the particular "Add" button that was just clicked. Then you can cast the button's Parent to a Panel:
var button = (Button)sender;
var parentPanel = (Panel)button.Parent;
These two lines will be sufficient as long as
You don't accidentally attach some other control other than a button to this event
All the "Add" buttons are contained within a Panel.

Attemping dynamic displaying a usercontrol,how to implement this?

I want utilize a custom control(ui like a button) dynamic show a tree under it when user click it.And hide tree when custom control lost focus.
how to get it ?
(In addition can't use Form control.)
Create a control(tree) which is hidden from the beginning.
yourControl.Visible = false;
Create your button and your click eventhandler
yourButton.Click += yourButton_Click;
private void yourButton_Click(object sender, EventArgs e)
{
yourControl.Visible = true;
}
To hide when focus is lost, you need to create another method/eventhandler:
yourButton.LostFocus += yourButton_LostFocus;
void yourButton_LostFocus(object sender, EventArgs e)
{
yourControl.Visible = false;
}

Categories