Usercontrol button not fireing event - c#

I'm trying to make a button that shows a message box when you click it. The button is made as a user control and it's put inside a panel container. What i want to do is when you click the button, the controls dissapear within the panel.
Code in usercontrol
public partial class UserControl1 : UserControl
{
public event EventHandler ButtonClick;
public UserControl1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//bubble the event up to the parent
if (this.ButtonClick != null)
this.ButtonClick(this, e);
MessageBox.Show("Test"); //test if the button itself is working
}
}
Code in Form
public partial class Form1 : Form
{
UserControl1 usercontrol1 = new UserControl1();
public Form1()
{
usercontrol1.ButtonClick += new EventHandler(btnIsClicked);
InitializeComponent();
}
private void btnIsClicked(object sender, EventArgs e)
{
panel1.Controls.Clear();
}
}
Having used the code above it's not working, the button works but nothing happens in the form.

Related

How to pass values between UserControls?

So i've a Form1 with three UserControl's as UC1,UC2 & UC3. Form1 contains a Button, UC1 and UC3. Both UC1 & UC3 are initially hidden when the application runs and the Form1 button click event makes the UC1 visible. UC1 contains a panel and inside the UC1 load event I'm adding UC2 into the panel through panel.Controls.Add() method at application run time. UC2 contains a Button and the click event of this button makes UC3 visible in Form1. UC3 contain a textbox. Now when I click the UC2 Button, I want it to pass a string value to UC3 textbox. How do I do that? or generally I need a simple solution to send values from one UserControl to another UserControl.
Here is all my code...
Form1.cs
namespace Problem
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
uC11.u3Visible += UC11_u3Visible;// Event came from UC1
}
private void UC11_u3Visible(object sender, EventArgs e)
{
uC31.Visible = true;
uC11.Visible = false;
}
private void button1_Click(object sender, EventArgs e)
{
uC11.Visible = true;
}
}
}
UC1.cs
namespace Problem
{
public partial class UC1 : UserControl
{
public event EventHandler u3Visible;//Declaring a public event
public UC1()
{
InitializeComponent();
}
private void UC1_Load(object sender, EventArgs e)
{
UC2 xy = new UC2();
xy.Name = "UsrCntrl2";
xy.Location = new Point(18, panel1.Controls.Count * 73);
panel1.Controls.Add(xy);// Adding UC2 in UC1
xy.open += Xy_open;// UC2 button click event
}
private void Xy_open(object sender, EventArgs e)
{
EventHandler handler = u3Visible;
if (handler != null)
{
handler(this, new EventArgs());// Creating another event to make UC3 visible
//upon button click from UC2.
}
}
}
}
UC2.cs
namespace Problem
{
public partial class UC2 : UserControl
{
public event EventHandler open;//Declaring a public event
public UC2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
EventHandler handler = open;
if (handler != null)
{
handler(this, new EventArgs());// Button click sending this event
}
string text = "Hello There!";// "I want this string to pass into UC3 textbox"
var ass = new UC3(text);
}
}
}
UC3.cs
namespace Problem
{
public partial class UC3 : UserControl
{
public UC3()
{
InitializeComponent();
}
public UC3(string text)
{
InitializeComponent();
TB.Text = text;// why textbox not showing this value?
MessageBox.Show(text);// while this MessageBox showing it!
}
}
}
Any help will be greatly appreciated. Thanks in advance!

Change window size depending on user control c#

So I have a MainForm, in which I got a Panel.
To this I have created several UserControls that I will put in my Panel on request from different buttons.
My question is: I need to change the windowsize on MainForm depending on which UC I have in the Panel. How do I do this?
I was thinking of creating a public method in MainForm and then call it in the different UC on load, what do you think? Give me your best solutions. Thanks.
Edit: If this is to any help, this is in my MainUC-code to bring in other UC to replace MainUC in panel
private void UC1Button_Click(object sender, EventArgs e)
{
Panel MainPanel = MainForm.MainPanel;
if (!MainPanel.Controls.Contains(UC1.Instance))
{
MainPanel.Controls.Add(UC1.Instance);
UC1.Instance.Dock = DockStyle.Fill;
UC1.Instance.BringToFront();
}
else
{
UC1.Instance.BringToFront();
}
In each UserControl you can create an event which is triggered when content should change. For example you can create an event when a button is clicked
public partial class MyControl : UserControl
{
public event OnButtonClicked ButtonClicked;
public MyControl()
{
InitializeComponent();
}
private void MyButton_Click(object sender, EventArgs e)
{
if(ButtonClicked != null)
{
ButtonClicked((Button)sender);
}
}
}
public delegate void OnButtonClicked(Button button);
Then in your MainForm you can subscribe to events and change panel content and window size when appropriate.
public partial class MainForm : Form
{
MyControl myControl;
void Subscribe()
{
myControl.ButtonClicked += myControl_ButtonClicked;
}
void myControl_ButtonClicked(Button button)
{
// Change panel content
// Resize window
}
}

button click inside Usercontrol for load another usercontrol in main form

I have main form and 2 usercontrols.The main form contains split container, In splitcontainer.panel1 i loaded UserControl1. In usercontrol different buttons are placed. I wants to load usercontrol2 on panel2(in main form) on button clicks which are placed inside the usercontrol1.
public partial class Form1 : Form
{
UserControl1 obj = new UserControl1();
public Form1()
{
InitializeComponent();
splitContainer1.Panel1.Controls.Add(obj);
}
}
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
public event EventHandler ButtonClick;
public void button1_Click(object sender, EventArgs e)
{
}
}
On button1_Click i want to load UserControl2 on form splitcontainer panel2
You could change your UserControl1 like this:
public void button1_Click(object sender, EventArgs e)
{
if(ButtonClick != null)
ButtonClick(this, e);
}
And then in your Form1 constructor add the following code:
obj.ButtonClick += (Sender, e) =>
{
splitContainer1.Panel2.Controls.Add(obj2);
};
This should work

How to make a button in a custom control to fire onClick event and have it handled in the main form where the custom control resides?

In C#, I created a custom control which inherits from UserControl, and added a button btnInTrayMap to the custom control.
Then I added the custom control to the main form. The main form has another button for comparison of their behaviours.
What I observed is the button on the main form works fine when clicked. However, the button btnInTrayMap which resides in the custom control does not respond at all when it is clicked.
I have the following code for the button in the custom control:
public partial class TrayMap : UserControl
{
public TrayMap()
{
InitializeComponent();
}
public event EventHandler MyCustomClickEvent;
protected virtual void OnMyCustomClickEvent(object sender, EventArgs e)
{
if (MyCustomClickEvent != null)
MyCustomClickEvent(this, e);
}
private void btnInTrayMap_Click(object sender, EventArgs e)
{
OnMyCustomClickEvent(sender, EventArgs.Empty);
}
}
I believe btnTrayMap.Click's event handler in TrayMap.designer.cs may have caused the problem:
this.btnInTrayMap.Click += new System.EventHandler(this.btnInTrayMap_Click);
In the main form, I have the code below:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnInForm_Click(object sender, EventArgs e)
{
MessageBox.Show("Test Button In Form", "btnInForm Button Clicked", MessageBoxButtons.OK);
}
public void MyCustomClickEvent(object sender, EventArgs e)
{
Button button = sender as Button;
MessageBox.Show("Test Button In TrayMap", button.Text + " Button Clicked", MessageBoxButtons.OK);
}
}
I would like to know how to set event delegation such that the MyCustomClickEvent method in the main form will be executed when the button btnInTrayMap is clicked.
Thank you.
You have not register your event in your main form. Try this way.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
trayMap.MyCustomClickEvent += MyCustomClickEvent; // i'm assuming trayMap is the name of user control in main form.
}
private void btnInForm_Click(object sender, EventArgs e)
{
MessageBox.Show("Test Button In Form", "btnInForm Button Clicked", MessageBoxButtons.OK);
}
private void MyCustomClickEvent(object sender, EventArgs e)
{
Button button = sender as Button;
MessageBox.Show("Test Button In TrayMap", button.Text + " Button Clicked", MessageBoxButtons.OK);
}
}

Eventhandler does not fire when user control's button is clicked

I have a button from a user control and want it to notify my form when it is clicked. Here is how I do it. It does not work. Can someone tell me what is wrong with it?
In user Control
public event EventHandler clicked;
public string items;
InitializedData data = new InitializedData();
ArrayList list = new ArrayList();
public DataInput()
{
InitializeComponent();
clicked+= new EventHandler(Add_Click);
}
public void Add_Click(object sender, EventArgs e)
{
items = textBox1.Text.PadRight(15) + textBox2.Text.PadRight(15) + textBox3.Text.PadRight(15);
if (clicked != null)
{
clicked(this, e);
}
}
In Form1
UserControl dataInput= new UserControl();
public void OnChanged(){
dataInput.clicked += Notify;
MessageBox.Show("testing");
}
public void Notify(Object sender, EventArgs e)
{
MessageBox.Show("FIRE");
}
Thanks
The UserControls Button Click event should be assigned to Add_Click, I don't think you want to assign the UserControl clicked event to Add_Click
Try removing clicked += new EventHandler(Add_Click); from your UserControl and set the UserControls Button Click event to Add_Click so it will trigger clicked on you Form
Example:
UserControl:
public partial class UserControl1 : UserControl
{
public event EventHandler clicked;
public UserControl1()
{
InitializeComponent();
// your button
this.button1.Click += new System.EventHandler(this.Add_Click);
}
public void Add_Click(object sender, EventArgs e)
{
if (clicked != null)
{
// This will fire the click event to anyone listening
clicked(this, e);
}
}
}
Form:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// your usercontrol
userControl11.clicked += userControl11_clicked;
}
void userControl11_clicked(object sender, EventArgs e)
{
}
}

Categories