C# - How to Transfer Information Between User Control - c#

im doing an application where user enter a value inside the text box then he press a button, both in the same user control. Then the result from the text box will show at the label of other user control. Both of the user control is in the same windows form.
Thanks!
Image of user interface

The most common way to do this is use an event. This is how I would do it:
First define an EventArgs:
public class MyEventArgs : EventArgs
{
public string Text { get; private set; }
public MyEventArgs(string Text)
{
this.Text = Text;
}
}
Then in your UserControl (the one with the button):
public partial class MyUserControl
{
public event EventHandler<MyEventArgs> ButtonClicked;
public MyUserControl()
{
//...
button1.Click += (o, e) => OnButtonClicked(new MyEventArgs(textBox1.Text));
}
protected virtual void OnButtonClicked(MyEventArgs args)
{
var hand = ButtonClicked;
if(hand != null) ButtonClicked(this, args);
}
}
Then subscribe to your MyUserControl.ButtonClicked event in the form and call a method in the second control.
Note if the behavior of the button and the text in the textbox are actually not related, you can use a property to get the text entered and an empty EventArgs for your event instead.
P.S. The names MyEventArgs, MyUserControl, and ButtonClicked are just for demonstration purposes. I encourage you to use more descriptive/relevant naming in your code.

try this:
public class FirstUserControl:UserControl
{
Public event EventHandler MyEvent;
//Public property in your first usercontrol
public string MyText
{
get{return this.textbox1.Text;} //textbox1 is the name of your textbox
}
private void MyButton_Clicked(/* args */)
{
if (MyEvent!=null)
{
MyEvent(null, null);
}
}
//other codes
}
public class SecondUserControl:UserControl
{
//Public property in your first usercontrol
public string MyText
{
set{this.label1.Text = value;} //label1 is the name of your label
}
//other codes
}
then in your MainForm:
public class MainForm:Forms
{
//Add two instance of the UserControls
public MainForm()
{
this.firstUserControl.MyEvent += MainWindow_myevent;
}
void MainWindow_myevent(object sender, EventArgs e)
{
this.secondUserControl.MyText = this.firstUserControl.MyText;
}
//other codes
}

Related

Binding TextBox to a Windows Form not working

My environment is:
Windows 11
Windows Form App created by the Windows Forms App template of Visual Studio 2022.
Problem:
I have a simple Windows Form with only a Textbox and a Button.
I am trying to update the Textbox text with new data whenever the button is pressed.
The binding works when the Windows Form is loaded. The text "12.34" appears in the Textbox.But when I click on the button, the Textbox is not updated with the new data.
Here is my code:
namespace WatchChart;
public partial class WatchForm:Form
{
public class BidAsk
{
public string? BidString { get; set; }
}
public WatchForm()
{
InitializeComponent();
}
private void MyForm_Load(object sender, EventArgs e)
{
var bid = new BidAsk() { BidString = "12.34" };
bidTextBox.DataBindings.Add(nameof(TextBox.Text), bid, nameof(BidAsk.BidString));
}
private void displayButton_Click(object sender, EventArgs e)
{
var bidask = new BidAsk();
bidask.BidString = "23.45";
}
}
Any help will be greatly appreciated,Charles
The first potential issue is that you need the bound variable to be a member of the main form (instead of making a local var inside the methods). The other potential issue is making sure to enable two-way binding. The BidString property should be implemented similar to the BidAsk shown in order to fire a PropertyChange event whenever its value changes.
class BidAsk : INotifyPropertyChanged
{
string _bidString = string.Empty;
public string BidString
{
get => _bidString;
set
{
if (!Equals(_bidString, value))
{
_bidString = value;
OnPropertyChanged();
}
}
}
public event PropertyChangedEventHandler? PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Test
Here's the code I used to test this answer::
public partial class MainForm : Form
{
public MainForm() => InitializeComponent();
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
BidAsk = new BidAsk();
textBoxBid.DataBindings.Add(
propertyName: nameof(TextBox.Text),
dataSource: BidAsk,
dataMember: nameof(BidAsk.BidString));
buttonDisplay.Click += onClickButtonDisplay;
}
BidAsk BidAsk { get; set; }
private void onClickButtonDisplay(object? sender, EventArgs e)
{
// Test string gobbledegook
var marklar = Guid.NewGuid().ToString().Substring(0, 11);
BidAsk.BidString = marklar;
}
}
You are creating a new BidAsk (let's call it bidAsk1) on load, and binding to that. Then on the button click, you are creating another BidAsk (bidAsk2) and setting the content. Your textbox is still bound to bidAsk1, not bidAsk2 so it will not be updated.
Try keeping a reference to the bound object:
namespace WatchChart;
public partial class WatchForm:Form
{
public class BidAsk
{
public string? BidString { get; set; }
}
private BidAsk bid;
public WatchForm()
{
InitializeComponent();
}
private void MyForm_Load(object sender, EventArgs e)
{
bid = new BidAsk() { BidString = "12.34" };
bidTextBox.DataBindings.Add(nameof(TextBox.Text), bid, nameof(BidAsk.BidString));
}
private void displayButton_Click(object sender, EventArgs e)
{
bid.BidString = "23.45";
}
}

Need event handler in another class

I have a user control(VCtrlDetails in the code below) that hosts a data grid(detailsGrid) which is private.
Now, i have this control loaded in another user control(UcResult_Details) and i want to handle grid selection changed event in this another user control.
public partial class VCtrlDetails : UserControl
{
public event EventHandler<bool> EnableEditTemplateButton;
private void InitializeComponent()
{
private System.Windows.Forms.DataGrid detailsGrid;
this.detailsGrid.SelectionChanged += new
System.EventHandler(this.detailsGrid_SelectionChanged);
}
private void detailsGrid_SelectionChanged(object sender, EventArgs e)
{
EnableEditButton?.Invoke(this, IsApproved());
}
public bool IsApproved()
{ }
}
public partial class UcResult_Details : UserControl
{
private readonly VCtrlDetails vCtrlDetails;
UcResult_Details()
{
//Need to subscribe to vCtrlDetails' grid selection changed event here in this ctor
}
}
I'm not that well versed with event handlers, so stuck with the solution as the grid object is private in the user control 'VCtrlDetails', so cannot directly do something like:
vCtrlDetails.detailsGrid.SelectionChanged += DetailsGrid_SelectionChanged
You need to bubble the event up and out of the VCtrlDetails class. You could do so by creating an event within the VCtrlDetails class and allowing your UcResult_Details class to subscribe to it.
public partial class VCtrlDetails : UserControl
{
public event EventHandler<bool> EnableEditTemplateButton;
public event EventHandler<EventArgs> DetailsGridSelectionChanged;
private void InitializeComponent()
{
private System.Windows.Forms.DataGrid detailsGrid;
this.detailsGrid.SelectionChanged += new
System.EventHandler(this.detailsGrid_SelectionChanged);
}
private void detailsGrid_SelectionChanged(object sender, EventArgs e)
{
EnableEditButton?.Invoke(this, IsApproved());
//Raise your custom event
DetailsGridSelectionChanged?.Invoke(this, e);
}
public bool IsApproved()
{
}
}
public partial class UcResult_Details : UserControl
{
private readonly VCtrlDetails vCtrlDetails;
UcResult_Details()
{
//Need to subscribe to vCtrlDetails' grid selection changed event here in this ctor
this.vCtrlDetails.DetailsGridSelectionChanged += new
EventHandler(this.vCtrlDetailsSelectionChanged);
}
private void vCtrlDetailsSelectionChanged(object sender, EventArgs e)
{
//Do whatever
}
}

C# How to have a form subscribe to an event which is within a user control within a floy layour panel?

So I have a form on a WinForms app.
On that form is a FlowLayoutPanel.
On the FlowLayout panel is a bunch of user controls each representing rows from a table from a database.
On each control is a button.
How do I have the form subscribe to a button click on one of the controls passing back that rows database info?
This is the control code:
public partial class ctrlLeague : UserControl
{
public League activeLeague = new League();
public event EventHandler<MyEventArgs> ViewLeagueClicked;
public ctrlLeague(League lg)
{
InitializeComponent();
lblLeagueName.Text = lg.leagueName;
activeLeague = lg;
}
private void btnViewLeague_Click(object sender, EventArgs e)
{
ViewLeagueClicked(this, new MyEventArgs(activeLeague));
}
public class MyEventArgs : EventArgs
{
public MyEventArgs(League activeLeague)
{
ActiveLeague = activeLeague;
}
public League ActiveLeague { get; }
}
}
if I put the following into the form constructor it tells me "
You can define your favorite event with delegate and call it wherever you want, here it is called inside btnView_Click.
This means that whenever btnView_Click called, your event is
actually called.
public partial class ctrlLeague : UserControl
{
public League activeLeague = new League();
public event EventViewLeagueClicked ViewLeagueClicked;
public delegate void EventViewLeagueClicked(object Sender);
public ctrlLeague(League lg)
{
InitializeComponent();
lblLeagueName.Text = lg.leagueName;
activeLeague = lg;
}
private void btnViewLeague_Click(object sender, EventArgs e)
{
if (ViewLeagueClicked != null)
ViewLeagueClicked(activeLeague);
}
}
now use
public Form1()
{
InitializeComponent();
League league = new League();
league.leagueName = "Seri A";
//
//These lines are best added in Form1.Designer.cs
//
ctrlLeague control = new ctrlLeague(league);
control.Location = new System.Drawing.Point(350, 50);
control.Name = "ctrlLeague";
control.Size = new System.Drawing.Size(150, 100);
control.ViewLeagueClicked += Control_ViewLeagueClicked;
this.Controls.Add(control);
}
private void Control_ViewLeagueClicked(object Sender)
{
League l = Sender as League;
MessageBox.Show(l.leagueName);
}

EventHandler listener

I'm in a position where I have two classes, one has an event handler for a button and I need to be able to listen to that event handler in the other class in order to make changes in the non-button class. I don't have much experience with this type of scenario so am not quite sure where to start.
Here is an exampple of this two classes (if I understood the question right).
class Form
{
Button _button1, _button2;
public Form()
{
_button1 = new Button("button1");
_button2 = new Button("button2");
_button1.Click += _button_Click;
_button2.Click += _button_Click;
}
void _button_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
Console.WriteLine(button.Name);
}
public void Click1()
{
_button1.FireEvent();
}
public void Click2()
{
_button2.FireEvent();
}
}
class Button
{
public event EventHandler Click;
public string Name;
public Button(string name)
{
Name = name;
}
public void FireEvent()
{
Click(this, new EventArgs());
}
}
Usage:
Form f = new Form();
f.Click1();
f.Click2();

C# How to access setter method on main form, from my user control?

I have a UserControl, that contains a panel, the panel contains a picture box.
When I MouseMove over the Picture Box, I want to update a label on the MainForm.
I have a get/set method on the main form, but how do I use it?? thanks
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
public String MouseCords
{
get { return this.MouseCordsDisplayLabel.Text; }
set { this.MouseCordsDisplayLabel.Text = value; }
}
}
public partial class ScoreUserControl : UserControl
{
public ScoreUserControl()
{
InitializeComponent();
}
private void ScorePictureBox_MouseMove(object sender, MouseEventArgs e)
{
// MainForm.MouseCords("Hello"); //What goes here?
}
}
Actually it's possible to do in your case like:
((MainForm)this.ParentForm).MouseCords = "Some Value Here";
But the right way is with events like Felice Pollano mentinoed:
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
this.myCustomControlInstanse.PicureBoxMouseMove += new EventHandler<StringEventArgs>(myCustomControlInstanse_PicureBoxMouseMove);
}
private void myCustomControlInstanse_PicureBoxMouseMove(object sender, StringEventArgs e)
{
this.MouseCordsDisplayLabel = e.Value // here is your value
}
}
public class StringEventArgs : EventArgs
{
public string Value { get; set; }
}
public partial class ScoreUserControl : UserControl
{
public event EventHandler<StringEventArgs> PicureBoxMouseMove;
public void OnPicureBoxMouseMove(String value)
{
if (this.PicureBoxMouseMove != null)
this.PicureBoxMouseMove(this, new StringEventArgs { Value = value });
}
public ScoreUserControl()
{
InitializeComponent();
}
private void ScorePictureBox_MouseMove(object sender, MouseEventArgs e)
{
this.OnPicureBoxMouseMove("Some Text Here");
}
}
Ideally, you should raise an event for the same.
Create a delegate
public delegate void Update();
in the user control
public class MyUserControl : UserControl
{
public event Update OnUpdate;
}
On the main form register a handler for the user controls event.
public class Main
{
public Main()
{
myUserControl.OnUpdate += new Update(this.UpdateHandler);
}
void UpdateHandler()
{
//you can set the delegate with sm arguments
//set a property here
}
}
On user control,
To raise an event on button click
do this
OnUpdate();
This might give you an idea...
public partial class ScoreUserControl : UserControl
{
public ScoreUserControl()
{
InitializeComponent();
}
private void ScorePictureBox_MouseMove(object sender, MouseEventArgs e)
{
// MainForm.MouseCords("Hello"); //What goes here?
MainForm parent = this.ParentForm as MainForm;
if (parent != null) parent.MouseCordsDisplayLabel.Text = "Hello";
}
}
You have several options:
Create an event on the user control and have to form listen to it (I think this is the recommended way by most C# programmers).
Pass a reference to the main form to the User Control (in the constructor). This way, the user control knows about its MainForm.
Cast this.ParentForm to the MainForm class, then you have the reference.
Options 2 and 3 are somewhat more comfortable and lazy, but the cost is that the user control has to know about the specific class MainForm. The first option has the advantage that you could reuse the user control in another project, because it does not know about the MainForm class.
You should publish an event from the user control and subscribe to it from the main form.
At least this is the pattern suggested for winform. In any case the idea is to make the control "observable" from the agents who need to see the coords, instead of using it as a driver to update the interested agents.

Categories

Resources