以其他形式从mainform.cs调用对象?

时间:2014-07-22 06:17:25

标签: c# winforms namespaces

我想从GraficDisplay(命名空间)调用mainform(mainform.cs)中的任何内容 在另一个(命名空间):GraphLib,但我不能,任何人都会告诉我为什么?我该如何解决这个问题?自项目开始以来,它给了我很多时间,每次我尝试这些错误时都会出现:

我打电话的时候:

mainform.toolstriplabel1.text = "87";

出现:

  

当前上下文中不存在名称'mainform'

如果我这样称呼:

GraficDisplay.MainForm.toolstriplabel1.text = "87";

出现:

  

当前上下文中不存在名称'GraficDisplay'

我的意思是我甚至无法在GraphLib(名称空间)中调用GraficDisplay(名称空间) MainForm还是publicpartial

1 个答案:

答案 0 :(得分:0)

我通常不会在这里关注链接,但是CodeProject是一个相当可靠的源码,所以我看看。

编辑:我很困惑你想要什么。这就是你真正想要的东西:

问题在于从另一种形式或部分形式引用表单或部分表单。这是一个处理库的问题,真的不应该是添加示例应用程序或依赖项等的命名空间。

所以你想要的是松耦合

这是一个使用库对象中的引用和注册方法来填充引用的解决方案。如果您没有注册任何内容,库将正常工作。

这个解决方案可以改变和扩展,但是我会留下它来注册两个对象:一个是Control,例如TextBox;另一个是组件,例如一个ToolStripItem。如果只想引用ToolStripItem,可以省略对Control和RegisterCtl方法的引用。

在帽子的情况下,你可以而且也应该用'Component'代替'ToolStripItem'来使事情变得更加紧密!

首先,您要了解行动的最终消费者PlotterGraphSelectCurvesForm。在这里添加这两个代码块:

public partial class PlotterGraphSelectCurvesForm : Form
{
    private int selectetGraphIndex = -1;
    private PlotterGraphPaneEx gpane = null;

    // block one: a Control reference (if you like!):
    Control myTextCtl;
    public void RegisterCtl(Control ctl) { if (ctl != null) myTextCtl = ctl; }

    // block one: a Component reference:
    Component myTextComp;
    public void RegisterComp(Component comp) { if (comp != null)  myTextComp = comp; }  
    //..

接下来,您可以编写您想要发生的事情,也许是这样:

    void tb_GraphName_TextChanged(object sender, EventArgs e)
    {
        if (selectetGraphIndex >= 0 && selectetGraphIndex < gpane.Sources.Count)
        {
            DataSource src = gpane.Sources[selectetGraphIndex];
            String Text = tb_GraphName.Text;

            // all controls have a Text:
            if (myTextCtl != null) myTextCtl.Text = Text;
            // here you need to know the type:
            if (myTextComp != null) ((ToolStripItem) myTextComp).Text = Text;
            //..   

        }

理论上你现在需要做的就是在Mainform中注册TextBox和/或ToolStripItem。但是,有一个复杂的问题:PlotterGraphSelectCurvesForm没有调用Mainform!相反,它是从UserObject PlotterGraphPaneEx调用的,而public partial class PlotterDisplayEx : UserControl { #region MEMBERS Control myTextCtl; public void RegisterCtl(Control ctl) { if (ctl != null) myTextCtl = ctl; } Component myTextComp; public void RegisterComp(Component comp) { if (comp != null) myTextComp = comp; } //.. 又位于MainForm中。在通过创建依赖关系而不弄乱库的同样精神上,您只需将相同的引用和注册方法添加到此UO中:

    public MainForm()
    {           
        InitializeComponent();

        display.RegisterCtl(aDemoTextBox);
        display.RegisterComp(toolstriplabel1);
        //..

现在你可以在MainForm中实际注册东西..:

    private void selectGraphsToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (GraphPropertiesForm == null)
        {
            GraphPropertiesForm = new PlotterGraphSelectCurvesForm();
            GraphPropertiesForm.RegisterCtl(myTextCtl);
            GraphPropertiesForm.RegisterComp(myTextComp);
        }
    //..

..并在UO中:

{{1}}

现在,当您打开“属性”表单并更改LabelText时,您可以看到“图形”中的文本以及“菜单”和“文本框”中的文本都发生更改。