如何以MDI的子格式(使用功能区)访问变量? [英] How do I access variables in a child form of MDI (with ribbon)?

查看:87
本文介绍了如何以MDI的子格式(使用功能区)访问变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试访问子表单中的变量。我正在从一个多形式的WinForm程序转换为带有功能区的MDI。子表单中的很多代码是相同的,我想编写尽可能多的父表单。我可以访问和修改子项中的控件,但无法弄清楚如何访问和更改变量的方法或找到任何提示。



我的子表单包含一个从List< t>填充的ListBox。在更改要显示的数据时,我通常会更改List< t>。并将ListBox的数据源设置为指向列表。



我尝试过:



我已尝试将它们公开,并尝试使用访问器。



我使用的解决方法是使用子表单控件(标签),然后更改它。然后,子控件xxChanged属性将执行相应的操作。唯一的问题是代码仍在孩子身上。



我可以解决的子格式中是否存在变量(如控件中)? div class =h2_lin>解决方案

谷歌搜索是你的朋友......快速搜索突出了许多可能对你有帮助的结果。你有没有尝试过任何研究?


我认为你正在以错误的方式解决这个问题。



如果你想重用使用从集合或数据源填充列表控件的代码,将该功能抽象到新助手类(例如ListViewHelper)中的方法中,如下所示:

 public static PopulateListView< br /> 
(ListView listView,< br />
IEnumerable< MyDataObject> dataObjects)

并在各种 Form <中调用该方法/ code> s。



/ ravi


虽然我没有找到一种方法来访问孩子的变量直接形成,我确实找到了一种从功能区执行子窗体中的代码的方法。



1.在子表格上创建一个标签



2.公开(或保护)



3.为标签添加TextChanged事件



4.查找并更改标签文本功能区。


I'm trying to access the variables in a child form. I am transitioning from a multi-form WinForm program to a MDI with a ribbon. A lot of the code in the child forms is the same and I would like to code as much is the parent form. I can access and modify controls in the child, but can't figure out how, or find any hints as to how to access and change the variables.

My child forms include a ListBox that is populated from a List<t>. In changing the data to display I normally change the List<t> and the set the ListBox's Datasource to point to the list.

What I have tried:

I've tried making them public, and have tried to use accessors.

The workaround that I am using is to use a child form control (label), and change that. The child control xxChanged property then takes the appropriate action. The only thing is the code is still in the child.

Isn't there a collection (like in Controls) of variables in the child form I can address?

解决方案

Google Search is your friend... A quick search highlights many possible results that could help you. Have you tried doing any research?


I think you're approaching this problem the wrong way.

If you want to re-use code that populates a list control from a collection or data source, abstract that functionality into a method in a new helper class (e.g. ListViewHelper) like so:

public static PopulateListView<br />
    (ListView listView,<br />
     IEnumerable<MyDataObject> dataObjects)

and call that method in your various Forms.

/ravi


While I did not find a way to access variables in a child form directly, I did figure out a way to execute code in the child form from the ribbon.

1. Create a label on the child form

2. Make it public (or protected)

3. Add a TextChanged event for the label

4. Find and change the label text from the ribbon.


这篇关于如何以MDI的子格式(使用功能区)访问变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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