Passing Variables from User Control to User Control - c#

I need a way to pass user control to user controls.
I am using Windows Forms.
For example. Say I have a radiobutton in User control 1 and I want User Control 2 to call and see if that radiobutton is checked on User control 1. How would I reference that?
And for some sample code:
This is UserControl1
public void radioButton1_CheckedChanged(object sender, EventArgs e)
{
}
This is UserControl2
private void button4_Click(object sender, EventArgs e)
{
if (radioButton1.Checked)
//do something
else
//do something
}

Seems like you have two unrelated user controls on your form. And UserControl2 (UC2) should change it's behavior is something happens on UserControl1 (UC1). That means you should have an event on UC1 which will fire if radiobutton1 checked state changes. You also will need to expose radiobutton status. You can do it either with custom EventArgs or with public property:
UserControl1
public event EventHandler SomethingChanged;
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if (SomethingChanged != null)
SomethingChanged(this, EventArgs.Empty);
}
public bool IsSomethingEnabled => radioButton1.Checked;
UC2 should allow changing it's behavior. That can be done with public property
UserControl2
public bool UseCoolFeature { get; set; }
private void button4_Click(object sender, EventArgs e)
{
if (UseCoolFeature)
//do something
else
//do something else
}
And last step - coordinator which will manage both usercontrols. It's your form. Subscribe to event from UC1 and change state of UC2:
Form
private void userControl1_SomethinChanged(object sender, EventArgs e)
{
userControl2.UseCoolFeature = ((UserControl1)sender).IsSomethingEnabled;
}
You can even use in-place event handler:
userControl1.SomethingChanged += (s,e) =>
userControl2.UseCoolFeature = userControl1.IsSomethingEnabled;

You can store the value in your session:
UserControl1
public void radioButton1_CheckedChanged(object sender, EventArgs e)
{
Session["radioButton1Checked"] = radioButton1.Checked;
}
UserControl2
private void button4_Click(object sender, EventArgs e)
{
if (Session["radioButton1Checked"] != null && (bool)Session["radioButton1Checked"])
//do something
else
//do something
}

Related

RoutedEvents Combobox not Firing

Hi could some kind person help. I have two user controls. One with a textbox, the other with a Combobox. The Main window will perform calculation routine as soon as combos and textboxes are modified.
The Textbox version works, the ComboBox doesn't. The only difference I can see is
Textbox uses TextChangedEventArgs
whereas
Combobox uses System.EventArgs
Any ideas?
Thanks
// UserControl - with TextBox
public event RoutedEventHandler ucTextChanged;
private void OnTextChanged(object sender, RoutedEventArgs e)
{
if (ucTextChanged != null)
{
ucTextChanged(this, new RoutedEventArgs());
}
}
private void txtValue_TextChanged(object sender, TextChangedEventArgs e)
{
OnTextChanged(sender, e);
}
// UserControl - ComboBox
public event RoutedEventHandler ucComboChanged;
private void OnComboChanged(object sender, RoutedEventArgs e)
{
if (ucComboChanged != null)
{
ucComboChanged(this, new RoutedEventArgs());
}
}
private void ucCombo_DropDownClosed(object sender, System.EventArgs e)
{
OnComboChanged(sender, e);
}
Try looking at the event SelectionChanged of the ComboBox ( https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.combobox.onselectionchanged )
XAML:
<ComboBox SelectionChanged="ucCombo_SelectionChanged"></ComboBox>
C#:
private void ucCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// your code here...
OnComboChanged(sender, e);
}

Is it possible to group click events?

I have a project that uses various click events and looks like this
namespace Example
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn_obj_Click(object sender, EventArgs e)
{
MyMethods.Method_1("text1");
}
private void btn_catg_Click(object sender, EventArgs e)
{
MyMethods.Method_1("text2");
}
private void btn_up_Click(object sender, EventArgs e)
{
MyMethods.Method_2("text1");
}
private void btn_top_up_Click(object sender, EventArgs e)
{
MyMethods.Method_2("text2");
}
private void btn_down_Click(object sender, EventArgs e)
{
MyMethods.Method_2("text3");
}
private void btn_top_down_Click(object sender, EventArgs e)
{
MyMethods.Method_2("text4");
}
public static class MyMethods
{
public static void Method_1(string text) {...}
public static void Method_2(string text) {...}
}
}
}
As you can see I have a quite a number of click events so i'm curious if I can group them all in another c# file or a class or something
In your code-behind, declare a common method you want to call when any of the above buttons fire the Click event.
private void CommonClick(object sender, EventArgs e)
{
}
Now in your Properties window for each button, you can assign this event handler for all buttons:
Now when any of the buttons are clicked this same event handler is called.
If you want to know which button is clicked, you can either use button Name or even the Tag property.
Let's say we assign a separate unique Tag for each button. Tag is a property you can see in the property window for each button (and most controls).
Then you can use a switch-case statement in your code to identify which button was clicked.
private void CommonClick(object sender, EventArgs e)
{
switch (((Button)sender).Tag)
{
case "B1":
break;
case "B2":
break;
}
}
Above, B1, B2 etc are the tags I've assigned to each button.
usually in the form designer you dblclick on the empty "click" event property to generate new method as btn_..._Click(object sender, EventArgs e).
instead you can select existed method, so multiple buttons can call the same method:
Then in the called Method you can check which control trigger this event:
private void button1_Click(object sender, EventArgs e)
{
if (sender == button2)
{
// ....
}
if (sender == button1)
{
// ....
}
}

Add similar behavior to a group of WinForms controls

I have a form with 6 buttons. These buttons serve to increase/decrease tha value of the respective textbox. Now I'm trying to "animate" the buttons. I want to get another effect on the button when the mouse is over him.
To do that, I have two diferent images in Resources and I am doing this code:
private void btnHoursDown_MouseHover(object sender, EventArgs e) {
btnHoursDown.Image = Game_Helper.Properties.Resources.DownHover;
}
private void btnHoursDown_MouseLeave(object sender, EventArgs e) {
btnHoursDown.Image = Game_Helper.Properties.Resources.Down;
}
This works fine. My question is: it wouldn't be wise to create a class (ButtonBehaviour.cs) and put this code in that class?
So I would have something like this:
ButtonBehaviour buttonBehaviour = new ButtonBehaviour();
private void btnHoursDown_MouseHover(object sender, EventArgs e) {
buttonBehaviour.buttonDownHover();
}
private void btnHoursDown_MouseLeave(object sender, EventArgs e) {
buttonBehaviour.buttonDownLeave();
}
And the class should be:
public class ButtonBehaviour {
public void buttonDownHover() {
// code
}
public void buttonDownLeave() {
// code
}
}
How can I create this Class Behaviour and make the buttons adapt this Behaviour?
if one effect should be applied for all buttons, try to add the same event handlers to them
private void btn_MouseHover(object sender, EventArgs e)
{
(sender as Button).Image = Game_Helper.Properties.Resources.DownHover;
}
private void btn_MouseLeave(object sender, EventArgs e)
{
(sender as Button).Image = Game_Helper.Properties.Resources.Down;
}
button which raised event is available via sender variable
this way you avoid code duplication for every button. creating a ButtonBehaviour or CustomButton is probably an over-engineering unless you need them in many forms

EventHandler for all controls in form

In my code I want to perform some actions when some controls are focused. So instead of having one handler for each control i was wondering if there could be any way of adding all controls to the handler and inside the handler function perform the desired action.
I have this:
private void tb_page_GotFocus(Object sender, EventArgs e)
{
tb_page.Visible = false;
}
private void tb_maxPrice_GotFocus(Object sender, EventArgs e)
{
tb_maxPrice.Text = "";
}
private void tb_maxPrice_GotFocus(Object sender, EventArgs e)
{
tb_maxPrice.Text = "";
}
I want this:
private void AnyControl_GotFocus(Object sender, EventArgs e)
{
if(tb_page.isFocused == true)
{
...
}
else if (tb_maxPrice.isFocused == true)
{
...
}
else
{
...
}
}
Is this possible? How could I do it? Thanks a lot.
Iterate your controls in your form or panel and subscribe to their GotFocus Event
private void Form1_Load(object sender, EventArgs e)
{
foreach (Control c in this)
{
c.GotFocus += new EventHandler(AnyControl_GotFocus);
}
}
void AnyControl_GotFocus(object sender, EventArgs e)
{
//You'll need to identify the sender, for that you could:
if( sender == tb_page) {...}
//OR:
//Make sender implement an interface
//Inherit from control
//Set the tag property of the control with a string so you can identify what it is and what to do with it
//And other tricks
//(Read #Steve and #Taw comment below)
}

how to run the event of user control not regular

I have a user control with some buttons (tmNewItem, tmEdit, tmInsert)
I write a clickButton event for them.
for example:
public void btnEdit_Click(object sender, EventArgs e)
{
btnNew.Enabled = false;
btnEdit.Enabled = false;
}
I used this user control in another project and write another method for the buttons and assign it to the usr control:
public void DTedit(object sender, EventArgs e)
{
}
private void UserControl_Load(object sender, EventArgs e)
{
DT_Navigator.btnCancel.Click += new EventHandler(DTedit);
}
and now, when I run the project and press btnEdit button, the first time, btnEdit_Click will execute and after that DTedit. can i change it? I mean the first time DTedit (that I define it in my project) run, and after it btnEdit_Click (that I define it in the user control) run?
how can I do that?
Try this
public void DTedit(object sender, EventArgs e)
{
//Place your code here
DT_Navigator.btnCancel.Click -= new EventHandler(DTedit); //This will remove handler from the button click and it will not be executed next time.
}
private void UserControl_Load(object sender, EventArgs e)
{
DT_Navigator.btnCancel.Click += new EventHandler(DTedit);
}
Suggested Code
//User control
public event CancelEventHandler BeginEdit;
public event EventHandler EndEdit;
private btnYourButton_Click(object sender, EventArgs e)
{
CancelEventArgs e = new CancelEventArgs();
e.Cancel = false;
if (BeginEdit != null)
BeginEdit(this, e);
if (e.Cancel == false)
{
if (EndEdit != null)
EndEdit(this, new EventArgs);
//You can place your code here to disable controls
}
}

Categories