需要帮助将文本框文本发送到控制台应用程序

时间:2013-12-29 16:53:48

标签: c# winforms console

嘿,我是编码C#的新手,我正在尝试编写一个C#IRC Bot,我想为它制作一个GUI,这样我就可以从GUI发送聊天了但是我遇到了这样的问题。

首先,我打开两件事,控制台应用程序和winform。 此代码在另一个类中执行:

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());

这是主要代码(我没有编写所有代码):

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

namespace IRCBot
{
    public partial class Form1 : Form
    {


        public Form1()
        {
            InitializeComponent();
        }
    }

    class IrcBot
    {
        // Irc server to connect 
        public static string SERVER = File.ReadAllText("server.txt");

        // Irc server's port (6667 is default port)
        private static int PORT = 6667;

        // User information defined in RFC 2812 (Internet Relay Chat: Client Protocol) is sent to irc server    
        private static string USER = "USER LeeSharp Bot v1 * :I'm a C# IRC bot made by LeeIzaZombie";

        // Bot's nickname
        private static string NICK = File.ReadAllText("nickname.txt");

        // Channel to join
        private static string CHANNEL = File.ReadAllText("channel.txt");

        // StreamWriter is declared here so that PingSender can access it
        public static StreamWriter writer;

        static void Main(string[] args)
        {
            NetworkStream stream;
            TcpClient irc;
            string inputLine;
            StreamReader reader;
            string nickname;

            try
            {
                irc = new TcpClient(SERVER, PORT);
                stream = irc.GetStream();
                reader = new StreamReader(stream);
                writer = new StreamWriter(stream);

                // Start PingSender thread
                PingSender ping = new PingSender();
                ping.Start();
                Console.WriteLine("Connecting to " + SERVER);
                Console.WriteLine("Port: " + PORT);

                writer.WriteLine(USER);
                writer.Flush();
                Console.WriteLine("Nickname: " + NICK + ".");
                writer.WriteLine("NICK " + NICK);
                writer.Flush();
                Console.WriteLine("Now joining " + CHANNEL);
                writer.WriteLine("JOIN " + CHANNEL);
                writer.Flush();


                while (true)
                {
                    while ((inputLine = reader.ReadLine()) != null)
                    {

                        if (inputLine.EndsWith("JOIN :" + CHANNEL))
                        {
                            nickname = inputLine.Substring(1, inputLine.IndexOf("!") - 1);

                            writer.WriteLine("PRIVMSG " + CHANNEL + " :" + "Hi " + nickname +
                            " and welcome to " + CHANNEL + " !");
                            writer.Flush();

                            // Sleep to prevent flooding :P
                            Thread.Sleep(2000);
                        }

                        if (inputLine.Contains(" is no longer AFK"))
                        {
                            writer.WriteLine("PRIVMSG " + CHANNEL + " :Welcome back! :D");
                            writer.Flush();

                            // Sleep to prevent excess flood
                            Thread.Sleep(2000);
                        }

                        if (inputLine.Contains("leebot go away"))
                        {
                            nickname = inputLine.Substring(1, inputLine.IndexOf("!") - 1);
                            if (nickname == "LeeIzaZombie")
                            {
                                writer.WriteLine("QUIT");
                                writer.Flush();
                            }


                            // Sleep to prevent excess flood
                            Thread.Sleep(2000);
                        }

                       /* if (inputLine.EndsWith(" joined the game."))
                        {
                            nickname = inputLine.Substring(1, inputLine.IndexOf(" joined") - 1);
                            nickname2 = nickname.Substring(1, nickname.IndexOf("PRIVMSG") - 1);
                            writer.WriteLine("PRIVMSG " + CHANNEL + " :Hey, " + nickname2 + " welcome to $server!");
                            writer.Flush();

                            // Sleep to prevent excess flood 
                            Thread.Sleep(2000);
                        }*/

                        if (inputLine.Contains("Hey LeeBot"))
                        {
                            writer.WriteLine("PRIVMSG " + CHANNEL + " :Hey, what up?");
                            writer.Flush();

                            // Sleep to prevent excess flood
                            Thread.Sleep(2000);
                        }

                        Thread.Sleep(5);

                        if (inputLine.EndsWith("JOIN :" + CHANNEL))
                        {
                            nickname = inputLine.Substring(1, inputLine.IndexOf("!") - 1);
                            inputLine = nickname + " joined " + CHANNEL;
                        }

                        Console.WriteLine(inputLine);
                    }

                    // Close all streams
                    writer.Close();
                    reader.Close();
                    irc.Close();
                }
            }
            catch (Exception e)
            {

                // Show the exception, sleep for a while and try to establish a new connection to irc server
                Console.WriteLine(e.ToString());
                Thread.Sleep(5000);
                string[] argv = { };
                Main(argv);
            }
        }
    }
}

我尝试做的是在Form和Textbox中创建一个按钮,我想让按钮将文本添加到IrcBot类的编写器中,如:

 writer.WriteLine("PRIVMSG " + CHANNEL + " :" + textbox.Text);
                            writer.Flush();

但我无法从表单中获取变量“CHANNEL”,但我手动改变它以测试作者并且作者没有工作。

Basicaly我希望Form使用控制台的编写器,我不知道该怎么做。

1 个答案:

答案 0 :(得分:0)

您无法访问它,因为它是private。将其更改为public,您应该可以从Form1课程中访问它。

由于CHANNEL是静态的,您应该使用类名访问它,如下所示:

IrcBot.writer.WriteLine("PRIVMSG " + IrcBot.CHANNEL + " :" + textbox.Text);
IrcBot.writer.Flush();