CFanatic

Go Back   CFanatic > Programming > C# Programming

Join CFanatic Forum Now

show/create form from variable topic posted under C# Programming which is a part of Programming category in CFanatic Forum
Reply
 
Thread Tools Display Modes
  #1  
Old 09-07-2010, 07:32 PM
Junior Member
 
Join Date: Sep 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Rhuntsman21 is on a distinguished road
| More
show/create form from variable

I am still learning C# so I am sorry if I do not use the correct terminology.

Here is what I am trying to do. I have the following code that I use to check to see if an instance of a form already exists, and if it does show it. Otherwise it creates the form and then shows it.
Code:
 private void btnAdd_Click(object sender, EventArgs e)
        {
            if (chbDetail.Checked == true)
            {
                bool exists = false;

                foreach (Form f in Application.OpenForms)
                {
                    if (object.ReferenceEquals(f.GetType(), typeof(Ingredient)))
                    {
                        exists = true;
                        break;
                    }
                }

                if (exists)
                {
                    Ingredient ingredient = (Ingredient)Application.OpenForms["Ingredient"];
                    ingredient.Show();
                }
                else
                {
                    Ingredient ingredient = new Ingredient();
                    ingredient.Show();
                }
            }
        }
The problem is that I need to do this with multiple forms in different parts of my program, so I wanted to create a class that I could just pass a variable into and have it open any form I wanted. I thought about using a switch but then I am still writing the code out multiple times. Any ideas would be appreciated.

Last edited by shabbir; 09-07-2010 at 10:23 PM. Reason: Code Blocks
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using variable length variable (bitset) myk45 C++ Programming 0 09-02-2010 01:37 PM
Show in same window. skypie MFC Programming 1 12-02-2009 04:53 AM
Show Updated Data In Listview bdtiger C# Programming 0 01-23-2009 02:33 PM
Exiting a form and gooding to the next by means of a button hamhallan C# Programming 1 04-10-2008 09:19 AM
Exiting a form and going to the next using a button hamhallan C# Programming 0 04-09-2008 11:18 AM

 

Advertisement

CFanatic Search
Custom Search

Advertisement

All times are GMT -5. The time now is 03:59 PM.



Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO ©2010, Crawlability, Inc.
Cfanatic.com is a premier member of the IDG TechNetwork. For advertising opportunities contact here
Get Paid for Working on Projects Matching Your Expertise at Go4Expert's Jobs Board