在FormA中按下以打开FormB [英] Button in FormA to open FormB

查看:81
本文介绍了在FormA中按下以打开FormB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我是C#编程的新手所以这可能是一个愚蠢的问题,但我想知道是否有办法连接两个表格彼此。



我有1个父表单,包含以下代码:

 private void AToolStripMenuItem_Click(object sender, EventArgs e)
{
FormA frm1 = new FormA();
frm1.MdiParent = this;
frm1.Show();
}

private void BToolStripMenuItem_Click(object sender,EventArgs e)
{
FormB frm1 = new FormB();
frm1.MdiParent = this;
frm1.Show();
}





在FormA中我有一个清单:

 private void fillListBox()
{
List< Docent> docenten = Docent.AllDocenten();

listBox1.DataSource = docenten;
listBox1.DisplayMember =AllData;
listBox1.ValueMember =DocentID;
}





FormB有3个按钮:第一个在列表中查找Docent。另外两个用于改变Docent中的数据或删除它。

现在我想从FormA中的列表中选择一个项目并在FormB中使用它来删除或更改它。因为我的程序现在的工作方式,你必须在更改或删除之前插入列表项的名称。

如果插入类似'lkjomkj'的内容,那就太难了列表;)。



谢谢!

解决方案

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



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



另请参阅:

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

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







此外,使用MDI是一个非常糟糕的主意。



确实,谁需要MDI?为什么要折磨自己并吓跑你的用户?

帮自己一个大忙:根本不要使用MDI。没有它,您可以更轻松地实现设计,质量更好。 MDI甚至被微软高度劝阻,事实上,微软将其从WPF中删除并且很难支持它。更重要的是,如果您使用MDI,您将吓跑所有用户。只是不要。请参阅:

http://en.wikipedia.org/wiki/Multiple_document_interface#Disadvantages [ ^ ],

如何在WPF中创建MDI父窗口? [ ^ ]。



我可以解释做什么。请看我过去的答案:

如何在WPF中创建MDI父窗口? [解决方案2 ],

关于在WPF中使用MDI窗口的问题 [ ^ ],

麦当劳给出错误 [ ^ ],

如何设置子窗体最大化,最后一个子窗体最小化 [ ^ ]。



-SA


如果我理解你的需求,你将需要FormB中的公共财产



 public string ItemToEdit {get;组; } 





并说明在FormA中的按钮点击事件和/或列表更改事件,您可以使用您想要的值设置ItemToEdit更改。



当然,如果您希望能够在单击按钮后更改正在编辑的内容,则可能需要在onclick事件之外声明FormB frm1。


Hi,

I am new at programming with C# so this could be rather a stupid question, but I was wondering if there is a way to connect two forms with each other.

I have 1 parent form with this code:

private void AToolStripMenuItem_Click(object sender, EventArgs e)
{
    FormA frm1 = new FormA();
    frm1.MdiParent = this;
    frm1.Show();
}

private void BToolStripMenuItem_Click(object sender, EventArgs e)
{
    FormB frm1 = new FormB();
    frm1.MdiParent = this;
    frm1.Show();
}



In FormA I have a list:

private void fillListBox()
{
    List<Docent> docenten = Docent.AllDocenten();

    listBox1.DataSource = docenten;
    listBox1.DisplayMember = "AllData";
    listBox1.ValueMember = "DocentID";
}



FormB has 3 buttons: The first one looks for the Docent in the list. The other two are used to alter the data in Docent or to delete it.
Now I would like to make it possible to select one item from the list in FormA and use it in FormB to delete or alter it. Because the way my program works right now, you have to insert the name of an item of the list before it can be altered or deleted.
That is rather difficult if you insert something like 'lkjomkj' in the list ;).

Thank you!

解决方案

This is 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[^].

[EDIT]

Besides, it's a really bad idea to use MDI.

Indeed, who needs MDI, ever? Why torturing yourself and scaring off your users?
Do yourself a great favor: do not use MDI at all. You can do much easier to implement design without it, with much better quality. MDI is highly discouraged even by Microsoft, in fact, Microsoft dropped it out of WPF and will hardly support it. More importantly, you will scare off all your users if you use MDI. Just don't. Please see:
http://en.wikipedia.org/wiki/Multiple_document_interface#Disadvantages[^],
How to Create MDI Parent Window in WPF?[^].

I can explain what to do instead. Please see my past answers:
How to Create MDI Parent Window in WPF? [Solution 2],
Question on using MDI windows in WPF[^],
MDIContainer giving error[^],
How to set child forms maximized, last childform minimized[^].

—SA


If I understand what you want, you will need a public property in FormB

public string ItemToEdit { get; set; }



and say on the button click event and/or the list changed event in FormA you can set ItemToEdit with the value you want to change.

Of course you might need to declare the FormB frm1 outside the onclick event if you want to be able to change what you are editing after you click the button.


这篇关于在FormA中按下以打开FormB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆