CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Nov 2013
    Location
    Pennsyvania, USA
    Posts
    28

    [solved]Using Multiple Windows Forms in C#

    Hello,

    The question in short is highlighted in blue just above the code window if you'd like to skip the long-winded explanation.

    I'm just learning how to use multiple forms. I've learned a quick trick to get started, but need more details.

    In the code below I instantiate the new form (in this case called Robo_Pass) as an object called Robo_Pass1. The site where I learned how to do this taught me that I need to instantiate the form outside of the Button Code that calls it. The reason for this is because the Button would otherwise continue to create new Robo_Pass forms every time it is clicked. However, this causes other problems. For example, if I then close the Robo_Pass form and try to show it again I get the error message that it no longer exists. So instead of closing the form I end up having to just "hide" it. That works, but it's not what I want. I want to be able to either just hide it, or close it outright and still be able to reopen it fresh again.

    So I would like to move the code that creates the new form object inside the Button Code. But then I need to have the button check first to see whether or not it needs to create a new instance of the form. I know that I can do this by using a variable of my own creation to keep track of whether or not I had previously created the form, but that can be messy. Especially since I would need to know when the Robo_Pass form has been closed.

    I'm wondering if there is a more professional way to test if an object like this has already been instantiated?

    In short, I would like to be able to either "Hide();" the Robot_Pass form keeping it alive (as I'm already doing), or "Close();" it altogether and be able to reopen it fresh (which I currently can't do). The way the code is now I can only "Hide" the Robo_Pass form. If I close it and try to open it again, I get the error message that it no longer exists because once it is closed it needs to be recreated which is outside the scope of the button that opens it.

    Is there some sort of simple statement like "If this object is not already instantiated, then create it, else don't"?


    Code:
    		public MainForm()
    		{
    			InitializeComponent();			
    		}
    		
    		/*
    		 * Create a new instance of the Robo_Pass form
    		 * I would like to move this into the actual button below
    		 * But in order to do that I need to learn how to prevent
    		 * multiple instantiations of it.
    		 */
    		Robo_Pass Robo_Pass1 = new Robo_Pass();
    
    		void Bnt_Robo_PassClick(object sender, EventArgs e)
    		{
    			Robo_Pass1.Show(); // Show the Robot_Pass Form Window
    			Robo_Pass1.TopMost=true; // Make it stay on top.
    		}



    Sorry for the long-winded explanation, but I'm a cyber idiot so please take that into consideration. Thank you.
    Last edited by Leucippus; November 1st, 2015 at 09:18 AM. Reason: To mark as [solved]
    In C# I use SharpDevelop IDE ver 4.4.1 and .NET version 4.0 (OS is Windows 7)
    In Python I use Geany IDE on a Raspberry Pi (OS is Raspbian)
    I'm also learning C and C++
    My main use of programming is for Robotics and AI.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Using Multiple Windows Forms in C#

    You can hide/show a form if you make it non-modal. If you want a model form, then pass in the instance of the parent form when you create it and have the parent track the instance of the child form. Next create an onClose handler inside the child form. When closed, the child form will use the passed in parent instance to close it's own instance.

    At any rate, please answer if you want the child form to be modal or non-modal. We can provide more direction after you answer this.

  3. #3
    Join Date
    Nov 2013
    Location
    Pennsyvania, USA
    Posts
    28

    Re: Using Multiple Windows Forms in C#

    Quote Originally Posted by Arjay View Post
    At any rate, please answer if you want the child form to be modal or non-modal. We can provide more direction after you answer this.
    Ok, please understand that you are conversing with someone who has absolutely no formal training or education in C# or even OOP programming in general. I used to program a bit with Visual Basic 6.0 years ago and did pretty well with it, but even that was all self-learned just by trial and error and digging through code pocket guides.

    Here's a basic outline of what I'm trying to accomplish:

    To begin with this main program is called the "Robot Overseer". That is the name of the MainForm. All I want this this main form to do is to open other forms, each one having a specific purpose. I want all those other forms to be able to remain open as I come back to the main form or "Robot Overseer" form. And I would like to have several of these other forms open simultaneously.

    Here's a graphic screenshot of what I'm trying to build:

    Name:  Overseer.jpg
Views: 181
Size:  26.1 KB

    The Robot Overseer form or window is the main controller. All it contains are the buttons to launch all these other individual applications. Each of these other forms are basically self contained programs that do various things. I would like to be able to have several of these other forms open simultaneously. Opening and closing them as needed.

    The coding in these other open forms will be communicating with a robot via shared text files as well as TCP sockets (although I haven't actually done the socket connections yet). So I want these other forms to continue to be "live" and interacting with the robot even when I might be actually working with a different form.

    I have NO CLUE, how to properly set this all up. I just know that it's not going to work if a form won't allow me to leave until I close it. It has to remain open and active whilst I go on to work with other active forms.

    I don't know if this is the proper way to code things or not. But I definitely don't want all this code on one form. That form would become huge and unmanageable. I'm trying to keep this modular so I don't get lost in all these totally separate programs.

    Although they aren't exactly separate either. Sometimes one form needs to know what's going on in another form. I'm planning on using a "Global Class" to define global data that I need to pass back and forth between these forms.

    As I say, I have no clue how to do this correctly, but this seems like a reasonable approach to me. The "Robot Overseer" is going to contain many different programs, and I'm trying to keep them separate by using separate forms for them.

    Maybe there's a better way to do this by just using separate frames on a single Overseer Form Window? But then how do you keep all the code separate?

    I warned you that I'm an idiot when it comes to OOP programming!

    I like the idea of what I'm trying to do. But I don't know if it's the right way to go about things?
    In C# I use SharpDevelop IDE ver 4.4.1 and .NET version 4.0 (OS is Windows 7)
    In Python I use Geany IDE on a Raspberry Pi (OS is Raspbian)
    I'm also learning C and C++
    My main use of programming is for Robotics and AI.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Using Multiple Windows Forms in C#

    Okay. So you need non-modal forms.

    One last question...

    Do these forms act on the same underlying robot data? In other words, will you only have one robot attached to your program (or is it possible for your program to control more than one robot at the same time)?

    If you can, please answer the questions in 5 lines or less.

  5. #5
    Join Date
    Nov 2013
    Location
    Pennsyvania, USA
    Posts
    28

    Re: Using Multiple Windows Forms in C#

    Quote Originally Posted by Arjay View Post
    If you can, please answer the questions in 5 lines or less.
    Line #1: I hope to be controlling and keeping track of 6 robots. (possibly more down the road) They each have unique data.

    Line #2: Each robot has a Raspberry Pi connected via WiFi to the Robot Overseer which runs on a Windows 7 notebook.

    Line #3: It is my hope and dream that this Overseer program and the robots can someday become fully autonomous making the Windows Form user interface unnecessary in some distant future.

    Line #4: During the development stage (which may last for years) I'll need the user interface.

    Line #5: I'm so excited I can hardly contain myself. I wish I was a programming guru!
    In C# I use SharpDevelop IDE ver 4.4.1 and .NET version 4.0 (OS is Windows 7)
    In Python I use Geany IDE on a Raspberry Pi (OS is Raspbian)
    I'm also learning C and C++
    My main use of programming is for Robotics and AI.

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Using Multiple Windows Forms in C#

    Quote Originally Posted by Leucippus View Post
    Line #1: I hope to be controlling and keeping track of 6 robots. (possibly more down the road) They each have unique data.
    Will the same instance of the program control all 6 robots at the same time? Or will you open an instance of the program that controls robot 1, then open another instance of the program that controls robot 2 and so on.

    This matters and determines the layout of how the data is stored by the program.

  7. #7
    Join Date
    Nov 2013
    Location
    Pennsyvania, USA
    Posts
    28

    Re: Using Multiple Windows Forms in C#

    Each form will interface with all 6 robots. So only one instance of each form needs to be loaded at a time.

    Each form simply handles different type of tasks, but will do so for all 6 robots.

    It might be informative to understand that the robots themselves will be each running their own "AI" programs on their Raspberry Pies. So they will basically be autonomous only looking to this Windows "Overseeing program" for basic instructions on what to do, and to report their progress. The Windows program itself will not be controlling the details of each robot's tasks. It will only be "Overseeing" the big picture making sure the robots know what their tasks are and helping them out if they report failures (or alarming me if the robots fail to report at all).

    So most of the "nitty-gritty" robot control stuff really goes on within the Raspberry Pies on board the robots. And that's all done in Python.
    In C# I use SharpDevelop IDE ver 4.4.1 and .NET version 4.0 (OS is Windows 7)
    In Python I use Geany IDE on a Raspberry Pi (OS is Raspbian)
    I'm also learning C and C++
    My main use of programming is for Robotics and AI.

  8. #8
    Join Date
    Nov 2013
    Location
    Pennsyvania, USA
    Posts
    28

    Re: Using Multiple Windows Forms in C#

    I finally managed to figure out how to do this after much web searching. I can post how I solved this problem if anyone is interested.
    In C# I use SharpDevelop IDE ver 4.4.1 and .NET version 4.0 (OS is Windows 7)
    In Python I use Geany IDE on a Raspberry Pi (OS is Raspbian)
    I'm also learning C and C++
    My main use of programming is for Robotics and AI.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured