如何在按钮单击时从另一个表单打开一个表单上的特定选项卡

问题描述:

我正在使用Windows窗体应用程序,我有一个窗体报告有3个选项卡。现在,当我单击另一个表单上的菜单项时,我希望使用相应的选项卡打开表单一。





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.


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





请帮助



Please help

设置一个带有菜单的表单中的事件,并以其他形式处理它。处理程序可以显示正确的选项卡:在两个表单之间传输信息,第2部分:孩子到父母 [ ^ ]
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[^]


这一切都简化为关于表单协作的热门问题。最强大的解决方案是在表单类中实现适当的接口,并传递接口引用而不是引用Form的整个实例。有关更多详细信息,请参阅我以前的解决方案:如何以两种形式复制列表框之间的所有项目 [ ^ ]。



另请参阅此处的其他解决方案讨论。如果应用程序足够简单,解决方案就像在一个表单中声明一些 internal 属性并将对一个表单的实例的引用传递给另一个表单的实例一样简单形成。对于更复杂的项目,这种违反严格封装的样式和松散耦合可能会增加代码的意外复杂性并引发错误,因此封装良好的解决方案将是优惠。



另请参阅:

http://en.wikipedia.org/wiki/Accidental_complexity [ ^ ],

http://en.wikipedia.org/wiki/Loose_coupling [ ^ ]。



-SA
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