Click here to Skip to main content
15,887,267 members
Home / Discussions / C#
   

C#

 
QuestionGraphicsPath.AddString Pin
xstoneheartx19-Aug-09 11:42
xstoneheartx19-Aug-09 11:42 
Questionc# text to wav volume Pin
mikeyman419-Aug-09 11:32
mikeyman419-Aug-09 11:32 
AnswerRe: c# text to wav volume Pin
Christian Graus19-Aug-09 15:38
protectorChristian Graus19-Aug-09 15:38 
QuestionConvert GlyphIndex to Unicode Pin
xstoneheartx19-Aug-09 11:30
xstoneheartx19-Aug-09 11:30 
QuestionModeless active window/class - Cannot access a disposed object. Pin
Natural_Demon19-Aug-09 9:05
Natural_Demon19-Aug-09 9:05 
AnswerRe: Modeless active window/class - Cannot access a disposed object. Pin
Christian Graus19-Aug-09 11:04
protectorChristian Graus19-Aug-09 11:04 
GeneralRe: Modeless active window/class - Cannot access a disposed object. Pin
Natural_Demon19-Aug-09 12:36
Natural_Demon19-Aug-09 12:36 
GeneralRe: Modeless active window/class - Cannot access a disposed object. Pin
Natural_Demon19-Aug-09 13:00
Natural_Demon19-Aug-09 13:00 
i found 1 solution

private void ModalesWindow2_FormClosing(object sender, FormClosingEventArgs e)
{
    OnClosingEventHandler();
    e.Cancel = true; // <--- surpresses the cancel event
    this.Hide();
}

works flawles.
: )

but now i like to know the second answer.
how to work around 'Cannot access a disposed object'

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Modales_window
{
    public partial class Form1 : Form
    {

        Form2 mw2 = new Form2();

        // Form1 form = (Form1)Application.OpenForms[0];
        // Form2 form2 = (Form2)Application.OpenForms[1];

        public Form1()
        {
            mw2.closingeventhandler += new Form2.ClosingEventHandler(mw2_closingeventhandler);
            InitializeComponent();
        }
        void mw2_closingeventhandler()
        {
            //mw2.Dispose();
            //mw2.Hide();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Cannot access a disposed object.
            // Object name: 'Form2'.
            if (!mw2.Created)
            {
                mw2.Show();
            }
            else
            {
                //MessageBox.Show("already open", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                if (mw2.Visible == true)
                {
                    mw2.Visible = false;
                }
                else
                {
                    mw2.Visible = true;
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = Application.OpenForms.Count.ToString();
            GetOpenFormTitles();
        }

        private void GetOpenFormTitles()
        {
            Collection<string> formTitles = new Collection<string>();
 
            try 
            {
                foreach (Form f in Application.OpenForms) 
                {
                    // Use a thread-safe method to get all form titles.
                    formTitles.Add(GetFormTitle(f));
                }
            }
            catch (Exception ex) 
            {
                formTitles.Add("Error: " + ex.Message);
            }

            this.listBox1.DataSource = formTitles;
        }
    
        private delegate string GetFormTitleDelegate(Form f);
        private string GetFormTitle(Form f)
        {
            // Check if the form can be accessed from the current thread.
            if (!f.InvokeRequired) 
            {
                // Access the form directly.
                return f.Text;
            }
            else 
            {
                // Marshal to the thread that owns the form. 
                GetFormTitleDelegate del = GetFormTitle;
                object[] param = { f };
                System.IAsyncResult result = f.BeginInvoke(del, param);
                // Give the form's thread a chance process function.
                System.Threading.Thread.Sleep(10);
                // Check the result.
                if (result.IsCompleted) 
                {
                    // Get the function's return value.
                    return "Different thread: " + f.EndInvoke(result).ToString();
                }
                else 
                {
                    return "Unresponsive thread";
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            mw2.Close();
            //mw2.Dispose(); // causes the famous 'Cannot access a disposed object.' again 
        }
    }
}


i added a Third button
mw2.Close(); closes the window, but doesn't free up the used recources
but private void GetOpenFormTitles() still shows the class is somehow active.
Application.OpenForms.Count.ToString(); results in 2

how do i free up the used recources and start all over again?


kind regards

Bad = knowing 2 much

GeneralRe: Modeless active window/class - Cannot access a disposed object. Pin
Christian Graus19-Aug-09 16:00
protectorChristian Graus19-Aug-09 16:00 
GeneralRe: Modeless active window/class - Cannot access a disposed object. Pin
Natural_Demon20-Aug-09 2:48
Natural_Demon20-Aug-09 2:48 
QuestionDynamic Menu Creation Problem Pin
jroberson1019-Aug-09 8:59
jroberson1019-Aug-09 8:59 
AnswerRe: Dynamic Menu Creation Problem Pin
Hristo-Bojilov19-Aug-09 9:37
Hristo-Bojilov19-Aug-09 9:37 
GeneralRe: Dynamic Menu Creation Problem Pin
jroberson1019-Aug-09 9:41
jroberson1019-Aug-09 9:41 
GeneralRe: Dynamic Menu Creation Problem Pin
Hristo-Bojilov19-Aug-09 9:46
Hristo-Bojilov19-Aug-09 9:46 
GeneralRe: Dynamic Menu Creation Problem Pin
jroberson1019-Aug-09 9:50
jroberson1019-Aug-09 9:50 
GeneralRe: Dynamic Menu Creation Problem Pin
Hristo-Bojilov19-Aug-09 9:56
Hristo-Bojilov19-Aug-09 9:56 
GeneralRe: Dynamic Menu Creation Problem Pin
jroberson1019-Aug-09 10:07
jroberson1019-Aug-09 10:07 
GeneralRe: Dynamic Menu Creation Problem Pin
jroberson1020-Aug-09 11:36
jroberson1020-Aug-09 11:36 
GeneralRe: Dynamic Menu Creation Problem Pin
0x3c019-Aug-09 9:47
0x3c019-Aug-09 9:47 
GeneralRe: Dynamic Menu Creation Problem Pin
jroberson1019-Aug-09 9:51
jroberson1019-Aug-09 9:51 
GeneralRe: Dynamic Menu Creation Problem Pin
0x3c019-Aug-09 9:58
0x3c019-Aug-09 9:58 
QuestionInsert function into event at runtime Pin
bonzaiholding19-Aug-09 8:45
bonzaiholding19-Aug-09 8:45 
AnswerRe: Insert function into event at runtime Pin
0x3c019-Aug-09 9:57
0x3c019-Aug-09 9:57 
GeneralRe: Insert function into event at runtime Pin
bonzaiholding19-Aug-09 10:18
bonzaiholding19-Aug-09 10:18 
GeneralRe: Insert function into event at runtime Pin
mustang8619-Aug-09 11:08
mustang8619-Aug-09 11:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.