将信息从IRC类发送到WinForm TextBox

时间:2014-09-24 01:17:22

标签: c# .net winforms

我在让IRC客户端工作时遇到问题。我通常不使用winforms或WPF,我选择了WinForms,因为它更容易使用,我不是新线程,但我不知道它是如何工作的。

我想简单地完成这件事:

  1. 使用" SmartIRC4Net"
  2. 连接到IRC
  3. 通过简单的
  4. 处理IRC文本
  5. 解析图书馆。将所有聊天和其他信息发送到GUI         (的WinForms)
  6. 问题是什么?

    • 全部。它不会绑定到UI或从Program.cs
    • 中创建的IRC类连接

    MainForm.cs:

    public partial class MainForm : Form {
        public MainForm() {
            InitializeComponent();
            BackgroundWorker b = new BackgroundWorker();
            b.DoWork += new DoWorkEventHandler(bgWork1);
            b.RunWorkerAsync();
        }
     } 
     private void bgWork1(object sender, DoWorkEventArgs e) {
         Program irc = new Program();
         irc.Start();
     }
    

    Program.cs:

    [DllImport("kernel32.dll")]
     private static extern void ExitProcess(int a);
    static MainForm Form1;
    public static MainForm form = new MainForm(); // Main Form Controls
    [STAThread]
     public static void Main() {
        Application.EnableVisualStyles();
        //Application.SetCompatibleTextRenderingDefault(true); - Ignoring this due to a start error.
        form.Text = "Test"; // Trying to send text, doesn't work
        form.MessageSend.Text = "Test 2"; // Trying to send text, doesn't work.
        MessageHandle(null, "Starting Client!! Welcome" + conf.BaseAdmin, 0);
        GenerateFiles();
        form.Text = "IRC Client Attempting to connect...";
        irc.OnConnected += new EventHandler(OnConnected);
        irc.OnChannelMessage += new IrcEventHandler(OnChanMsg);
        irc.OnQueryMessage += new IrcEventHandler(OnPriv);
        irc.OnJoin += new JoinEventHandler(OnJoined);
        irc.ActiveChannelSyncing = true;
        try {
            irc.Connect(conf.Server, 6667);
        } catch {
            Debug.WriteLine("Error in connecting"); // for debugging
        }
        irc.Listen();
    }
    public static void MessageHandle(string name, string msg, int type) {
        DateTime now = DateTime.Now;
        string timestamp = now.ToShortTimeString();
        if (type == 0) {
            // Client Message 
            form.MessageSend.Text += String.Format("[{0}]   --- {1}\n\r", timestamp, msg);
        } else if (type == 1) {
            // Private Message
            form.MessageSend.Text += String.Format("[{0}]   <{1}> {2}\n\r", timestamp, name, msg);
        } else if (type == 2) {
            // Regular User Message
            form.MessageSend.Text += String.Format("[{0}]   <{1}> {2}\n\r", timestamp, name, msg);
        } else if (type == 3) {
            // Channel Mod (Operator)
            form.MessageSend.Text += String.Format("[{0}]   <@{1}> {2}\n\r", timestamp, name, msg);
        } else if (type == 4) {
            // Broadcaster (Admin)
            form.MessageSend.Text += String.Format("[{0}]   <~{1}> {2}\n\r", timestamp, name, msg);
        } else if (type == 5) {
            // Bot Admin
            form.MessageSend.Text += String.Format("[{0}]   <+{1}> {2}\n\r", timestamp, name, msg);
        } else if (type == 6) {
            // Channel Action (/me)
            form.MessageSend.Text += String.Format("[{0}]   * {1} {2}\n\r", timestamp, name, msg);
        } else if (type == 7) {
            // Join Channel
            form.MessageSend.Text += String.Format("[{0}]   *** {1} has joined the channel!", timestamp, name);
        } else if (type == 8) {
            // Self Message
            form.MessageSend.Text += String.Format("[{0}]   <{1}> {2}\n\r", timestamp, name, msg);
            irc.SendMessage(SendType.Message, conf.Channel, msg);
        }
    }
        #region --- Connection Handler (OnConnected) ---
        public static void OnConnected(object Sender, EventArgs e) {
            try {
                irc.Login(conf.Nick, "Name", 0, conf.Nick);
                irc.RfcJoin(conf.Channel);
                form1.ResetText();
                form1.Text = "IRC.Cli | Connected as " + conf.Nick + " (" + conf.Channel + ")";
    
                MessageHandle(conf.Nick, null, 7);
                irc.Listen();
            } catch (Exception er) {
                MessageHandle(null, "[Error] : " + er.Message + " " + er.Source, 0);
            }
        }
        #endregion
    

1 个答案:

答案 0 :(得分:1)

看起来您通过使用表单替换控制台来修改Console projectWinForm projectConsole project之间的主要区别之一就是此行

Application.Run(new MainForm());

Application.Run(Form)显示表单,并开始运行标准的应用程序消息循环,因此表单可以对鼠标点击,重新绘制自身等做出反应。如果没有这一行,我不知道会发生什么。

  

全部。它不会绑定到UI ...

我的建议是在Visual Studio中创建一个新的Windows窗体项目,并将IRC代码用于单独的类中。

class IRCClient
{
    public event EventHandler OnConnected;
    public event IrcEventHandler OnChannelMessage;
    public event IrcEventHandler OnQueryMessage;
    public event JoinEventHandler OnJoin;
    public bool ActiveChannelSyncing { get; set; }
    ...    
}

然后在你的MainForm.cs

private void bgWork1(object sender, DoWorkEventArgs e) 
{
    IRCClient irc = new IRCClient();
    irc.OnConnected += new EventHandler(OnConnected);
    irc.OnChannelMessage += new IrcEventHandler(OnChanMsg);
    irc.OnQueryMessage += new IrcEventHandler(OnPriv);
    irc.OnJoin += new JoinEventHandler(OnJoined);
    irc.ActiveChannelSyncing = true; 
    irc.Start();
}

private void OnConnected(object sender, EventArgs e)
{
    ...
}

//and other event handlers

Program.cs

中的标准Windows表单条目
[STAThread]
static void Main(string[] args)
{
    Application.Run(new MainForm());
}