WindowLoaded() not getting called - c#

I have a breakpoint in a form's OnLoaded event (which dynamically creates some controls), yet when I instantiate the form, it simply sits there looking blanched, and Window_Loaded() is never called.
I instantiate the form with a custom constructor:
NoUseForAName nufan = new NoUseForAName(iListMsgTypes, dtFrom, dtTo);
nufan.Show();
And have added the Loaded() event, which I expect to get called directly after I call .Show() on the form:
private void Window_Loaded(object sender, RoutedEventArgs e)
Why is [Window_]Loaded() not getting reached?

You should either add
InitializeComponent();
as the first line of your custom constructor, or call the default constructor like this:
public NoUseForAName(...)
: this()
{
...
}

Did you hook up the event anywhere? e.g. on the new instance
nufan.Loaded += Window_Loaded;
(Also loaded is not called "directly" after show, loading may take a while)

Related

How to call a function inside a form from a user control (C# Winforms)

So I have user control I want to hide/send to back and I want to call the public function in my form where the user control is, from the control itself.
I have a button in the user control with the following code:
mainboard MAIN = new mainboard(); // mainboard is a form to call to.
MAIN.PastLockScreen(); // PastLockScreen is a public void inside mainboard
When I click the button the public function in mainboard does not get called. There are no errors, what am I doing wrong, and how can I call a function in a form from a user control?
void inside mainboard
public void PastLockScreen()
{
lockscreen1.SendToBack(); // lockscreen1 is the usercontrol that this function gets called from
}
The void is being referenced but not called?
Edit: I have done some investigating and turns out that my timers I have in any form or control, also dont work. But buttons on the actual form itself do work. (and yes I did do timerName.Start(); when the form/control loads.)
Solved the issue above, my timers needed to display time, which the time string I defined inside the class instead of inside the timer.tick
From within the UserControl, just cast ParentForm to type mainboard:
// .. form within the UserControl that is CONTAINED by form mainboard ...
private void button1_Click(object sender, EventArgs e)
{
mainboard MAIN = this.ParentForm as mainboard;
if (MAIN != null)
{
MAIN.PastLockScreen();
}
}
Note that this is a TIGHTLY COUPLED approach that limits your use of the UserControl to only mainboard.
A better approach would be to make the UserControl raise some kind of custom EVENT that the mainboard form then subscribes to. When the event is received then the form itself would run the appropriate method. This would mean you could potentially utilize the UserControl in a different form without changing any code within it. This latter approach would be LOOSELY COUPLED.
Try This
In Form1
to Show the Form2
Form2 frm2 = new Form2();
frm2.Show();
To call the method do you want
Form2 cmd = new Form2();
cmd.PastLockScreen();
In Form2
public void PastLockScreen()
{
this.SendToBack();
}

Why does Message Box appear twice when event is called.

So I've been following the Visual Studio tutorials that microsoft has available (more specifically the math quiz one found at https://msdn.microsoft.com/en-us/library/vstudio/dd492172.asp)
but I deviated a bit from the tutorial because I wanted to see if I could create an event and call it using the EventHandler delegate though it might not be the best solution.
public event EventHAndler quizStarted;
here is the code for creating the event.
now in the method
public Form1()
{
this.quizStarted += new System.EventHandler(this.showThatTheQuizStarted);
InitializeComponent();
}
I have initialized my event with an instance of the EventHanlder that points to my showThatTheQuizStarted method.
public void showThatTheQuizStarted(object sender, EventArgs e)
{
MessageBox.Show("Quiz Has Started");
}
and finally when the start button is pressed I call the quizStarted event as shown below.
private void startButton_Click(object sender, EventArgs e)
{
startButton.Enabled = false;
quizStarted(this, new EventArgs());
this.StartTheQuiz();
}
in this order the message box goes away after hitting okay once, also in StartTheQuiz() nothing calles a message box directly or indirectly.
but if I place the this.quizStarted += new System.EventHandler(this.showThatTheQuizStarted); line into the startButton_Click method, the message box appears twice one right after the other.
Though I found a solution I would like to know why this happens if I place this line of code out of the constructor.
If...
this.quizStarted += new System.EventHandler(this.showThatTheQuizStarted);
... gets called multiple times, as would happen if you move it inside a button Click event handler method, then you are in fact adding and registering a new event handler every time.
In other words, when quizStarted is invoked, it may call multiple event handlers, depending on how many you choose to register. If you register the same event handler multiple times, then it will get called as many times.
That's why you want to leave the registration in a place where you are guaranteed to register the event handler once and only once.

C# Objects (Creating and Using)

In C# if you create an object on a button click, at the end of the click event, the object is no longer in scope. How do I create an object on the click of a button, but release it on the click of another button. Here is what I am trying to do:
Build an application that accepts user data and adds it to a database. The application contains two classes: Personal information and employer information. The personal information class object should be created when the "Start Application" button is pressed. The application then opens a tab that requests personal information. If the "Add Employer" button is selected, an instance of Employer is created. After they press submit at the end, it should close the one or two objects and return to the main menu. When the next person presses the button, it should go through the process again.
I can create objects on click or in the class itself, but how do you limit the life of the object between to button presses?
Thanks!
Use a class-scoped data member. Very quick example:
public class MyClass
{
private Foo foo;
private void Button1_Click(object sender, EventArgs e)
{
this.foo = new Foo();
}
private void Button2_Click(object sender, EventArgs e)
{
// I can access this.foo here. I can also dispose of it
// if it is IDisposable and/or I can set it to null.
// To check if button 1 was pressed, check to see if the object
// is null (if it is, button 1 wasn't pressed)
}
}
You can do this by defining one of the event handlers anonymously inside of the other event handler:
void button1_Click(object sender, EventArgs e)
{
Foo data = new Foo();
EventHandler handler = null;
handler = (s, args) =>
{
//Use Foo here
DoStuff(data);
button2.Click -= handler;
};
button2.Click += handler;
}
Note that we need to do some extra work to make sure to remove the event handler when it is clicked here. If you don't need to do that, then it does simplify the code.
Here the anonymous method is closing over the local variable, which will extend that variable's lifetime for the lifetime of the anonymous method. This approach, unlike those using a field, actually creates a variable who's lifetime lasts until the next button is clicked, and doesn't leave a variable lying around with a meaningless value before the first button is clicked or after the second is clicked.
Sounds like you're wanting to use properties?
Here's a link to MSDN, but there's lots out there if you just Google'C# properties'
I can create objects on click or in the class itself, but how do you limit the life of the object between to button presses?
Sadly you're pretty much stuck with putting it at class scope. There is no simply way to limit the scope of an object to just a few methods within a class. If your 2 event handlers can access your Employer, then so can every other method within the class. You just have to not use it.
However, out of curiosity I managed to hack around and produce the effect you want. I did a class with 2 event handlers, one for each button, as well as a private Employer variable.
public class ButtonHandler
{
private Employer employerData;
public void button1_Click(object sender, EventArgs e)
{
employerData = "set data here";
}
public void button2_Click(object sender, EventArgs e)
{
employerData = "use / clear data here";
}
}
Then I went to my Form.Designer.cs and manually changed the button.Click event handler from the default this.btnLoadPlaylist_Click to my functions within the ButtonHandler class above. The problem with this is it gets deleted every time the Designer.CS is regenerated by Visual Studio. So the effect is possible, just not convenient with the tools we are given.

MainForm_Load doesn't work c#

The MainForm_Load doesn't work, the form is showing as I designed, but nothing in the load method happens. When I put a breakpoint it just skip on this method, I tried to delete the method and recreate it but it still not working.
I'm using Visual Studio 2010, everithing works fine with other projects I did.
Even the most basic function not working in it.
Here is an example:
public FormMain()
{
InitializeComponent();
}
private void FormMain_Load(object sender, EventArgs e)
{
MessageBox.Show("Test");
}
Any ideas??
Tnx
Did you type this by hand?
You need to assign the event, if you do this on the designer it will auto generate the code. If you want to do by hand you have to manually assign the event.
public FormMain()
{
InitializeComponent();
this.Load += FormMain_Load;
}
Everything looks fine here.Please check this.
Go to Properties window of your form(here it is FormMain.cs[Design] ). Click on event section. check in Load event your
FormMain_Load
method is defined or not ?
give the breakpoint and check it is calling or not.
Give breakpoint on the form constructor .
And you need to check, From where you are calling this form ?
if this form is the first form in your application, then go to Program.cs file. and check there this is available or not inside Main Function.
Application.Run(new FormMain());
In FormMain.Designer.cs page check
this.Load += new System.EventHandler(this.FormMain_Load);
is available inside
private void InitializeComponent()
{
}
or not?

Button Trigger an Event Or Command that will be caught by another class

I am trying to trigger an event from a Button that will be caught in a different class without having this class as an instance in my class. Can I do that?
Lets say my Buttons are getting created in PictoPanelViewModel and this class doesnt have any reference to the MainViewModel, I want myButton to trigger an event that will call a method inside MainViewModel.
I tried myButton.Command and myButton.Click but these two need a reference of MainViewModel so I can call it.
I'm a little bit confused now.
EDIT
The Buttons are created dynamically in PictoPanelViewModel
SI assume that MainViewModel has a reference to PictoPanelViewModel at least for an instant, and, to be in the worst case, that the buttons have not been created yet at that time. If this is the case I would:
Add an event myButtonClickedEvn to PictoPanelViewModel
Create a method TriggerMyButtonClickedEvn which simply triggers the event in PictoPanelViewModel
Associate TriggerMyButtonClickedEvn to myButton.Click
In MainViewModel, at the time your class sees PictoPanelViewModel, associate your method to the newly created event.
All this translates in code like this.
In PictoPanelViewModel:
this.myButton.Click += new System.EventHandler(this.TriggerMyButtonClickedEvn);
public event EventHandler myButtonClickedEvn;
private void TriggerMyButtonClickedEvn(object sender, EventArgs e)
{
if (myButtonClickedEvn != null)
myButtonClickedEvn(sender, e);
}
In MainViewModel (in a place where you have the instance of PictoPanelViewModel):
aPictoPanelViewModel.myButtonClickedEvn += new System.EventHandler (myButtonClickedInPictoPanelViewModel);

Categories

Resources