How to open multiple instances of one Form at the same time?

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

30 Replies - 4502 Views - Last Post: 23 March 2011 - 04:12 PM Rate Topic: -----

#1 bronze  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 08-January 11

How to open multiple instances of one Form at the same time?

Posted 21 March 2011 - 05:28 PM

I'm working on a project, which is essentially MS's Notepad with a few extra features, one being the ability to open all files from a directory, where each file opens in its own window. So far I have
        public void opener(String dir)
        {
            String[] files = Directory.GetFiles(@dir);
            if (files.Length > 0)
            {
                foreach (String s in files)
                {
					try
					{
						MainWindow mw = new MainWindow();
						mn.Show();
                                                // File opening stuff here.
					}
					catch (AccessViolationException err)
					{
						MessageBox.Show("ERROR: " + err.Message);
					}
                }
            }
            else
            {
                MessageBox.Show("There are no files to open!");
            }
        }



However, when I go to run it, It takes forever to do anything, and when it does, it tells me "Win32Exception was unhandled."

Suggestions :wacko:

This post has been edited by bronze: 21 March 2011 - 05:29 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How to open multiple instances of one Form at the same time?

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9094
  • View blog
  • Posts: 34,148
  • Joined: 12-June 08

Re: How to open multiple instances of one Form at the same time?

Posted 21 March 2011 - 05:45 PM

What sort of file is it trying to open? Have you debugged through it? My guess it's something big (like an exe) or something that notepad isn't really designed to open.
Was This Post Helpful? 0
  • +
  • -

#3 bronze  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 08-January 11

Re: How to open multiple instances of one Form at the same time?

Posted 21 March 2011 - 05:47 PM

its a file that has literally 2 characters in it. the file extension is .sun (souped up notedpad.)
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9094
  • View blog
  • Posts: 34,148
  • Joined: 12-June 08

Re: How to open multiple instances of one Form at the same time?

Posted 21 March 2011 - 05:57 PM

Well time to toss a break point in your "// File opening stuff here" and see what is going on! Debug that puppy!
Was This Post Helpful? 1
  • +
  • -

#5 Ionut  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 385
  • View blog
  • Posts: 1,057
  • Joined: 17-July 10

Re: How to open multiple instances of one Form at the same time?

Posted 22 March 2011 - 12:36 AM


MainWindow mw = new MainWindow();
mn.Show();



Here is a typo? Who is mn?
can you post also // File opening stuff here.? At what line does the error raise?
Was This Post Helpful? 0
  • +
  • -

#6 bronze  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 08-January 11

Re: How to open multiple instances of one Form at the same time?

Posted 22 March 2011 - 12:54 PM

View PostIonut, on 22 March 2011 - 02:36 AM, said:


MainWindow mw = new MainWindow();
mn.Show();



Here is a typo? Who is mn?
can you post also // File opening stuff here.? At what line does the error raise?

mn is just a typo. Should be mw//Open stuff here is simply mw.richTextBox1.LoadFile();.

The error comes at the line mw.Show();
Was This Post Helpful? 0
  • +
  • -

#7 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4468
  • View blog
  • Posts: 7,780
  • Joined: 08-June 10

Re: How to open multiple instances of one Form at the same time?

Posted 22 March 2011 - 12:58 PM

Well then, the error is with your MainWindow class! It's never even getting to your file stuff.

You need to view that exception. Check what it's message property is, check if there are any InnerExceptions, and check the StackTrace to see if you can figure out what's happening.

Win32Exceptions are a pain in the ass to debug. Look up some info on them here
http://msdn.microsof...2exception.aspx
Was This Post Helpful? 0
  • +
  • -

#8 bronze  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 08-January 11

Re: How to open multiple instances of one Form at the same time?

Posted 22 March 2011 - 01:05 PM

View PostinsertAlias, on 22 March 2011 - 02:58 PM, said:

Well then, the error is with your MainWindow class! It's never even getting to your file stuff.

You need to view that exception. Check what it's message property is, check if there are any InnerExceptions, and check the StackTrace to see if you can figure out what's happening.

Win32Exceptions are a pain in the ass to debug. Look up some info on them here
http://msdn.microsof...2exception.aspx

Oh, I thought that was just a hemorrhoid :P

Thanks for the resource, and I'll get on it :)
Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4468
  • View blog
  • Posts: 7,780
  • Joined: 08-June 10

Re: How to open multiple instances of one Form at the same time?

Posted 22 March 2011 - 01:11 PM

Tip #1 (and I can't stress this hard enough): Always check the MSDN. A quick google of the component's name should get you to a page with tons of relevant info that's always worth checking out.

The MSDN will define every property, method, and event that a class has, and it'll often provide usage examples for the class and it's most common methods.

Tip #2: Debug. When VS catches an exception, you gain access to a wealth of info. You'll see a box like this:

Posted Image

Most of the links on it are crap, but if you click the "View Detail..." link, you get this:

Posted Image

Which'll show you all the properties of the exception. Hugely useful.

Anyway, if all this fails, post the code to Mainwindow.
Was This Post Helpful? 3
  • +
  • -

#10 bronze  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 08-January 11

Re: How to open multiple instances of one Form at the same time?

Posted 22 March 2011 - 01:53 PM

Thanks for the tips, and I've sorted out what was going on (It was a rather stupid error.)

But anyways, I'm faced with a new challenge. When I use mw.Show();, the form flashes on the screen and exits.

I've also tried mw.ShowDialog(); however when there are multiple files in the directory (which should result in multiple windows of mw appearing) only one appears, and when I exit that form the next one shows! I guess I'm asking how to show multiple instances of one form at a time.
Was This Post Helpful? 0
  • +
  • -

#11 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9094
  • View blog
  • Posts: 34,148
  • Joined: 12-June 08

Re: How to open multiple instances of one Form at the same time?

Posted 22 March 2011 - 02:04 PM

Stop for a minute and think about what you are doing. If you show modal it will stop processing until that declared modal form is shut down or hidden. That explains why when you close one it opens another.

You run a show on your object that you just created... it lives briefly, but once that loop comes back around again it is destroyed and a new object is shown. That's why it flickers.

The problem seems to be with how you are creating the object, right? Maybe create a list of your object time, add on to it where you create your current object, and then show all the forms afterward?
Was This Post Helpful? 0
  • +
  • -

#12 bronze  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 08-January 11

Re: How to open multiple instances of one Form at the same time?

Posted 22 March 2011 - 02:15 PM

ok, I've created List<Form> toBeShown = new List<Form>();, and when I loop through all the strings in files, I add mw to the list. At this point, I'm not sure how to simultaneously open each form in toBeShown
Was This Post Helpful? 0
  • +
  • -

#13 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4468
  • View blog
  • Posts: 7,780
  • Joined: 08-June 10

Re: How to open multiple instances of one Form at the same time?

Posted 22 March 2011 - 02:17 PM

Quote

You run a show on your object that you just created... it lives briefly, but once that loop comes back around again it is destroyed and a new object is shown. That's why it flickers.


That's not necessarily true. Just to test this theory, I tried this:

private void button1_Click(object sender, EventArgs e) {
    List<string> strings = new List<string>() { "one", "two", "three", "four", "five" };
    foreach (string s in strings) {
        Form2 f2 = new Form2 { Text = s };
        f2.Show();
    }
}


And I end up with this:

Posted Image

Which is basically the same as bronze is doing.

One thing we haven't seen: the code for Mainwindow. If there's something in there that's triggering a close, that could be the issue. Also, there could be something in the code that follows those lines (that wasn't included) that calls .Close.

We're missing too much information here, I think.
Was This Post Helpful? 0
  • +
  • -

#14 bronze  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 08-January 11

Re: How to open multiple instances of one Form at the same time?

Posted 22 March 2011 - 02:21 PM

edit by insertAlias: no need to quote large posts directly above yours

Code for MainWindow:

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

namespace WindowsFormsApplication1
{
    public partial class MainWindow : Form
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }
        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            pf pref = new pf();
            pref.Show();
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            NewWindow nn = new NewWindow();
            nw.Show();
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            Open o = new Open();
            o.check();
        }
    }
}


This post has been edited by insertAlias: 22 March 2011 - 02:24 PM

Was This Post Helpful? 0
  • +
  • -

#15 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4468
  • View blog
  • Posts: 7,780
  • Joined: 08-June 10

Re: How to open multiple instances of one Form at the same time?

Posted 22 March 2011 - 02:26 PM

OK, nothing important there (other than the fact that these child forms can spawn forms of their own...that's going to be a nightmare of window management!).

What about in the part you edited out:
// File opening stuff here.


What's the code there?
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3