Click here to Skip to main content
15,889,595 members
Please Sign up or sign in to vote.
1.20/5 (3 votes)
See more:
I to open a Window Form, from a UserControl.. is it possible any ways... i tried but it is not listing me any of the forms...!

eg:
1) I have created a userControl1
2) I have a windowForm1
3) NOW, I have to open a windowForm1 from userControl1's textbox keyDown event.

How can i do that...?

Please HELP...!
Posted
Updated 24-Oct-19 14:56pm
Comments
BillWoodruff 20-Feb-12 19:15pm    
Sri Ajay, if you respond in detail to the questions in the comment below, I will take the time to post what I think is a solution, and that will also give the other two people who posted solutions already the chance to revise their answers.

imho, this is a good example of premature answers to a question that needs clarification.

First, re the new Form you will create: is that going to be an instance of a pre-defined Form that you have already designed, created, possibly created publicly accessible features of, or just a plain generic new Form ?

Why are you using some KeyDown event in a TextBox on a UserControl in a Form to create a new Form ? This is a very strange design, and it would help to know your exact reasons for doing this (because it's a homework requirement ? because it's absolutely required by the nature of your project ... ?).

And, do you wish to create and show a new Form only once, or any time your particular Key press is performed by the user. If you want to allow the creation of multiple Forms: why ?

You need to clarify exactly what happens in the KeyDown event of the UserControl TextBox that creates of an instance of a new Form, and then calls its Show method to make it visible.

Surely you do not mean that you intend to create a new Form on any KeyDown of any Key ? In that case: imagine your application running, and the user types "whoops a daisy:" do you really want to create 14 new Forms :)

Do you realize that the new Form(s) you create by some user-action in the UserControl will have their 'Parent property set to "null:" which means they will be totally independent of both the UserControl, and the Form the UserControl is sited upon : unless:

As in most real-world applications, your new Form is going to need to exchange data or interact with either the UserControl, or a Main Form, or both. Then you will need to store a reference to any Form, or Forms, you create in the UserControl, at the moment you create them. Do you have any strategy for this ? This is where we need to know what the purpose of the Form created in the UserControl is: and how it is used.

One can guess what you mean, here, is that you need to recognize some specific Key press, possibly in combination with the modifier Keys (alt, shift, control), and only then create and show a new Form in response to that specific Key combination.

For your example use (roughly) the following :

C#
//Check what the exact arguments for this method should be but you get the idea
void OnKeyDown(object sender, EventArgs e)
{
    //Create an instance of your form to open
    windowForm1 TheForm = new windowForm1();
    //Now show it
    TheForm.Show(); //(alternatively TheForm.ShowDialog(); depending on your desired result
    //Your form should now be showing
}


This is all you should have to do to open a new form from any event. Be very careful though - this code will produce a massive number of windows open if a user just holds down the enter key say... I would suggest better code would be:

C#
//Create an instance of your form to open - outside the event so that it can be reused.
windowForm1 TheForm = new windowForm1();
//Store whether the form has already been shown
bool TheForm_Shown = false;
//Check what the exact arguments for this method should be but you get the idea
void OnKeyDown(object sender, EventArgs e)
{
    //Check to see if the form is already open or not
    if(TheForm_Shown)
    {
        TheForm.Close();
    }
    //Set the check to true as the form is now showing - set it before so that even if showing the form fails there isn't suddenly a mass of errors! 
    TheForm_Shown = true;
    //Now show it
    TheForm.Show(); //(alternatively TheForm.ShowDialog(); depending on your desired result
    //Your form should now be showing
}


Hope this helps,
Ed
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 20-Feb-12 13:21pm    
Sure, a 5.
--SA
BillWoodruff 20-Feb-12 20:00pm    
Reason for my vote of #2:

0. See my comments to SAK above on why giving any answer to this question without clarification from the OP is probably useless.

1. +1 because at least you do show the user some code.

2. when you 'Close the Form in your second example, if it is already 'Shown: you destroy it: you will have to re-create it using 'New to show it again.

Your code will crash the second time the user presses a key because TheForm will be null.

You can fix this by using 'Hide on the Form rather than 'Close, but this is still bad design.
Ed Nutting 21-Feb-12 2:14am    
Sorry for my omission of the "new" line - I had meant to put it in. I have always found closing and recreating a form is better - it guarantees the form is reset regardless of what state the user might have gotten it into (even if theoretically and after testing there wasn't anything wrong with it).
BillWoodruff 21-Feb-12 2:34am    
Actually, your code will will work if you just change using the 'Close method to 'Hide. If I see code I know will crash, I just can't help commenting on it: but I do note how much easier it is for me to see things that will crash in other peoples' code than my own :)

Whether or not it's better to re-create a Form: well, I think that depends on the context of the solution and its requirements. I balk at any suggestion of a "one-size-fits-all" position.

There have been many times I have hidden Forms, such as configuration Forms: because I wanted to retain the user's content in those Forms when they were made visible again, so the user could then edit only the content they wanted to change.

But, my main focus here is to point out that, in this case, our lack of information from the OP really limits how effective an answer we can give.

That's why I use this question, and its answers, as an example here:

http://www.codeproject.com/Messages/4162340/Perfect-example-of-the-QA-problem-I-now-call-Vague.aspx

Of what I, and many other people on CP, perceive as one of the structural problems with QA as it is now.

When you have some of the most brilliant people on CP, like Pete O'Hanlon, refusing to take part in QA any more because of what I call the "vague question followed by premature or digressive answer" syndrome: well, "Houston, we have a problem."

Please see the Site Bugs and Suggestions forum for many discussions we've had about this, and other issues. You can be sure that Chris Maunder, and the CP staff, are definitely aware of the problems in QA and doing their best to plan to ameliorate them in the future.

best, Bill
Sergey Alexandrovich Kryukov 21-Feb-12 5:16am    
Bill, I commented on this comment, too. In particular, found one mistake in you note, please see below.
--SA
There is no such verb as "open" a form. You just create it with a constructor and then show it with System.Windows.Forms.Form.Show, http://msdn.microsoft.com/en-us/library/system.windows.forms.form.aspx[^], http://msdn.microsoft.com/en-us/library/system.windows.forms.control.show.aspx[^].

To use your user control on some other form, you need to add to your user control and expose some event like "TextBoxKeyDown", because exposing the reference to the text box itself would be a bad practice; and hard-coding the event handler of your text box event KeyDown internally in the code of the user control would be even worse. If you handle the event outside the user control, you do it some other context where you may already have an instance of the form to be shown or where you can create one.

For better understanding, please read about loose coupling: http://en.wikipedia.org/wiki/Loose_coupling[^].

Generally, showing a form on a click in a text box looks like an ill UI design.

—SA
 
Share this answer
 
v3
Comments
Ed Nutting 20-Feb-12 13:11pm    
Good comments and links, my 5+ :)
Sergey Alexandrovich Kryukov 20-Feb-12 13:21pm    
Thank you, Edward.
--SA
BillWoodruff 20-Feb-12 19:33pm    
My reason for voting this answer #1:

0. unnecessary pedantry and digression: the user doesn't really need a lecture on why they should not use the word 'open:' you'll even find that term in some .NET books published by MS Press.

1. vague: "just create it with a constructor:" not vague: you create a new Instance of a Windows Form Object using 'New, which is a standard Constructor for Objects.

2. vague: "To use your user control on some other form, you need to add to your user control:" not vague: "to add an Instance of a UserControl to a Form, you can either add it at design-time from the ToolBox (in which it will appear when it is compiled), or add it at runtime by creating an Instance of it using 'New and then calling the 'Add method on the Forms ControlCollection, passing the Instance of the UserControl as the parameter to the 'Add method.

3. off-topic speculation: the OP clearly states that he wishes to create a new Form in response to a KeyDown in a UserControl: you go off on a tangent here that ignores the question.

In this case we do not know enough about the user's intention, or design, to speculate about such topics as "loose coupling." Which is why you should have asked the user the kind of questions I ask him in my comment before going on an all-too-typical, for you, digressive and vague answer.

By the way, I agree with you that, in general, the idea of a key-combination in a TextBox as a trigger for creating a Form is bad design, unless there is some exceptional reason here. But that is just more speculation.
Sergey Alexandrovich Kryukov 20-Feb-12 20:05pm    
What happened to you, Bill?

I don't know if I really need to reply... Well, let me try:

0. You say this like this is something bad. :-)

1. Not vague because it's absolutely irrelevant what constructor to use; any instance constructor providing instance returns an instance; this is all what matters; besides, "using New, which is a standard Constructor for Objects" is gibberish: "new" is not a constructor, and there is no such thing as a standard constructor; I'm asking again: what happened to you, Bill? I thought you knew programming a bit; look, my advice is: don't expose your shame, remove it, and I will gladly remove my comment.

2. You just did not get it. I explained what event should be added to a user control class, and you are talking about adding a user control member to a form; I assumed that OP knows why creating a user control; one of its purposes is to be able to be added by a Designer;

3. Let me decide on the scope of my answer; sometimes I tell a whole anecdote on the topic; I am providing some help to an enquirer, not answering exact question because I think this is much more valuable.

With "loose coupling" you just missed the point because you missed the point in your item #2. There are two options: 1) to handle a click with opening of some window in the event handler of TextBox.KeyDown; 2) create some event of the user control, and invoke this event (after a check for null) from the event handler of TextBox.KeyDown. Case #1 is too tight coupling making the control less universal and expanding its access scope; #2 is loose coupling making handling of the event more flexible.

Maybe I wasn't clear enough, but in this comment I've explained more of it.

Best wishes,
--SA
BillWoodruff 21-Feb-12 1:10am    
Hi SAK, l

Let me note that I never used the word "bad" in my comments to you. My criticisms are quite specific. And, the issue of this being a "vague question" has far greater "weight" in my mind than anything else.

If I had wanted to pass some "summary judgement" on your solution (which I did not), I would have used language like: "not worthy of your intellect and abilities, as demonstrated on QA quite often." :)

Feel free to "skewer" me, here, on "Site Bugs and Suggestions," if you wish:

http://www.codeproject.com/Messages/4162340/Perfect-example-of-the-QA-problem-I-now-call-Vague.aspx

Where I have used this question as an example of what I see as one of the key problems on QA today.

best, Bill

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900