c# winform programming - c#

I have two forms; one called 'win' and the other called 'loss'. There is a button on 'win' form which displays the 'loss' form. When this button is clicked both forms are visible. When I close the 'loss' form and then click the button on the 'win' form again I get the following exception:
An unhandled exception has occured: Unable to access a disposed object ..object :form
Please could someone point me in the right direction so I can resolve this?

It is because your 'loss' form is already closed and has been disposed, so it cannot be used anymore. You need to create a new instance of the form, like so (don't know how exactly your code looks):
this.loss = new LossForm();
this.loss.Show();

You can verify IsDisposed property of form, before referencing it.
E.g. button click handler on 'win' form:
if (loss.IsDisposed)
return;
// do stuff with loss form
Update: I think it's better not to share control between forms.
You can run 'loss' form as Dialog. And read all needed properties after dialog closed.
You can subscribe to 'loss' form events and process them in 'win' form.

It's not a very good model your going for but you could hook into the formClosing event, cancel it and then hide the form instead. That means the form wont be automatically disposed and you could call show again.
Put some time aside to research MVC architecture - it looks complicated at first, but it really does help.

Related

close windows form works on a form but not on another one

I have crated a windows form application.
VS2012 created a form1 form to me
then I crated a second form called Main
there is a button on form1 form to go to Main form
when I run the application using vs2012, and then I click the x button at the top right of form form1, the form close and I can start working on vs. but the problem is when I click the x button at the top right of the form Main, I can't start working on VS, but I need to click Stop Debugging red button in the vs in order to be able to type in vs.
so my question is when I give this application to my customers, and when the click the x button at the top right of the form, I need the application to close totally, I don't need them to go the task manager and close the application from it.
Your application starts with Form 1, when you close it you are closing the application. Main is just a form, when you close it you are closing the form. What you need to is, go to FormClosing event of Main form and add
Application.Exit();
got to the FormClosing event and put this code :
System.Envirenement.Exit(0);
Problem is because your Form1 form still running(hidden).
If you use Form1 only for open your Main form. Then delete Form1 and set Main as StartUp form in Properties of your proejct:
Application -> StartUp form
After this when you close Main form application will be closed properly
If you use Form1 then you need close Form1 too after you close Main form.
Use FormClosed eventhandler for this purpose
What do you do with form1 when you go to Main? If you just hide it in order to show the other, then close the other one, while just keeping form1 hidden, then form1 will naturally still be running, even if Main is closed.
Sounds like the logical thing to do would be to go back to form1 from Main when you close the latter - otherwise, you can close both using Application.Exit(0) on the appropriate event.
In either case, it would be easier to answer this if you had provided some actual code.
Update:
You could also use System.Environment.Exit(0), but that might require more privileges, which might or might not affect your situation. Again - some code would be helpful.
In regard to your comments, see this answer for more info about termination options.
Right click on Main form -> Properties
on the Event tab choose FormClosing event
and write this code
private void Main_FormClosing(object sender, FormClosingEventArgs e)
{
System.Environment.Exit(0);
}

Need an event that will fire after activating a child form

I am working with an existing WinForm application and it uses the following code to re-activate a child form that has previously been loaded:
private void Activate(Form frm)
{
frm.WindowState = FormWindowState.Maximized;
this.ActivateMdiChild(frm);
}
The trouble I'm having is that when the form is re-activated, I can not seem to find any event on the form that gets raised naturally by the code above. I'm also having difficulty adding code to manually raise the event on the child form, and I think it's because I'm working with a generic Form object.
I've tried Load, MdiChildActivate, MaximumSizeChanged, Activated and a few other events, and none of them fire upon running the code above. I basically need to update some of the form elements after that code is called above and figured an event would work well.
Does anyone know an event that will be raised on the child form after it has been reactivated?
Have you tried the Enter event of the child form? I don't think it fires if the form is already active though.
Another option would be to cast it as your own type and add your own event, then fire it directly.

What events are triggered when ShowDialog(ParentForm) is called in C#

Simple question. I have a MainForm and a settingsForm. The settings form is initialized once and then shown every time the user clicks a button. I need it to do something when this button is clicked.
m_inputSettings.ShowDialog(this); //edit settings selected, so show that form
This is the MainForm calling the settings form, which it does fine. But I need the SettingsForm to do something every time this happens. Presently, I cant figure out if this call actually triggers any events I can set handlers for. Does it trigger an event? If not, is there another way I can tell my SettingsForm to do something every time this call happens?
Note: Any code in the mainform after that line doesn't get executed until the SettingsForm returns, but that is intentional.
Thanks.
Edit: One of the things I want my form to do it select a specific control when this happens, but it seems that that is impossible until after the form is done loading everything.
You can override the OnVisibleChanged method in your settings form. Make sure to call base.OnVisibleChanged though as to not screw up any potential observers of the event (and anything else the base class may do inside of that method.)
FormShown event - raised only once when form is displayed first time.
OnPaint / OnActivate - every time form is activated, but these events raised even when you switch with other application, which probably you don't want to do.
If you are changing form visbility, then you can use OnVisibleChanged
If you are minimizing the form, you can use OnSizeChanged / OnLocationChanged event.
If none suits you, make a public property and set false when form is closed / hidded, and set true before showing it. OnActivate, use this property to do your task.
Maybe use VisibleChanged event.
Override OnShown() event in your form this will raise only once after the form is opened
The disadvantage of OnVisibleChanged is it will also get raised when the form is closed.
On Paint , On Activate and OnEnter will raise before form is shown to the user.

C# WPF: webbrowser modal dialog close from win32 app

I am curious as to if this is even possible, but basically what I need is when opening a System.Windows.Forms.WebBrowser Modal Dialog from a WPF application I need to catch a value passed back from the page to WPF and keep a window up or close it based on the value that I return. Is this even possible or am I going to have to about in a different way?
Thanks, Andrew
surely is possible, just create the dialog, register an event handler for a custom event you have created in your second form and show it as modal, then inside your form you do what you need to do and when something happens you fire the event the main form has registered to, in the custom EventArgs class used in your event you can pass the value main form needs to get. from the main form you check the value and you do nothing or close the popup....

Web browser stealing focus

i have two winforms loaded simultaneously
first form with web browser and second form having just controls
second form always on top of second form, second form doesn't receive any mouse events while first form loaded, after closing first form, second form receive all mouse events.
how do i solve this issue
Please post the code you're using to show for your second form, although it should be something like this:
Form2 myForm2 = new Form2();
Form2.Show(this);
It's not very likely that your WebBrowser is causing this.

Categories

Resources