从无权访问它们的类更新表单元素

时间:2010-09-19 15:14:16

标签: c# visual-studio

我想从通常无法访问它们的类中访问某些表单元素。请允许我说明问题。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Collections;

namespace MyApp {
    public partial class MyApp : Form
    {
        public MyApp()
        {
            InitializeComponent();

            // Code
        }

        public void updateLabel(string message)
        {
            myLabel.Text = message;
        }
    }

    public class NewClass
    {
        public NewClass()
        {
            // I want to call updateLabel("My message") here, but 'MyApp.updateLabel("My message");' didn't work even though I made updateLabel public
        }
    }
}

我该如何解决这个问题?我对C#比较陌生,但我有C,PHP,Java和JavaScript方面的经验。我正在使用Visual C#2010 Express。

6 个答案:

答案 0 :(得分:1)

您需要将MyApp类的实例传递给NewClass类 然后,您可以在UpdateLabel实例上调用MyApp,而无需将标签公开。

答案 1 :(得分:0)

由于updateLabel是MyApp中的非静态成员方法,因此您需要在调用其任何实例方法之前创建MyApp实例。

在NewClass ctor中使用以下代码行:

MyApp myapp = new MyApp();
myapp.updateLabel("Hello World");

我假设MyApp类已经被实例化,在这种情况下你必须像SLaks已经提到的那样将引用传递给NewClass(可能是构造函数)。

答案 2 :(得分:0)

可能是下面的技术将有所帮助。它可以使用ActionFunc

[Test]
        public void ActionsTest()
        {
            var parent = new Parent();
            parent.Child.RaiseCallFromParent();
            parent.Child.RaiseCallInParent();
        }

        public class Parent
        {
            private readonly Child _child = new Child();

            public Parent()
            {
                Child.ActionToCallMethodFromParent = methodCalledFromChild;
                Child.ActionToBeCalledInParent += actionCalledInParent;
            }

            public Child Child
            {
                get { return _child; }
            }

            private void actionCalledInParent()
            {
                Console.WriteLine("It is called in parent on child initiative.");
            }


            private void methodCalledFromChild()
            {
                Console.WriteLine("It is called from child");
            }
        }

        public class Child
        {
            public Action ActionToCallMethodFromParent;
            public Action ActionToBeCalledInParent;

            public void RaiseCallFromParent()
            {
                //This works in cases when you need to consume something from Parent but here you cannot take it directly
                if (ActionToCallMethodFromParent != null)
                    ActionToCallMethodFromParent();
            }

            public void RaiseCallInParent()
            {
                //This works like an event
                if (ActionToBeCalledInParent != null)
                    ActionToBeCalledInParent();
            }
        }

答案 3 :(得分:0)

这是我自己提出的解决方案。我将myLabel作为参数传递给需要访问标签的类构造函数,如下所示:

电话:

NewClass newClassObj = new NewClass(myLabel);

班级:

public class NewClass
{
    public NewClass(Label myLabel)
    {
        myLabel.Text = "Hello world!";
    }
}

除非这是一个糟糕的编程习惯,否则我更喜欢这个解决方案。想法?

答案 4 :(得分:0)

最好从你自己的类中引发一个事件然后在表单中捕获它并从那里更新控件,然后你就不会将你的逻辑连接到特定的UI元素。

答案 5 :(得分:0)

public void updatelabel(string message)更改为public static void updatelabel(string message)

然后从new class开始,您可以像myapp.updatelabel(message)一样访问它。

您必须使用 myapp 添加到新课程的顶部。

相关问题