Can't change form labels on event - c#

I have two static labels that are supposed to show the users counters that I have kept on the back end but they do not change on the form. I have tried searching for answers but I can't understand most of them.
//Watchers
private static void CDdirWatcher_Created(object sender, FileSystemEventArgs e)
{
CDCreated += 1;
}
private static void LPdirWatcher_Created(object sender, FileSystemEventArgs e)
{
LPCreated += 1;
}
So the above are events where my counters go up
And below is the events I'm trying to make to create the change to the labels.
private void cdCounterLbl_TextChanged(object sender, EventArgs e)
{
cdCounterLBL.Text = CDCreated.ToString();
}
private void lpCounterLbl_TextChanged(object sender, EventArgs e)
{
lpCounterLBL.Text = Convert.ToString(LPCreated);
}
I'm trying
cdCounterLBL.TextChanged += cdCounterLbl_TextChanged;
lpCounterLBL.TextChanged += lpCounterLbl_TextChanged;
I've tried the above but the labels still don't change

Thank you for taking the time to share your problem.
It seems that you misunderstood what are things like classes, variables, methods and events, and how to use them.
That said and if I understood what you want to do, this may solve your problem.
TextChanged, for label, is raised when you change the Text property by code.
It is not because you change the values of your counters that some event will be raised for the labels.
Counters and labels are two separate things, totally different and unrelated, without links, but you can link as follows:
private static void CDdirWatcher_Created(object sender, FileSystemEventArgs e)
{
CDCreated += 1;
cdCounterLbl_TextChanged(this, null);
}
private static void LPdirWatcher_Created(object sender, FileSystemEventArgs e)
{
LPCreated += 1;
lpCounterLbl_TextChanged(this, null);
}
private void cdCounterLbl_TextChanged(object sender, EventArgs e)
{
cdCounterLBL.Text = CDCreated.ToString();
}
private void lpCounterLbl_TextChanged(object sender, EventArgs e)
{
lpCounterLBL.Text = LPCreated.ToString();
}
So that does not work because of static methods and we have a bad design because of calling TextChanged to change the Text.
Thus here how to do.
Add a static event:
private static Action CountersUpdated;
Add in the form's Load event:
CountersUpdated += DoUpdateCounters;
And in the form's FormClosed event:
CountersUpdated -= DoUpdateCounters;
With:
private void DoUpdateCounters()
{
cdCounterLBL.Text = CDCreated.ToString();
lpCounterLBL.Text = LPCreated.ToString();
}
Now you can write:
private static void CDdirWatcher_Created(object sender, FileSystemEventArgs e)
{
CDCreated++;
CallCountersUpdated();
}
private static void LPdirWatcher_Created(object sender, FileSystemEventArgs e)
{
LPCreated++;
CallCountersUpdated();
}
private static void CallCountersUpdated()
{
if ( CountersUpdated != null ) CountersUpdated();
}
You can also make two events for CDCounterUpdated and LPCounterUpdated instead of one CountersUpdated.
Have a good job and a good life in C# and OOP.

Related

More efficient way to format several nearly identical functions?

I was wondering if there was a more efficient way to format the following code:
private void nmudc1_Enter(object sender, EventArgs e)
{
SelectAllNumericalUpDownText(nmudc1);
}
private void nmudc1_Click(object sender, EventArgs e)
{
SelectAllNumericalUpDownText(nmudc1);
}
private void nmudc2_Enter(object sender, EventArgs e)
{
SelectAllNumericalUpDownText(nmudc2);
}
private void nmudc2_Click(object sender, EventArgs e)
{
SelectAllNumericalUpDownText(nmudc2);
}
And so on.
I apologize if this has been asked before or elsewhere, and I assume the answer is relatively simple. Unfortunately, I have been unsuccessful in finding a suitable answer thus far.
I have thought about trying to put in in an array, or an object, but I really just have no idea how I would go about doing this to a function like this.
Thank you.
You can create only one event function, And send the sender as parameter to the SelectAllNumericalUpDownText function.
Register all events to this function:
nmudc1.Click += nmudc_Event;
nmudc1.Enter += nmudc_Event;
nmudc2.Click += nmudc_Event;
nmudc2.Enter += nmudc_Event;
Generic implementation to the function:
private void nmudc_Event(object sender, EventArgs e)
{
SelectAllNumericalUpDownText(sender as <TYPE>);
}
Since all event handlers in your code sample have the same method signature, you can simply use the same method to handle all the events:
private void NumericUpDown_event(object sender, EventArgs e)
{
SelectAllNumericalUpDownText(sender as NumericUpDown);
}
This is assuming, of course, that your sender object is actually a NumericUpDown.
You could use the following alternative:
private void NumericUpDown_Event(object sender, EventArgs e)
{
if (!(sender is <controltype> nump))
return;
SelectAllNumericalUpDownText(nump);
}
By this you do not call the method if the sender is not of type < controltype >. Therefor you already used the generic EventArgs class you can subscript the Enter/Click event to this method.

How to Fire DropDownClosed Event from a Button

I'm working on a WinForms app. My ComboBox has DropDownClosed event, but I need to fire this event from a Button. How can I do this?
Like this:
private void button1_Click(object sender, EventArgs e)
{
comboBox1_DropDownClosed(sender, e);
}
private void comboBox1_DropDownClosed(object sender, EventArgs e)
{
MessageBox.Show("Test");
}
Please take a look , I believe this is what you are talking about
private void abc_Click(object sender, RoutedEventArgs args)
{
}
private void xyz_Click(object sender, RoutedEventArgs args)
{
abc_Click(sender, args);
}

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

Find which Event was Fired

I am attempting to capture which Event was fired. I have two events that point to the same function, CurrentLoan_LogEntryEvent. Inside CurrentLoan_LogEntryEvent, how do I determine which Event was actually fired: LogEntryAdded or LogEntryChange.
Below you'll find sample of my code how I have it now. Let me know if you have any questions about my code.
CurrentLoan is a Loan object, which has two events.
public MyApplication()
{
ThirdPartyDLL.LoanOpened += new EventHandler(CurrentLoanOpened);
}
private void CurrentLoanOpened(object sender, EventArgs e)
{
ThirdPartyDLL.CurrentLoan.LogEntryAdded += CurrentLoan_LogEntryEvent;
ThirdPartyDLL.CurrentLoan.LogEntryChange += CurrentLoan_LogEntryEvent;
}
private void CurrentLoan_LogEntryEvent(object sender, LogEntryEventArgs e)
{
// When LogEntry was Added or Changed.
// How do I determine if LogEntryAdded or LogEntryChange was fired?
}
If you want to differentiate two events, no point in attaching a single method for multiple events. Give them different handlers.
You typically attach single handler for multiple events when you don't care about where the event origin, but you always wanted to do the same thing in the handler.
If you have some common logic to be executed, you can call it inside the handlers.
private void CurrentLoanOpened(object sender, EventArgs e)
{
ThirdPartyDLL.CurrentLoan.LogEntryAdded += CurrentLoan_LogEntryAdded;
ThirdPartyDLL.CurrentLoan.LogEntryChange += CurrentLoan_LogEntryChange;
}
private void CurrentLoan_LogEntryAdded(object sender, LogEntryEventArgs e)
{
//LogEntryAdded fired
YourOptionalCommonMethodIfAny();
}
private void CurrentLoan_LogEntryChange(object sender, LogEntryEventArgs e)
{
//LogEntryChange fired
YourOptionalCommonMethodIfAny();
}
Why don't you simply do this:
private void CurrentLoanOpened(object sender, EventArgs e)
{
ThirdPartyDLL.CurrentLoan.LogEntryAdded += CurrentLoan_LogEntryAddedEvent;
ThirdPartyDLL.CurrentLoan.LogEntryChange += CurrentLoan_LogEntryChangeEvent;
}
private void CurrentLoan_LogEntryAddedEvent(object sender, LogEntryEventArgs e)
{
// First do what you must do specifically for added events
CurrentLoan_LogEntry(e);
}
private void CurrentLoan_LogEntryChangeEvent(object sender, LogEntryEventArgs e)
{
// First do what you must do specifically for changed events
CurrentLoan_LogEntry(e);
}
Binding one handler to multiple events and then figuring out what to do inside that handler is just overcomplicating things.
Always keep your code as simple to understand and change as possible.

How to call a button click event from another method

How can I call SubGraphButton_Click(object sender, RoutedEventArgs args) from another method?
private void SubGraphButton_Click(object sender, RoutedEventArgs args)
{
}
private void ChildNode_Click(object sender, RoutedEventArgs args)
{
// call SubGraphButton-Click().
}
You can easily do it by the following piece of code (assuming that name of your button is btnButton):
btnButton.PerformClick();
You can call the button_click event by simply passing the arguments to it:
private void SubGraphButton_Click(object sender, RoutedEventArgs args)
{
}
private void ChildNode_Click(object sender, RoutedEventArgs args)
{
SubGraphButton_Click(sender, args);
}
you can call the button_click event by passing..
private void SubGraphButton_Click(object sender, RoutedEventArgs args)
{
}
private void ChildNode_Click(object sender, RoutedEventArgs args)
{
SubGraphButton_Click(sender, args);
}
Also without passing..
private void SubGraphButton_Click(object sender, EventArgs args)
{
}
private void Some_Method() //this method is called
{
SubGraphButton_Click(new object(), new EventArgs());
}
You can perform different approaches to work around this. The best approach is, if your both buttons are suppose to do the same job, you can define a third function to do the job. for example :
private void SubGraphButton_Click(object sender, RoutedEventArgs args)
{
myJob()
}
private void ChildNode_Click(object sender, RoutedEventArgs args)
{
myJob()
}
private void myJob()
{
// Your code here
}
but if you are still persisting on doing it in your way, the best action is :
private void SubGraphButton_Click(object sender, RoutedEventArgs args)
{
}
private void ChildNode_Click(object sender, RoutedEventArgs args)
{
SubGraphButton_Click.PerformClick();
}
In WPF, you can easily do it in this way:
this.button.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
Usually the better way is to trigger an event (click) instead of calling the method directly.
private void PictureBox1_Click(object sender, EventArgs e)
{
MessageBox.Show("Click Succes");
}
private void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
PictureBox1_Click(sender, e); //or try this one "this.PictureBox1_Click(sender, AcceptButton);"
}
}
You can simply call it:
SubGraphButton_Click(sender, args);
Now, if your SubGraphButton_Click does something with the args, you might be in trouble, but usually you don't do anything with them.
For me this worked in WPF
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
RoutedEventArgs routedEventArgs = new RoutedEventArgs(ButtonBase.ClickEvent, Button_OK);
Button_OK.RaiseEvent(routedEventArgs);
}
}
Use InvokeOnClick event. it works even if the button is invisible/disabled
A simple way to call it from anywhere is just use "null" and "RoutedEventArgs.Empty", like this:
SubGraphButton_Click(null, RoutedEventArgs.Empty);
For WPF:
YourButtonName.RaiseEvent(new RoutedEventArgs(System.Windows.Controls.Primitives.ButtonBase.ClickEvent));
Add it to the instance of the Click delegate:
ChildNode.Click += SubGraphButton_Click
which is inkeeping with the pattern .NET events follow (Observer).
For people wondering, this also works for button click.
For example:
private void btn_Click(object sender, EventArgs e)
{
MessageBox.Show("Test")
}
private void txb_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
btn_Click(sender, e);
}
When pressing Enter in the textfield(txb) in this case it will click the button which will active the MessageBox.
we have 2 form in this project.
in main form change
private void button1_Click(object sender, EventArgs e)
{
// work
}
to
public void button1_Click(object sender, EventArgs e)
{
// work
}
and in other form, when we need above function
private void button2_Click(object sender, EventArgs e)
{
main_page() obj = new main_page();
obj.button2_Click(sender, e);
}

Categories