从另一个对象访问Form组件会抛出“System.NullReferenceException未处理”

时间:2011-04-14 15:24:16

标签: c#

我正在尝试从WCF对象中修改属于Form2的TextBox。

namespace server2
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private ServiceHost duplex;

        private void Form2_Load(object sender, EventArgs e)     /// once the form loads, create and open a new ServiceEndpoint.
        {
            duplex = new ServiceHost(typeof(ServerClass));
            duplex.AddServiceEndpoint(typeof(IfaceClient2Server), new NetTcpBinding(), "net.tcp://localhost:9080/service");
            duplex.Open();
            this.Text = "SERVER *on-line*";
        }
    }


    class ServerClass : IfaceClient2Server
    {


        IfaceServer2Client callback;

        public ServerClass()
        {
            callback = OperationContext.Current.GetCallbackChannel<IfaceServer2Client>();

        }

        public void StartConnection(string name)
        {
            var myForm = Form.ActiveForm as Form2;
            myForm.textBox1.Text = "Hello world!";  /// <- this one trows “System.NullReferenceException was unhandled”
                                                    /// unless Form2 is selected when this fires.

            callback.Message_Server2Client("Welcome, " + name );
        }


        public void Message_Cleint2Server(string msg)
        {
        }

        public void Message2Client(string msg)
        {
        }

    }




    [ServiceContract(Namespace = "server", CallbackContract = typeof(IfaceServer2Client), SessionMode = SessionMode.Required)]


    public interface IfaceClient2Server           ///// what comes from the client to the server.
    {
        [OperationContract(IsOneWay = true)]
        void StartConnection(string clientName);

        [OperationContract(IsOneWay = true)]
        void Message_Cleint2Server(string msg);
    }


    public interface IfaceServer2Client          ///// what goes from the sertver, to the client.
    {
        [OperationContract(IsOneWay = true)]
        void AcceptConnection();

        [OperationContract(IsOneWay = true)]
        void RejectConnection();

        [OperationContract(IsOneWay = true)]
        void Message_Server2Client(string msg);
    }

}

然而“myForm.textBox1.Text =”Hello world!“;”行抛出System.NullReferenceException未处理“...

任何想法,谢谢!

2 个答案:

答案 0 :(得分:2)

myForm可能不属于Form2类型,或者可能不包含textBox1字段。确保检查这两种情况是否为空。

答案 1 :(得分:1)

正如初始问题的评论中所讨论的那样,当您想要的表单不活动时,问题是您指的是ActiveForm。每当尝试使用as关键字进行投射时,如果投射无效,结果将为null。由于您抓取了无法转换为Form2的表单(因为它是另一种形式),因此您正确地收到了空引用异常。

假设您已在Form2上强制执行单例规则而您尚未使用表单的名称,则可以通过Application.OpenForms集合访问它,如下所示:

(Form2)Application.OpenForms["Form2"];

在您的代码示例中,可能如下所示:

public void StartConnection(string name)
{
    //var myForm = Form.ActiveForm as Form2;
    var myForm = Application.OpenForms["Form2"] as Form2;
    myForm.textBox1.Text = "Hello world!";  /// <- this one trows “System.NullReferenceException was unhandled”
                                            /// unless Form2 is selected when this fires.

    callback.Message_Server2Client("Welcome, " + name );
}

那就是说,我认为我不负责将表单控件修改为WCF服务。我很快就会消耗我的表单中服务触发的事件。