调用控件(等待表单创建控件) [英] Invoking Controls (waiting on form to create control)

查看:71
本文介绍了调用控件(等待表单创建控件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



好吧,多亏了约翰·西蒙斯(John Simmons),我有点到位了.而不是根本不起作用,现在当我尝试从服务器发送内容时,它给了我这个错误.

在创建窗口句柄之前,无法在控件上调用Invoke或BeginInvoke."

我猜想这与表单在调用richTextBox之前没有完全加载有关,但是我如何才能让它在继续加载表单之前等待表单加载Windows句柄?

在此先谢谢您.



Okay, thanks to John Simmons I kinda sorta got somewhere. Instead of it simply not working, now it gives me this error when I try to send something from the server.

"Invoke or BeginInvoke cannot be called on a control until the window handle has been created."

I guess this has something to do with the form not being completely loaded before it invokes the richTextBox, but how can I make it wait for the form to load the windows handle before proceeding?

Thanks in advance.

推荐答案

本文 [< ^ ]应该会帮助您.

:)
This article[^] should help you out then.

:)


您的Invoke实现是正确的,所以问题一定在其他地方.

您是否在调试器中检查过此内容?

control.Text = text;行是否在SetText方法中执行?

在调用SetText( rtbChat, content );时,参数是否包含正确的值?

尼克
Your Invoke implementation is correct, so the problem must be somewhere else.

Have you checked this in the debugger?

Is the line control.Text = text; being executed in the SetText method?

In your call to SetText( rtbChat, content ); do the parameters hold correct values?

Nick


我可能不会像你做的那样调用自己.

并且,由于您*知道*所涉及的控件的实际类型,为什么不将该类型传递给委托或仅传递给文本(因为您可以放心地假定实际控件本身存在)?

为了方便起见,您正在添加晦涩感,结果使图片变得混乱.

I probably wouldn''t invoke myself like you''re doing.

And, since you *know* the actual type of control in question, why not pass that type to the delegate, or just the text (since you can safely assume that the actual control itself exists)?

You''re adding obscurity for the (intended) sake of convenience, and muddying the picture as a result.

public delegate void DelegateTextSetter;
public DelegateTextSetter TextSetter = new DelegateTextSetter(SetText);

public void SetText(string text)
{
    myRichTextControl.Text = text;
}

public void OnReceive(IAsyncResult ar)
{
    String content = String.Empty;
    StateObject state = (StateObject)ar.AsyncState;
    Socket handler = state.workSocket;
    int bytesRead;
    if (handler.Connected)
    {
        // Read data from the client socket.
        try
        {
            bytesRead = handler.EndReceive(ar);
            if (bytesRead > 0)
            {
                state.sb.Remove(0, state.sb.Length);
                state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
                // Display Text in Rich Text Box
                content = state.sb.ToString();

                Invoke(TextSetter, content);

                handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(OnReceive), state);
            }
        }
        catch (SocketException socketException)
        {
            if (socketException.ErrorCode == 10054 || ((socketException.ErrorCode != 10004) && (socketException.ErrorCode != 10053)))
            {
                handler.Close();
            }
        }
    }
}


这篇关于调用控件(等待表单创建控件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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