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)
{
}
}
Related
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!
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.
I have a Form with Button ButtonGo.
and I have a class which takes a button through its constructor, then handles its events:
public class HandlingClass
{//.......
Button go ;
public HandlingClass(Button btn)
{
this.go = btn;
this.go.Click += new EventHandler(this.go_Click);
}
//.....
public void go_Click(object sender, EventArgs e)
{
//logic here
}
What am I doing wrong, and why isn't the event being raised when I press the Button in the caller form?
This code works for me
public class HandlingClass
{
Button go;
public HandlingClass(Button btn)
{
go = btn;
go.Click += go_Click;
}
void go_Click(object sender, RoutedEventArgs e)
{
throw new NotImplementedException();
}
}
and in your loaded event of the class having the button you just add the below code
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
HandlingClass hc=new HandlingClass(**MyButton**);
}
MyButton should be the reference to your button.
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);
}
}
I have 2 forms.
Form1:
public partial class Panel1
{
public void ShowExport(object sender, EventArgs e)
{
.......
}
}
Form2:
public partial class Panel2
{
public delegate void ShowExportReport(object sender, EventArgs e);
public event ShowExportReport ShowExportClicked;
private void buttonExport_Click(object sender, RoutedEventArgs e)
{
if (ShowExportClicked != null)
{
ShowExportClicked(sender, new EventArgs());
}
}
}
When I click button -
button.Click = buttonExport_Click
How can I call Panel1.ShowExport() from Panel2.buttonExport_Click?
In the Panel1 you have to subscribe the event:
pnl2.ShowExportClicked += new ShowExportReport(ShowExport);
You need to assign the handler for the event ShowExportClicked in Panel 1 class to the Panel 2 class object.
public partial class Panel1
{
Panel2 pnl2;
public Panel1()
{
pnl2 = new Panel2();
pnl2.ShowExportClicked += new ShowExportReport(ShowExport);
}
public void ShowExport(object sender, EventArgs e)
{
.......
}
}
pnl2.ShowExportClicked += ShowExport;
Create your event on Form1. and listen to the event in Form2.
Form1:
public event EventHandler ShowExportChanged;
private void ShowExportChanged()
{
var handler = ShowExportChanged;
if(handler == null)
return;
handler(this, EventArgs.Empty);
}
public void ShowExport(object sender, EventArgs e)
{
ShowExportChanged();
}
Form2:
pnl1.ShowExportChanged+= new OnShowExportChanged(ShowExportChanged);
How can I call Panel1.ShowExport() from Panel2.buttonExport_Click?
By passing (only the necessary) information from form1 when instantiating form2.
Form1.cs:
void ShowForm2_Click()
{
var form2 = new Form2();
form2.ShowExportClicked += ShowExport;
form2.Show();
}
Now from Form2 you can simply call ShowExport on button click.