I create the ButtonClick event in the UserControl below and want to handle it in a Form, but I get an error that says 'The name UserControl1.ButtonClick' does not exist in the current context.
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
public event EventHandler ButtonClick;
private void btnUpdate_Click(object sender, EventArgs e)
{
if (this.ButtonClick != null)
this.ButtonClick(this, e);
}
Form:
UserControl1.ButtonClick += new EventHandler(UserControl_ButtonClick);
protected void UserControl_ButtonClick(object sender, EventArgs e)
{
//handle the event
}
Form:
public PatientNumber()
{
InitializeComponent();
userControl11.updateButton.Click += button_Click;
}
protected void button_Click(object sender, EventArgs e)
{
//handle the event
}
UserControl:
public Button btnUpdatee
{
get
{
return btnUpdate;
}
set
{
btnUpdate = value;
}
}
This code worked.
Thank you
Related
I have two user controls usercontrol_1 and usercontrol_2, using a click event i am bring in usercontrol_1 to a panel called panel_screen on my main form.
private void btn_Click(object sender, EventArgs e)
{
if (!panel_screen.Controls.Contains(usercontrol_1.Instance))
{
panel_screen.Controls.Add(usercontrol_1.Instance);
usercontrol_1.Instance.Dock = DockStyle.Fill;
usercontrol_1.Instance.BringToFront();
}
else
usercontrol_1.Instance.BringToFront();
}
Similarly i wanna bring usercontrol_2 to the same panel on the main form using a button (click event) on usercontrol_1.
How do i do this? any help would be appreciated.
I'm not sure where you are having problem, I think the way you are using usercontrol_1.Instance might cause the issue.
I have working example here, you can try it.
private void btn_Click(object sender, EventArgs e)
{
bool userControlIsAlreadyInPanel = false;
//assuming you are cheking if usercontrol_1 is already there on panel
// you don't want to create new usercontrol, but just bring existing control to the front
foreach(UserControl control in panel_screen.Controls)
{
if (control.GetType() == typeof(usercontrol_1))
{
userControlIsAlreadyInPanel = true;
control.BringToFront();
}
}
if(!userControlIsAlreadyInPanel)
{
usercontrol_1 instane = new usercontrol_1();
panel_screen.Controls.Add(instane);
instane.Dock = DockStyle.Fill;
instane.BringToFront();
}
}
output
namespace WindowsFormsApp1
{
public delegate void MyEventDelegate(object sender, string name);
public partial class Form1 : Form
{
usercontrol_1 _ctrl1 = null;
usercontrol_2 _ctrl2 = null;
public Form1()
{
InitializeComponent();
_ctrl1 = new usercontrol_1();
_ctrl1.Dock = DockStyle.Fill;
_ctrl1.userControlButtonClicked += userControlButtonClicked;
_ctrl2 = new usercontrol_2();
_ctrl2.Dock = DockStyle.Fill;
_ctrl2.userControlButtonClicked += userControlButtonClicked;
this.Load += Form1_Load;
}
private void Form1_Load(object sender, EventArgs e)
{
userControlButtonClicked(_ctrl1, "1");
}
private void userControlButtonClicked(object sender, string name)
{
panel1.Controls.Clear();
if (sender.Equals(_ctrl1))
{
panel1.Controls.Add(_ctrl2);
}
else if (sender.Equals(_ctrl2))
{
panel1.Controls.Add(_ctrl1);
}
}
}
}
namespace WindowsFormsApp1
{
public partial class usercontrol_1 : UserControl
{
public event MyEventDelegate userControlButtonClicked;
public usercontrol_1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MyEventDelegate med = userControlButtonClicked;
if (med != null)
{
med(this, "1");
}
}
}
}
namespace WindowsFormsApp1
{
public partial class usercontrol_2 : UserControl
{
public event MyEventDelegate userControlButtonClicked;
public usercontrol_2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MyEventDelegate med = userControlButtonClicked;
if (med != null)
{
med(this, "2");
}
}
}
}
I have a class that inherits from UserControl:
public partial class MyView : System.Windows.Forms.UserControl
I want to handle the event that occurs when the user clicks on the X in the upper right corner. Maybe it is Form.Closing? But I don't see that as an option in the designer. What event is it?
Edit:
class SomeControl : UserControl
{
Form _owner;
public SomeControl()
{
}
protected override void OnVisibleChanged(EventArgs e)
{
base.OnVisibleChanged(e);
if (Visible)
{
_owner = FindForm();
//_owner = ParentForm;
_owner.FormClosing += _owner_FormClosing;
_owner.FormClosed += _owner_FormClosed;
}
}
private void _owner_FormClosed(object sender, FormClosedEventArgs e)
{
throw new NotImplementedException();
}
private void _owner_FormClosing(object sender, FormClosingEventArgs e)
{
Hide();
_owner.FormClosing -= _owner_FormClosing;
_owner.FormClosed -= _owner_FormClosed;
Parent.Controls.Remove(this);
_owner = null;
}
}
I created a usercontrol that contains many buttons and in the main form I have a textbox.
I add the usercontrol to the main form and I want to click any button on the usercontrol and have the textbox in the main form shows the button text.
The question is how to pass the string of the button in usercontrol to the textbox in the main form? This is what I'm trying to do
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
public string a ;
private void button1_Click(object sender, EventArgs e)
{
a = button1.Text;
}
private void button2_Click(object sender, EventArgs e)
{
a = button2.Text;
}
private void button3_Click(object sender, EventArgs e)
{
a = button3.Text;
}
and the main form code is :
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox1.Text = usrCtrl.a;
// usrCtrl come from : Usercontrol1 usrCtrl = new Usercontrol1();
}
and it shows nothing in the textbox.
refer to this answer, you need to create a property changed event.
UserControl.cs class;
public partial class UserControl1 : UserControl
{
public event PropertyChangedEventHandler PropertyChanged;
public UserControl1()
{
InitializeComponent();
}
private string stringA;
public string a
{
get { return stringA; }
set
{
if (value != stringA)
{
stringA = value;
if (PropertyChanged!= null)
{
PropertyChanged(this, new PropertyChangedEventArgs(a));
}
}
}
}
private void button1_Click(object sender, EventArgs e)
{
a = button1.Text;
}
private void button2_Click(object sender, EventArgs e)
{
a = button2.Text;
}
private void button3_Click(object sender, EventArgs e)
{
a = button3.Text;
}
private void button4_Click(object sender, EventArgs e)
{
a = button4.Text;
}
}
On Form's Load we need to define the event,
private void Form1_Load(object sender, EventArgs e)
{
cntr.PropertyChanged += Cntr_PropertyChanged; // press tab + tab after += and it will generate the following method automatically.
}
Here is Event;
private void Cntr_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
textBox1.Text = cntr.a.ToString(); //cntr is the instance of UserControl1
}
Hope helps,
Your code to change the textBox1.Text value is in the wrong event handler.
The textBox1_TextChanged event handler only fires when text in that field changes.
What you need to do is put the line:
textBox1.Text = a;
in the click event handlers.
Here is my code in my userControl
public partial class UserControlHomeScreen : UserControl
{
public event EventHandler SomethingHappened;
public void DoSomething()
{
EventHandler handler = SomethingHappened;
if (handler != null)
{
handler(this, EventArgs.Empty);
}
}
public void HandleEvent(object sender, EventArgs args)
{
MessageBox.Show("Wafak.");
}
public UserControlHomeScreen()
{
InitializeComponent();
}
private void btnAverageDailyBal_Click(object sender, EventArgs e)
{
this.Tag = 0;
this.Hide();
}
private void btnComputeTransferPricing_Click(object sender, EventArgs e)
{
this.Tag = 1;
this.Hide();
}
}
And here is my code in my main form
private void HomeScreen()
{
uHomeScreen = new UserControlHomeScreen();
uHomeScreen.Dock = DockStyle.Fill;
//uHomeScreen.Disposed += new EventHandler(uHomeScreen_Disposed);
uHomeScreen.SomethingHappened += new EventHandler(uHomeScreen_SomethingHappened);
panelMain.Controls.Add(uHomeScreen);
}
void uHomeScreen_SomethingHappened(object sender, EventArgs e)
{
MessageBox.Show("throw new NotImplementedException();");
}
What i want to happen is that when the usercontrol is hidden i want to fire an event in my main form but does not work, what am i missing? please help. thanks!
Your naming convention for event raiser (DoSomething) is confusing, your code doesn't call DoSomething (or raise the event SomethingHappened), so how could it fire for you? Add the following code in your user control class:
//override the OnVisibleChanged
protected override void OnVisibleChanged(EventArgs e){
if(!Visible) DoSomething();
}
i created user control which looks like this:
and code behind looks like:
using System;
using System.Windows.Forms;
namespace Controlls
{
public partial class Toolbar : UserControl
{
public Toolbar()
{
InitializeComponent();
}
private void pbNaprej_Click(object sender, EventArgs e)
{
}
private void pbNazaj_Click(object sender, EventArgs e)
{
}
private void pbNovo_Click(object sender, EventArgs e)
{
}
private void bpbBrisi_Click(object sender, EventArgs e)
{
}
private void pbPotrdi_Click(object sender, EventArgs e)
{
}
private void txtOd_TextChanged(object sender, EventArgs e)
{
}
}
}
now i move this UC to form but there is a problem. How to get this events from UC. I need to implement click events for buttons in form where i use this user control. What i can see is that i can use it like one component and i can not get separate buttons from it.
Add 2 event handlers for yor control, and subscribe to the toobox event at the place you use it:
using System;
using System.Windows.Forms;
namespace Controlls
{
public enum ToolboxButtonType { Potrdi, Brisi, Novo, Nazaj, Naprej }
public partial class Toolbar : UserControl
{
public Toolbar()
{
InitializeComponent();
}
private void pbNaprej_Click(object sender, EventArgs e)
{
OnToolboxClick(ToolboxButtonType.Naprej);
}
private void pbNazaj_Click(object sender, EventArgs e)
{
OnToolboxClick(ToolboxButtonType.Nazaj);
}
private void pbNovo_Click(object sender, EventArgs e)
{
OnToolboxClick(ToolboxButtonType.Novo);
}
private void bpbBrisi_Click(object sender, EventArgs e)
{
OnToolboxClick(ToolboxButtonType.Brisi);
}
private void pbPotrdi_Click(object sender, EventArgs e)
{
OnToolboxClick(ToolboxButtonType.Potrdi);
}
private void txtOd_TextChanged(object sender, EventArgs e)
{
OnTextChanged(txtOd.Text);
}
}
private OnToolboxClick(ToolboxButtonType button)
{
if (ToolboxClick != null)
{
ToolboxClick(this, new ToolboxClickEventArgs(button));
}
}
private OnTextChanged(string text)
{
if (ToolboxTextChanged!= null)
{
ToolboxTextChanged(this, new ToolboxTextChangedEventArgs(text));
}
}
public class ToolboxTextChangedEventArgs: EventArgs
{
public string Text { get; private set; }
public ToolboxClickEventArgs(string text) { Text = text; }
}
public class ToolboxClickEventArgs : EventArgs
{
public ToolboxButtonType Button { get; private set; }
public ToolboxClickEventArgs(ToolboxButtonType button) { Button = button; }
}
public event EventHandler<ToolboxClickEventArgs> ToolboxClick;
public event EventHandler<ToolboxTextChangedEventArgs> ToolboxTextChanged;
}
For example:
//Toolbar toolbar = new Toolbar();
toolbar.ToolboxTextChanged += (s, e) => { btn1.Text = e.Text; };
toolbar.ToolboxClick += (s, e) =>
{
swith(e.Button)
{
case ToolboxButtonType.Brisi: //
break;
default:
break;
}
};
You need to create event in MainForm class and generarate it in hand mode.
If you also draw your icons on canvas or smth, you need to handle onClick your MainForm, than calculate mouse click position, and generate event that you were create.
If it amount of buttons or smth that has their own onClick events, you can hang on this buttons event the action that will generate your even in main form.
Answer what approach you use, and I past some code.
ADDED:
public partial class Toolbar : UserControl
{
public Toolbar()
{
InitializeComponent();
}
public enum ToolBarCommands
{
Naprej, Nazaj, Novo
}
public Action<object, ToolBarCommands> MenuItemClick;
private void pbNaprej_Click(object sender, EventArgs e)
{
if(MenuItemClick != null)
MenuItemClick(this, ToolBarCommands.Naprej);
}
private void pbNazaj_Click(object sender, EventArgs e)
{
if(MenuItemClick != null)
MenuItemClick(this, ToolBarCommands.Nazaj);
}
private void pbNovo_Click(object sender, EventArgs e)
{
if(MenuItemClick != null)
MenuItemClick(this, ToolBarCommands.Novo);
}
}
How to use it? easy:
// Define section
var toolbar = new Toolbar();
toolbar.MenuItemClick += MenuItemClickHandler;
...
MenuItemClickHandler(object sender, Toolbar.ToolBarCommands item)
{
switch(item)
{
case Toolbar.ToolBarCommands.Naprej:
// Naperej handler
break;
case Toolbar.ToolBarCommands.Nazaj:
// Nazaj handler
break;
case Toolbar.ToolBarCommands.Novo:
// Novo handler
break;
}
}
ADDED:
If names of your buttons same as enum members you can cut this part of code:
private void pbNaprej_Click(object sender, EventArgs e)
{
if(MenuItemClick != null)
MenuItemClick(this, ToolBarCommands.Naprej);
}
private void pbNazaj_Click(object sender, EventArgs e)
{
if(MenuItemClick != null)
MenuItemClick(this, ToolBarCommands.Nazaj);
}
private void pbNovo_Click(object sender, EventArgs e)
{
if(MenuItemClick != null)
MenuItemClick(this, ToolBarCommands.Novo);
}
It will look's like one method (hang this method to all your menu element events)
private void pbItem_Click(object sender, EventArgs e)
{
if(MenuItemClick != null)
{
ToolBarCommands command;
var res = ToolBarCommands.TryParse(((Button)sender).Name, command)
if(res == true)
MenuItemClick(this, command);
}
}