在Windows窗体文本框中显示字符串

时间:2014-02-19 15:31:26

标签: c# winforms textbox console

我有一个名为“ e.RequestMessage.Text ”的字符串。在类中,我要显示的字符串在窗体中的文本框中具有值。

我的项目包含一个在控制台中显示我的字符串的类。所以我想在Windows窗体的文本框中显示这个刺痛。我为它添加了一个窗体(该类在前面运行)

我该如何做到这一点?

using System;
using Eneter.Messaging.DataProcessing.Serializing;
using Eneter.Messaging.EndPoints.TypedMessages;
using Eneter.Messaging.MessagingSystems.MessagingSystemBase;
using Eneter.Messaging.MessagingSystems.TcpMessagingSystem;
using Eneter.ProtoBuf;
using message.declarations;

namespace ServiceExample
{
    class Program
    {
        private static IDuplexTypedMessageReceiver<MyResponse, MyRequest> myReceiver;

        static void Main(string[] args)
        {
            // Instantiate Protocol Buffer based serializer.
            ISerializer aSerializer = new ProtoBufSerializer();

            // Create message receiver receiving 'MyRequest' and receiving 'MyResponse'.
            // The receiver will use Protocol Buffers to serialize/deserialize messages. 
            IDuplexTypedMessagesFactory aReceiverFactory = new DuplexTypedMessagesFactory(aSerializer);
            myReceiver = aReceiverFactory.CreateDuplexTypedMessageReceiver<MyResponse, MyRequest>();

            // Subscribe to handle messages.
            myReceiver.MessageReceived += OnMessageReceived;

            // Create TCP messaging.
            IMessagingSystemFactory aMessaging = new TcpMessagingSystemFactory();

            IDuplexInputChannel anInputChannel
                = aMessaging.CreateDuplexInputChannel("tcp://127.0.0.1:8060/");

            // Attach the input channel and start to listen to messages.
            myReceiver.AttachDuplexInputChannel(anInputChannel);

            Console.WriteLine("The service is running. To stop press enter.");
            Console.ReadLine();

            // Detach the input channel and stop listening.
            // It releases the thread listening to messages.
            myReceiver.DetachDuplexInputChannel();
        }

        // It is called when a message is received.
        private static void OnMessageReceived(object sender, TypedRequestReceivedEventArgs<MyRequest> e)
        {
            Console.WriteLine("Received: " + **e.RequestMessage.Text**);

            // Create the response message.
            MyResponse aResponse = new MyResponse();
            aResponse.Length = e.RequestMessage.Text.Length;

            // Send the response message back to the client.
            myReceiver.SendResponseMessage(e.ResponseReceiverId, aResponse);
        }
    }
}

Windows表单代码:

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;

namespace ServiceExample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

3 个答案:

答案 0 :(得分:0)

你在哪里构建表格?

您可以做的一件事是为表单创建一个自定义构造函数,该构造函数接收String并将e.RequestMessage.Text传递给它。

类似的东西:

public Form1(String messageText)
{   
    InitializeComponent();
    aTextBox.Text = messageText;
} 

然后在ServiceExample

Form1 form1 = new Form1(e.RequestMessage.Text);

答案 1 :(得分:0)

您是否尝试过更新文本框的值作为活动的一部分?这将完成:

    public void OnMessageReceived(params)
    {
        yourTextBox.Text = e.RequestMessage.Text;
    }

除非我误解了你的问题,否则这应该符合你的期望。

答案 2 :(得分:0)

private void button1_Click(object sender, EventArgs e)
    {
        sum = x + y;
        MessageBox.Show("Ans=" + sum);
    }

    private void textBox3_TextChanged(object sender, EventArgs e)
    {
        string myString = sum.ToString();
        textBox3.Text = myString;
    }