简体   繁体   中英

Need help sending textbox text to the console application

Hey I'm new to coding C# and I'm trying to code a C# IRC Bot and I want to make a GUI for it so I can send chat from the GUI but I'm having problems doing so.

First off I'm opening two things, the Console app and a winform. This code is executed within another class:

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

Here is the main code (I did not code it all):

    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);
            }
        }
    }
}

What I've tried to do was make a button in the Form and a Textbox and I wanted to make the button add the text into the writer in the IrcBot class like:

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

But I can't get the variable "CHANNEL" from the Form, but I manualy changed it to test the writer and the writer did not work.

Basicaly I want the Form to use the console's writer, and I have no idea how to do it.

You can't access it because it's private . Change it to public and you should be able to access it from your Form1 class.

As CHANNEL is static you should access it with the class name, like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM