jeg glemte lige at der også er en design fil, men det går nok.
Det du kan gøre er at stoppe din tråd. Mener du kan kalde ctThread.Stop() når du lukker din form.
En måske mere korrekt måde er at i stedet for at sige while(true) så laver du en variable bool running, så sætter du den til false når du lukker din applikation (formen) og så burde den bryde løkken. Du skal bare huske og låse din variabel "running" for undgå problemer med 2 tråde der tilgår en variabel samtidig.
Eksempel:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Collections;
- using System.Net.Sockets;
- using System.Net;
- using System.IO;
- using System.Threading;
- using System.Text.RegularExpressions;
- using bIRC;
-
- namespace bIRC
- {
- public partial class Form1 : Form
- {
- private bool running;
- private string this_server;
- private string this_user;
- private string this_channel;
- private int this_port;
-
- private string readData;
- private string farve;
-
- private string add_user;
- private string user_redy = "no";
-
- private string channel_add;
-
- private string channel_topic_add;
-
- private string luk = "no";
-
- ToolStripItem con_1_forlad;
-
- Color this_farve;
-
- Thread ctThread;
-
- ArrayList besked = new ArrayList();
- ArrayList besked_see = new ArrayList();
- ArrayList besked_color = new ArrayList();
- ArrayList member = new ArrayList();
- ArrayList channel = new ArrayList();
- ArrayList channel_topic = new ArrayList();
-
- NetworkStream stream;
- TcpClient irc;
- public StreamWriter writer;
- private string inputLine;
- public StreamReader reader;
-
-
-
- private StreamWriter log = new StreamWriter("log/log.txt");
-
- public Form1()
- {
-
- InitializeComponent();
- richTextBox2.SelectionAlignment = HorizontalAlignment.Center;
- }
-
- private void Form1_Load(object sender, EventArgs e)
- {
- con_1_forlad = contextMenuStrip1.Items.Add("Forlad");
- bIRC.server subForm = new server(this);
- subForm.Show();
- }
-
- public void start(string server, int port, string user, string channel)
- {
- irc = new TcpClient(server, port);
- stream = irc.GetStream();
- writer = new StreamWriter(stream);
- reader = new StreamReader(stream);
-
- skriv("NICK " + user);
- skriv("USER " + user + " 0 * :" + user);
-
- this_server = server;
- this_port = port;
- this_user = user;
- this_channel = channel;
-
-
- running = true;
- ctThread = new Thread(modtag);
- ctThread.Start();
- }
-
-
- private void modtag()
- {
-
- this.FormClosing += new FormClosingEventHandler(this.form_lukker);
- while (running)
- {
- if (luk == "yes")
- {
- Thread.Sleep(500000);
- }
- while (reader != null)
- {
- if (reader != null && irc.Connected)
- {
- inputLine = reader.ReadLine();
- }
- save_log(inputLine);
-
- string[] data = inputLine.Split(' ');
- string time = string.Format("{0:HH:mm}", DateTime.Now);
-
- switch (data[1])
- {
- case "001":
- skriv("JOIN " + this_channel);
- channel_add = this_channel;
- add_channel();
- opdate_channel_topic();//vi aktivere den her for så er vi sikker på at den bliver kaldt min 1 gang
- break;
- case "353":
- string[] head_353 = Regex.Split(inputLine, ".*? 353 .*? = (.*?) :(.*?)");
- string[] user_split = head_353[3].Split(' ');
- for (int i = 0; i < user_split.Length; i++)
- {
- member.Add(head_353[1] + "&" + user_split[i]);
- }
- opdate_user_list();
- user_redy = "yes";
- break;
- case "332":
- string[] head_332 = Regex.Split(inputLine, ":.*? 332 .*? (.*?) :(.*?)");
- channel_topic.Add(head_332[1] + "&" + head_332[3]);
- opdate_channel_topic();
- break;
-
- case "PRIVMSG":
- string[] req = Regex.Split(inputLine, ":(.*?)!.*? PRIVMSG (.*?) :(.*?)");
- if (Regex.IsMatch(inputLine, ":.*? PRIVMSG .*? :.*?ACTION (.*?)"))
- {
- string[] d = Regex.Split(inputLine, ":.*? PRIVMSG .*? :.*?ACTION (.*?)");
- besked.Add((string)req[2] + "&blue&[" + time + "] * " + get_op(req[2], req[1]) + req[1] + " " + d[2]);
- }
- else
- {
- besked.Add((string)req[2] + "&black&[" + time + "]" + get_op(req[2], req[1]) + (string)req[1] + ": " + req[4]);
- }
- break;
- case "JOIN":
- if (user_redy == "yes")
- {
- string[] head_join = Regex.Split(inputLine, ":(.*?)!.*? JOIN :?(.*?)");
- string fin = "no";
- for (int i = 0; i < member.Count; i++)
- {
- if ((string)member[i] == (string)head_join[3] + "&" + get_op((string)head_join[3], (string)head_join[1]) + (string)head_join[1])
- {
- fin = "yes";
- }
- }
- if (fin == "no")
- {
- member.Add(head_join[3] + "&" + get_op((string)head_join[3], (string)head_join[1]) + head_join[1]);
- opdate_user_list();
- besked.Add(head_join[3] + "&green&[" + time + "]" + head_join[1] + " er kommet ind i denne channel");
- }
- }
- break;
- case "NICK":
- string[] head_nick = Regex.Split(inputLine, ":(.*?)!.*? NICK :(.*?)");
- ArrayList remove_user = new ArrayList();
- for (int i = 0; i < member.Count; i++)
- {
- string[] reg_member = Regex.Split((string)member[i], "^(.*?)&(.*?)");
- string op = get_op(reg_member[1], head_nick[1]);
-
- if ((string)member[i] == (string)reg_member[1] + "&" + op + (string)head_nick[1])
- {
- member.Add(reg_member[1] + "&" + op + head_nick[3]);
- besked.Add(reg_member[1] + "&green&[" + time + "]" + head_nick[1] + " har skiftet nick til " + head_nick[3]);
- remove_user.Add(member[i]);
-
- }
- }
- for (int b = 0; b < remove_user.Count; b++) { member.Remove(remove_user[b]); }
- opdate_user_list();
-
- break;
- case "PART":
- string[] head_part = Regex.Split(inputLine, ":(.*?)!.*? PART :(.*?)");
- besked.Add(head_part[3] + "&red&[" + time + "]" + head_part[1] + " har forladt denne channel");
- member.Remove(head_part[3] + "&" + get_op(head_part[3], head_part[1]) + head_part[1]);
- opdate_user_list();
- break;
- case "QUIT":
- string[] head_quit = Regex.Split(inputLine, ":(.*?)!.*? QUIT :(.*?)");
-
- for (int i = 0; i < member.Count; i++)
- {
- string[] user = Regex.Split((string)member[i], "^(.*?)&(.*?)");
- string op = get_op(user[1], head_quit[1]);
- if ((string)member[i] == (string)user[1] + "&" + op + (string)head_quit[1])
- {
- string grund = "Lukkede ned";
- if ((string)head_quit[3] != string.Empty)
- {
- grund = head_quit[3];
- }
-
- besked.Add(user[1] + "&red&[" + time + "]" + user[3] + " har forladt denne channel grund: " + grund);
- member.Remove(member[i]);
- }
- }
- opdate_user_list();
- break;
- case "TOPIC":
- string[] head_topic = Regex.Split(inputLine, ":(.*?)!.*? TOPIC (.*?) :(.*?)");
- for (int i = 0; i < channel_topic.Count; i++)
- {
- string[] split = Regex.Split((string)channel_topic[i], "^(.*?)&(.*?)");
-
- if (split[1] == head_topic[2])
- {
- channel_topic.Remove(channel_topic[i]);
- channel_topic.Add(head_topic[2] + "&" + head_topic[4]);
- besked.Add(head_topic[2] + "&green&[" + time + "]" + head_topic[1] + " har skiftet channels topic til \"" + head_topic[4] + "\"");
- }
-
- }
- opdate_channel_topic();
- break;
- default:
- if (data[0] == "PING")
- {
- skriv("PONG " + data[1]);
- }
- break;
- }
-
- opdatere_besked();
- //readData = inputLine;
- //msg();
-
- }
- writer.Close();
- reader.Close();
- irc.Close();
- }
-
- }
-
- private void save_log(string skriv)
- {
- log.WriteLine(skriv, true);
- log.Flush();
- }
-
- private void hojere_klik_channel(object sender, MouseEventArgs e)
- {
-
- if (e.Button == MouseButtons.Right)
- {
- contextMenuStrip1.Show(Cursor.Position);
-
-
- int index = this.listBox2.IndexFromPoint(e.Location);
- if (index != ListBox.NoMatches)
- {
- contextMenuStrip1.Visible = true;
- listBox2.SelectedIndex = index;
-
-
- if (listBox2.Text != this_channel)
- {
- del_user_list();
- del_besked();
- besked_see.Clear();
- this_channel = listBox2.Text;
- opdate_user_list();
- opdatere_besked();
- opdate_channel_topic();
- }
-
- }
- else
- {
- contextMenuStrip1.Visible = false;
- }
- }
-
-
- }
-
- private void add_channel_topic()
- {
- if (this.InvokeRequired)
- {
- this.Invoke(new MethodInvoker(add_channel_topic));
- }
- else
- {
- richTextBox2.SelectionAlignment = HorizontalAlignment.Center;
- richTextBox2.Text = channel_topic_add;
- }
- }
-
- private void opdate_channel_topic()
- {
- string tekst = "TOPIC: ingen topic er sat i denne channel!";
- for (int i = 0; i < channel_topic.Count; i++)
- {
- string[] split = Regex.Split((string)channel_topic[i], "^(.*?)&(.*?)");
- if ((string)split[1] == this_channel)
- {
- tekst = split[3];
-
- }
- }
- channel_topic_add = tekst;
- add_channel_topic();
- }
-
- private void del_besked()
- {
- if (this.InvokeRequired)
- {
- this.Invoke(new MethodInvoker(del_besked));
- }
- else
- {
- richTextBox1.Text = "";
- }
- }
-
- private void opdatere_channel_list()
- {
- if (this.InvokeRequired)
- {
- this.Invoke(new MethodInvoker(opdatere_channel_list));
- }
- else
- {
- for (int i = 0; i < channel.Count; i++)
- {
- listBox2.Items.Add(channel[i]);
- }
- }
-
- }
-
- private void add_channel()
- {
- if (this.InvokeRequired)
- {
- this.Invoke(new MethodInvoker(add_channel));
- }
- else
- {
- listBox2.Items.Add(channel_add);
- channel.Add(channel_add);
- }
- }
-
- private string get_op(string channel, string nick)
- {
- string op = string.Empty;
-
- for (int i = 0; i < member.Count; i++)
- {
- if ((string)member[i] == channel + "&@" + nick && op == string.Empty)
- {
- op = "@";
- }
- else if ((string)member[i] == channel + "&+" + nick && op == string.Empty)
- {
- op = "+";
- }
- }
-
- return op;
- }
-
- private void add_user_t_list()
- {
- if (this.InvokeRequired)
- {
- this.Invoke(new MethodInvoker(add_user_t_list));
- }
- else
- {
- listBox1.Items.Add(add_user);
- }
- }
-
- private void empty_channel_list()
- {
- if (this.InvokeRequired)
- {
- this.Invoke(new MethodInvoker(empty_channel_list));
- }
- else
- {
- listBox2.Items.Clear();
- }
-
- }
-
- private void del_user_list()
- {
- if (this.InvokeRequired)
- {
- this.Invoke(new MethodInvoker(del_user_list));
- }
- else
- {
- listBox1.Items.Clear();
- }
- }
-
- private void opdate_user_list()
- {
- del_user_list();
- for (int i = 0; i < member.Count; i++)
- {
- string[] req = Regex.Split((string)member[i], "(.*?)&(.*?)");
- if ((string)req[1] == this_channel)
- {
- add_user = req[3];
- add_user_t_list();
- }
- }
- }
-
- private void opdatere_besked()
- {
- for (int i = 0; i < besked.Count; i++)
- {
- string[] req = Regex.Split((string)besked[i], "^(.*?)&(.*?)&(.*?)");
- if (req[1] == this_channel && besked_see.Contains(i) == false)
- {
-
- readData = (string)req[4];
- farve = req[2];
- msg();
- besked_see.Add(i);
- }
- }
- }
-
- private void skriv(string skriv)
- {
- writer.WriteLine(skriv);
- writer.Flush();
- }
-
- private void msg()
- {
- if (this.InvokeRequired)
- {
- this.Invoke(new MethodInvoker(msg));
- }
- else
- {
- besked_color.Add(farve + "&" + richTextBox1.Text.Length + "&" + readData.Length);
- richTextBox1.AppendText(readData + Environment.NewLine);
-
- for (int i = 0; i < besked_color.Count; i++)
- {
- string[] rich = Regex.Split((string)besked_color[i], "(.*?)&(.*?)&(.*?)");
-
- switch ((string)rich[1])
- {
- case "blue":
- this_farve = Color.Blue;
- break;
- case "black":
- this_farve = Color.Black;
- break;
- case "green":
- this_farve = Color.Green;
- break;
- case "red":
- this_farve = Color.Red;
- break;
- }
-
-
- richTextBox1.SelectionStart = int.Parse(rich[2]);
-
- richTextBox1.SelectionLength = int.Parse(rich[4]);
-
- richTextBox1.SelectionColor = this_farve;
- }
-
- richTextBox1.SelectionStart = richTextBox1.Text.Length;
- richTextBox1.SelectionLength = 0;
- richTextBox1.ScrollToCaret();
-
- }
- }
-
- private void svar()
- {
- string time = string.Format("{0:HH:mm}", DateTime.Now);
- if (Regex.IsMatch(textBox1.Text, "^/me (.*?)"))
- {
- string[] me = Regex.Split(textBox1.Text, "^/me (.*?)");
- besked.Add(this_channel + "&blue&[" + time + "] * " + this_user + " " + me[2] + " ");
- skriv("PRIVMSG " + this_channel + " :\u0001ACTION " + me[2] + "\u0001");
- }
- else if (Regex.IsMatch(textBox1.Text, "/join #.*?"))
- {
- string[] channell = Regex.Split(textBox1.Text, "/join #(.*?)");
- user_redy = "no";
- skriv("JOIN #" + channell[2]);
- channel_add = "#" + channell[2];
- add_channel();
- }
- else if (Regex.IsMatch(textBox1.Text, "^/nick (.*?)"))
- {
- string[] username = Regex.Split(textBox1.Text, "^/nick (.*?)");
- this_user = username[2];
- skriv("NICK " + username[2]);
- }
- else
- {
- besked.Add(this_channel + "&black&[" + time + "]" + this_user + ": " + textBox1.Text);
- skriv("PRIVMSG " + this_channel + " :" + textBox1.Text);
- }
- opdatere_besked();
-
- textBox1.Text = "";
- }
-
- private void enter_modtag(object sender, KeyPressEventArgs e)
- {
- if (e.KeyChar == (char)Keys.Enter && textBox1.Text != string.Empty)
- {
- svar();
- }
- }
-
- private void button1_Click(object sender, EventArgs e)
- {
- if (textBox1.Text != string.Empty)
- {
- svar();
- }
- }
-
- private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
- {
- if (listBox2.Text != this_channel && listBox2.Text != string.Empty)
- {
- del_user_list();
- del_besked();
- besked_see.Clear();
- this_channel = listBox2.Text;
- opdate_user_list();
- opdatere_besked();
- opdate_channel_topic();
- }
- }
-
-
-
- private void contextMenuStrip1_Opening(object sender, ToolStripItemClickedEventArgs e)
- {
- ToolStripItem item = e.ClickedItem;
-
- if (item == con_1_forlad)
- {
- string sel_name = listBox2.Text;
- if (channel.Count == 1 || channel.Count == 0)
- {
- string time = string.Format("{0:HH:mm}", DateTime.Now);
- besked.Add(sel_name + "&red&[" + time + "]ERROR! " + sel_name + " er den endeste du har åben og kan derfor ikke lukkes!");
- opdatere_besked();
- }
- else
- {
- skriv("PART " + sel_name);
- channel.Remove(sel_name);
- listBox2.Items.Clear();
- empty_channel_list();
- opdatere_channel_list();
- }
- }
-
- }
-
- private void form_lukker(object sender, FormClosingEventArgs e)
- {
- lock (new object())
- running = false;
- }
-
- }
- }
Det er ikke testet, da jeg ikke har det hele. Er du i tvivl om det jeg har gjort så spørg
Edit:jeg tror også man kan følgende (ligger tættere på det du har):
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Collections;
- using System.Net.Sockets;
- using System.Net;
- using System.IO;
- using System.Threading;
- using System.Text.RegularExpressions;
- using bIRC;
-
- namespace bIRC
- {
- public partial class Form1 : Form
- {
- private string this_server;
- private string this_user;
- private string this_channel;
- private int this_port;
-
- private string readData;
- private string farve;
-
- private string add_user;
- private string user_redy = "no";
-
- private string channel_add;
-
- private string channel_topic_add;
-
- private string luk = "no";
-
- ToolStripItem con_1_forlad;
-
- Color this_farve;
-
- Thread ctThread;
-
- ArrayList besked = new ArrayList();
- ArrayList besked_see = new ArrayList();
- ArrayList besked_color = new ArrayList();
- ArrayList member = new ArrayList();
- ArrayList channel = new ArrayList();
- ArrayList channel_topic = new ArrayList();
-
- NetworkStream stream;
- TcpClient irc;
- public StreamWriter writer;
- private string inputLine;
- public StreamReader reader;
-
-
-
- private StreamWriter log = new StreamWriter("log/log.txt");
-
- public Form1()
- {
-
- InitializeComponent();
- richTextBox2.SelectionAlignment = HorizontalAlignment.Center;
- }
-
- private void Form1_Load(object sender, EventArgs e)
- {
- con_1_forlad = contextMenuStrip1.Items.Add("Forlad");
- bIRC.server subForm = new server(this);
- subForm.Show();
- }
-
- public void start(string server, int port, string user, string channel)
- {
- irc = new TcpClient(server, port);
- stream = irc.GetStream();
- writer = new StreamWriter(stream);
- reader = new StreamReader(stream);
-
- skriv("NICK " + user);
- skriv("USER " + user + " 0 * :" + user);
-
- this_server = server;
- this_port = port;
- this_user = user;
- this_channel = channel;
-
- ctThread = new Thread(modtag);
- ctThread.Start();
- }
-
-
- private void modtag()
- {
-
- this.FormClosing += new FormClosingEventHandler(this.form_lukker);
- while (true)
- {
- if (luk == "yes")
- {
- Thread.Sleep(500000);
- }
- while (reader != null)
- {
- if (reader != null && irc.Connected)
- {
- inputLine = reader.ReadLine();
- }
- save_log(inputLine);
-
- string[] data = inputLine.Split(' ');
- string time = string.Format("{0:HH:mm}", DateTime.Now);
-
- switch (data[1])
- {
- case "001":
- skriv("JOIN " + this_channel);
- channel_add = this_channel;
- add_channel();
- opdate_channel_topic();//vi aktivere den her for så er vi sikker på at den bliver kaldt min 1 gang
- break;
- case "353":
- string[] head_353 = Regex.Split(inputLine, ".*? 353 .*? = (.*?) :(.*?)");
- string[] user_split = head_353[3].Split(' ');
- for (int i = 0; i < user_split.Length; i++)
- {
- member.Add(head_353[1] + "&" + user_split[i]);
- }
- opdate_user_list();
- user_redy = "yes";
- break;
- case "332":
- string[] head_332 = Regex.Split(inputLine, ":.*? 332 .*? (.*?) :(.*?)");
- channel_topic.Add(head_332[1] + "&" + head_332[3]);
- opdate_channel_topic();
- break;
-
- case "PRIVMSG":
- string[] req = Regex.Split(inputLine, ":(.*?)!.*? PRIVMSG (.*?) :(.*?)");
- if (Regex.IsMatch(inputLine, ":.*? PRIVMSG .*? :.*?ACTION (.*?)"))
- {
- string[] d = Regex.Split(inputLine, ":.*? PRIVMSG .*? :.*?ACTION (.*?)");
- besked.Add((string)req[2] + "&blue&[" + time + "] * " + get_op(req[2], req[1]) + req[1] + " " + d[2]);
- }
- else
- {
- besked.Add((string)req[2] + "&black&[" + time + "]" + get_op(req[2], req[1]) + (string)req[1] + ": " + req[4]);
- }
- break;
- case "JOIN":
- if (user_redy == "yes")
- {
- string[] head_join = Regex.Split(inputLine, ":(.*?)!.*? JOIN :?(.*?)");
- string fin = "no";
- for (int i = 0; i < member.Count; i++)
- {
- if ((string)member[i] == (string)head_join[3] + "&" + get_op((string)head_join[3], (string)head_join[1]) + (string)head_join[1])
- {
- fin = "yes";
- }
- }
- if (fin == "no")
- {
- member.Add(head_join[3] + "&" + get_op((string)head_join[3], (string)head_join[1]) + head_join[1]);
- opdate_user_list();
- besked.Add(head_join[3] + "&green&[" + time + "]" + head_join[1] + " er kommet ind i denne channel");
- }
- }
- break;
- case "NICK":
- string[] head_nick = Regex.Split(inputLine, ":(.*?)!.*? NICK :(.*?)");
- ArrayList remove_user = new ArrayList();
- for (int i = 0; i < member.Count; i++)
- {
- string[] reg_member = Regex.Split((string)member[i], "^(.*?)&(.*?)");
- string op = get_op(reg_member[1], head_nick[1]);
-
- if ((string)member[i] == (string)reg_member[1] + "&" + op + (string)head_nick[1])
- {
- member.Add(reg_member[1] + "&" + op + head_nick[3]);
- besked.Add(reg_member[1] + "&green&[" + time + "]" + head_nick[1] + " har skiftet nick til " + head_nick[3]);
- remove_user.Add(member[i]);
-
- }
- }
- for (int b = 0; b < remove_user.Count; b++) { member.Remove(remove_user[b]); }
- opdate_user_list();
-
- break;
- case "PART":
- string[] head_part = Regex.Split(inputLine, ":(.*?)!.*? PART :(.*?)");
- besked.Add(head_part[3] + "&red&[" + time + "]" + head_part[1] + " har forladt denne channel");
- member.Remove(head_part[3] + "&" + get_op(head_part[3], head_part[1]) + head_part[1]);
- opdate_user_list();
- break;
- case "QUIT":
- string[] head_quit = Regex.Split(inputLine, ":(.*?)!.*? QUIT :(.*?)");
-
- for (int i = 0; i < member.Count; i++)
- {
- string[] user = Regex.Split((string)member[i], "^(.*?)&(.*?)");
- string op = get_op(user[1], head_quit[1]);
- if ((string)member[i] == (string)user[1] + "&" + op + (string)head_quit[1])
- {
- string grund = "Lukkede ned";
- if ((string)head_quit[3] != string.Empty)
- {
- grund = head_quit[3];
- }
-
- besked.Add(user[1] + "&red&[" + time + "]" + user[3] + " har forladt denne channel grund: " + grund);
- member.Remove(member[i]);
- }
- }
- opdate_user_list();
- break;
- case "TOPIC":
- string[] head_topic = Regex.Split(inputLine, ":(.*?)!.*? TOPIC (.*?) :(.*?)");
- for (int i = 0; i < channel_topic.Count; i++)
- {
- string[] split = Regex.Split((string)channel_topic[i], "^(.*?)&(.*?)");
-
- if (split[1] == head_topic[2])
- {
- channel_topic.Remove(channel_topic[i]);
- channel_topic.Add(head_topic[2] + "&" + head_topic[4]);
- besked.Add(head_topic[2] + "&green&[" + time + "]" + head_topic[1] + " har skiftet channels topic til \"" + head_topic[4] + "\"");
- }
-
- }
- opdate_channel_topic();
- break;
- default:
- if (data[0] == "PING")
- {
- skriv("PONG " + data[1]);
- }
- break;
- }
-
- opdatere_besked();
- //readData = inputLine;
- //msg();
-
- }
- writer.Close();
- reader.Close();
- irc.Close();
- }
-
- }
-
- private void save_log(string skriv)
- {
- log.WriteLine(skriv, true);
- log.Flush();
- }
-
- private void hojere_klik_channel(object sender, MouseEventArgs e)
- {
-
- if (e.Button == MouseButtons.Right)
- {
- contextMenuStrip1.Show(Cursor.Position);
-
-
- int index = this.listBox2.IndexFromPoint(e.Location);
- if (index != ListBox.NoMatches)
- {
- contextMenuStrip1.Visible = true;
- listBox2.SelectedIndex = index;
-
-
- if (listBox2.Text != this_channel)
- {
- del_user_list();
- del_besked();
- besked_see.Clear();
- this_channel = listBox2.Text;
- opdate_user_list();
- opdatere_besked();
- opdate_channel_topic();
- }
-
- }
- else
- {
- contextMenuStrip1.Visible = false;
- }
- }
-
-
- }
-
- private void add_channel_topic()
- {
- if (this.InvokeRequired)
- {
- this.Invoke(new MethodInvoker(add_channel_topic));
- }
- else
- {
- richTextBox2.SelectionAlignment = HorizontalAlignment.Center;
- richTextBox2.Text = channel_topic_add;
- }
- }
-
- private void opdate_channel_topic()
- {
- string tekst = "TOPIC: ingen topic er sat i denne channel!";
- for (int i = 0; i < channel_topic.Count; i++)
- {
- string[] split = Regex.Split((string)channel_topic[i], "^(.*?)&(.*?)");
- if ((string)split[1] == this_channel)
- {
- tekst = split[3];
-
- }
- }
- channel_topic_add = tekst;
- add_channel_topic();
- }
-
- private void del_besked()
- {
- if (this.InvokeRequired)
- {
- this.Invoke(new MethodInvoker(del_besked));
- }
- else
- {
- richTextBox1.Text = "";
- }
- }
-
- private void opdatere_channel_list()
- {
- if (this.InvokeRequired)
- {
- this.Invoke(new MethodInvoker(opdatere_channel_list));
- }
- else
- {
- for (int i = 0; i < channel.Count; i++)
- {
- listBox2.Items.Add(channel[i]);
- }
- }
-
- }
-
- private void add_channel()
- {
- if (this.InvokeRequired)
- {
- this.Invoke(new MethodInvoker(add_channel));
- }
- else
- {
- listBox2.Items.Add(channel_add);
- channel.Add(channel_add);
- }
- }
-
- private string get_op(string channel, string nick)
- {
- string op = string.Empty;
-
- for (int i = 0; i < member.Count; i++)
- {
- if ((string)member[i] == channel + "&@" + nick && op == string.Empty)
- {
- op = "@";
- }
- else if ((string)member[i] == channel + "&+" + nick && op == string.Empty)
- {
- op = "+";
- }
- }
-
- return op;
- }
-
- private void add_user_t_list()
- {
- if (this.InvokeRequired)
- {
- this.Invoke(new MethodInvoker(add_user_t_list));
- }
- else
- {
- listBox1.Items.Add(add_user);
- }
- }
-
- private void empty_channel_list()
- {
- if (this.InvokeRequired)
- {
- this.Invoke(new MethodInvoker(empty_channel_list));
- }
- else
- {
- listBox2.Items.Clear();
- }
-
- }
-
- private void del_user_list()
- {
- if (this.InvokeRequired)
- {
- this.Invoke(new MethodInvoker(del_user_list));
- }
- else
- {
- listBox1.Items.Clear();
- }
- }
-
- private void opdate_user_list()
- {
- del_user_list();
- for (int i = 0; i < member.Count; i++)
- {
- string[] req = Regex.Split((string)member[i], "(.*?)&(.*?)");
- if ((string)req[1] == this_channel)
- {
- add_user = req[3];
- add_user_t_list();
- }
- }
- }
-
- private void opdatere_besked()
- {
- for (int i = 0; i < besked.Count; i++)
- {
- string[] req = Regex.Split((string)besked[i], "^(.*?)&(.*?)&(.*?)");
- if (req[1] == this_channel && besked_see.Contains(i) == false)
- {
-
- readData = (string)req[4];
- farve = req[2];
- msg();
- besked_see.Add(i);
- }
- }
- }
-
- private void skriv(string skriv)
- {
- writer.WriteLine(skriv);
- writer.Flush();
- }
-
- private void msg()
- {
- if (this.InvokeRequired)
- {
- this.Invoke(new MethodInvoker(msg));
- }
- else
- {
- besked_color.Add(farve + "&" + richTextBox1.Text.Length + "&" + readData.Length);
- richTextBox1.AppendText(readData + Environment.NewLine);
-
- for (int i = 0; i < besked_color.Count; i++)
- {
- string[] rich = Regex.Split((string)besked_color[i], "(.*?)&(.*?)&(.*?)");
-
- switch ((string)rich[1])
- {
- case "blue":
- this_farve = Color.Blue;
- break;
- case "black":
- this_farve = Color.Black;
- break;
- case "green":
- this_farve = Color.Green;
- break;
- case "red":
- this_farve = Color.Red;
- break;
- }
-
-
- richTextBox1.SelectionStart = int.Parse(rich[2]);
-
- richTextBox1.SelectionLength = int.Parse(rich[4]);
-
- richTextBox1.SelectionColor = this_farve;
- }
-
- richTextBox1.SelectionStart = richTextBox1.Text.Length;
- richTextBox1.SelectionLength = 0;
- richTextBox1.ScrollToCaret();
-
- }
- }
-
- private void svar()
- {
- string time = string.Format("{0:HH:mm}", DateTime.Now);
- if (Regex.IsMatch(textBox1.Text, "^/me (.*?)"))
- {
- string[] me = Regex.Split(textBox1.Text, "^/me (.*?)");
- besked.Add(this_channel + "&blue&[" + time + "] * " + this_user + " " + me[2] + " ");
- skriv("PRIVMSG " + this_channel + " :\u0001ACTION " + me[2] + "\u0001");
- }
- else if (Regex.IsMatch(textBox1.Text, "/join #.*?"))
- {
- string[] channell = Regex.Split(textBox1.Text, "/join #(.*?)");
- user_redy = "no";
- skriv("JOIN #" + channell[2]);
- channel_add = "#" + channell[2];
- add_channel();
- }
- else if (Regex.IsMatch(textBox1.Text, "^/nick (.*?)"))
- {
- string[] username = Regex.Split(textBox1.Text, "^/nick (.*?)");
- this_user = username[2];
- skriv("NICK " + username[2]);
- }
- else
- {
- besked.Add(this_channel + "&black&[" + time + "]" + this_user + ": " + textBox1.Text);
- skriv("PRIVMSG " + this_channel + " :" + textBox1.Text);
- }
- opdatere_besked();
-
- textBox1.Text = "";
- }
-
- private void enter_modtag(object sender, KeyPressEventArgs e)
- {
- if (e.KeyChar == (char)Keys.Enter && textBox1.Text != string.Empty)
- {
- svar();
- }
- }
-
- private void button1_Click(object sender, EventArgs e)
- {
- if (textBox1.Text != string.Empty)
- {
- svar();
- }
- }
-
- private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
- {
- if (listBox2.Text != this_channel && listBox2.Text != string.Empty)
- {
- del_user_list();
- del_besked();
- besked_see.Clear();
- this_channel = listBox2.Text;
- opdate_user_list();
- opdatere_besked();
- opdate_channel_topic();
- }
- }
-
-
-
- private void contextMenuStrip1_Opening(object sender, ToolStripItemClickedEventArgs e)
- {
- ToolStripItem item = e.ClickedItem;
-
- if (item == con_1_forlad)
- {
- string sel_name = listBox2.Text;
- if (channel.Count == 1 || channel.Count == 0)
- {
- string time = string.Format("{0:HH:mm}", DateTime.Now);
- besked.Add(sel_name + "&red&[" + time + "]ERROR! " + sel_name + " er den endeste du har åben og kan derfor ikke lukkes!");
- opdatere_besked();
- }
- else
- {
- skriv("PART " + sel_name);
- channel.Remove(sel_name);
- listBox2.Items.Clear();
- empty_channel_list();
- opdatere_channel_list();
- }
- }
-
- }
-
- private void form_lukker(object sender, FormClosingEventArgs e)
- {
- try
- {
- ctThread.Abort();
- }
- catch(ThreadAbortException abortException)
- {
- save_log("Stoppede tråd......");
- }
-
- if (reader != null)
- {
- writer.Close();
- reader.Close();
-
- reader = null;
- irc.Close();
- }
- }
-
- }
- }
Indlæg senest redigeret d. 15.07.2012 16:01 af Bruger #6559