Custom event in a winform usercontrol - c#

I am adding a usercontrol dynamically to a winform. The user control has a custom event.
form_load()
{
ucUpdateProgress ucUP = new ucUpdateProgress();
ucUP.customEvent += new EventHandler<CustomEventArgs>(ucUP_customEvent);
this.Controls.Add(new ucUpdateProgress());
}
I am calling this event when the user click the cancel button. But the customevent's value is null. Why is that? Whats wrong with my code?
private void button1_Click(object sender, EventArgs e)
{
CustomEventArgs cEA = new CustomEventArgs(true);
customEvent(sender, cEA);
}
Thanks,
Syd

That should be
this.Controls.Add(ucUP)
not
this.Controls.Add(new ucUpdateProgress());

Related

c# how to catch mouse event outside textbox

i need to hide a ListBox when I focus out a textbox. if i click on a different control or use Tab key then the textbox's "Leave" event occurs. But if I click inside the form, on any free space, then focusout doesn't happen. i saw something called mouse capture but i cant implement it.
i tried this:
private void txtProduct_Enter(object sender, EventArgs e)
{
listProduct.Show();
UIElement el = (UIElement)sender;
el.CaptureMouse();
}
private void MouseClickedElseWhere(object sender, MouseEventArgs e)
{
if (e.Clicks >= 1)
{
txtProduct_Leave(sender, new EventArgs());
}
}
private void txtProduct_Leave(object sender, EventArgs e)
{
listProduct.Hide();
}
but obviously it shows error. how do i achieve this? any help?
I had to make click event for my groupboxes even if groupbox doesnt have a click event by default.
//my_page.designer.cs
this.groupBox2.Click += new System.EventHandler(this.groupBox2_clicked);
//my_page.cs
private void groupBox2_clicked(object sender, EventArgs e)
{
listProduct.Hide();
}

asp.net event handler for dynamicallycreated button

I have a button created in code behind like so:
some method {
Button btnExportToExcel = new Button();
btnExportToExcel.Text = "Export To Excel";
btnExportToExcel.Click += new EventHandler(btnExportToExcel_Click);
pnlListView.Controls.Add(btnExportToExcel);
}
protected void btnExportToExcel_Click(object sender, EventArgs e)
{
//do something
}
The problem is that I can't ever get to execute the code from the event method.
Why is that?
are you adding controls to page in preinit event handler? Check
You must add the button to any controler .
protected void Page_Load(object sender, EventArgs e)
{
Button btnExportToExcel = new Button();
btnExportToExcel.Text = "Export To Excel";
btnExportToExcel.Click += new EventHandler(btnExportToExcel_Click);
//this is add the button to the form1
this.form1.Controls.Add(btnExportToExcel);
}
void btnExportToExcel_Click(object sender, EventArgs e)
{
//...
Response.Write("click me...");
}
Please add the dynamic controls in the Page's Init event handler so that the ViewState and Events are triggered appropriately.

Issue with LostFocus event of a TextBox

I'm trying to use this function:
private void IDCustTextBox_LostFocus(object sender, System.EventArgs e)
{
if (CustName.Text == "abc")
MessageBox.Show("Error");
}
When I type abc in CustName textbox, and then leave the textbox, I dont get any message. In the textbox properties I can see that "textbox.Changed" is using the event LostFocus.
How can I get this to show the Error message above?
There is no LostFocus event for textbox in property Window,if you want to use this then you must need to add event handler, there is textbox leave event in property window, that could be used as below:
private void textBox1_Leave(object sender, EventArgs e)
{
// do your stuff
}
for adding event handler you need to write the following:
textBox1.LostFocus += new EventHandler(textBox1_LostFocus);
then you can use it as below:
private void textBox1_LostFocus(object sender, EventArgs e)
{
// do your stuff
}
You will need to let the field know that there is a handler for the event LostFocus
Since this is not part of the properties window you will have attach the handler as such
CustTextBox.LostFocus += new EventHandler(IDCustTextBox_LostFocus);

How to implement listview using Usercontrol and a flowlayout panel?

I am trying to place Usercontrols on a flowlayout panel that act as a listview in C# (windows forms) . How can I implement selecting items here ?
Click event is not working when the user clicks on the controls of Usercontrol..
Any ideas ?
private void flowLayoutPanel1_ControlAdded(object sender, ControlEventArgs e)
{
e.Control.Click += new EventHandler(Clicked);
}
private void Clicked(object sender, EventArgs e)
{
//click event code goes here
}

Add events to controls that were added dynamically

I am working on a winforms app and I have added some controls dynamically (eg. Button). I want to add an event to that created button; how can I perform this? Also, can someone refer a C# book to me which covers all winforms topics?
// create some dynamic button
Button b = new Button();
// assign some event to it
b.Click += (sender, e) =>
{
MessageBox.Show("the button was clicked");
};
// add the button to the form
Controls.Add(b);
I totally agree with Darin's answer, and this is another syntax of adding dynamic event
private void Form1_Load(object sender, EventArgs e)
{
Button b = new Button();
b.Click += new EventHandler(ShowMessage);
Controls.Add(b);
}
private void ShowMessage(object sender,EventArgs e)
{
MessageBox.Show("Message");
}

Categories