Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am working on windows form application and i have one form Reports with 3 tabs. now when i click a Menu Item on another form, i want form one to be opened with respective tab.


C#
private void purchaseToolStripMenuItem_Click(object sender, Event Args e)
        {
            Reports report = new Reports();
            report.Show();
        }


Please help
Posted
Comments
BillWoodruff 7-Feb-14 21:54pm    
For questions like this it's valuable if you describe which is the application's "Main Form", and how any "Secondary" Forms are created. If the Form with the Menu creates the Form with the TabControl, that may lead to a different solution than if the Form with the TabControl creates the Form with the Menu. Or, if both TabControl and Menu Forms are created by another Form, that's valuable to know.

The Form that creates the other Forms has the opportunity to "inject" references into the Forms it creates; those references could include a reference to a Property, a Control, a Method, or the creating Form as instance of an Interface shared with the Forms it creates.

Set up an event in the form with the menu, and handle it in the other form. The handler can display the correct tab: Transferring information between two forms, Part 2: Child to Parent[^]
 
Share this answer
 
Comments
Garth J Lancaster 7-Feb-14 17:41pm    
nice answer and set of articles - the one that almost always gets me is between controls on different tab pages, because for some reason I think the tab pages are separate forms - when I clear my brain of that thought , life gets better :-)
Sergey Alexandrovich Kryukov 7-Feb-14 19:15pm    
Look, the answer is misleading to some extent, as well as the referenced articles. Effectively there is no such thing as "child" and "parent" relationships between forms. Your article is based on messing up some concepts. Even if it works, I bet none of the forms are children of others. (Unless TopLevel is set to false, which would make no sense, even though it would prevent the exception thrown when you try to set a parent (System.Windows.Control.Parent) to a form. Forms could be main or not main, owner and owned form (which is not really related to "main" or not), but not parent and child.

Besides, any idea based on asymmetric roles of two collaborating form is wrong. Any reasonable technique would work in exact same way. The difference is just one: which form is given some access to some other form. Everything else is irrelevant: direction of information transfer, relationships between any control, time, etc...

—SA
Member 9764132 8-Feb-14 6:30am    
OriginalGriff thanks for the answer. i tried doing it ut couldnt make. could u please give example
OriginalGriff 8-Feb-14 6:36am    
The link contains examples - what part of them didn't make sense?
What did you try exactly?
This is all reduced to the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

Please also see other solutions in this discussion. If the application is simple enough, the solution could be as simple as declaring of some internal property in one form and passing a reference to the instance of one form to the instance of another form. For more complex projects, such violation of strictly encapsulated style and loose coupling could add up the the accidental complexity of the code and invite mistakes, so the well-encapsulated solution would be preferable.

Please see also:
http://en.wikipedia.org/wiki/Accidental_complexity[^],
http://en.wikipedia.org/wiki/Loose_coupling[^].

—SA
 
Share this answer
 

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