private string open_friend_chat() { try { string reciever = listBox1.SelectedItem.ToString(); char[] delimiterChars = { ' ', ',', '.', ':', '\t', '\r', '=','>','\n','-', }; string[] lines = reciever.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None); //string[] words = reciever.Split(delimiterChars); int i = 0; string reciever_user_name = null; string reciever_login = null; foreach (string s in lines) { if (i == 0) reciever_user_name = s; if (i == 2) reciever_login = s; i++; } string[] words = reciever_login.Split(delimiterChars); i=0; string reciever_login_id=null; foreach(string s in words) { if ( s !=" " && s!="ID" && s!=null) { reciever_login_id = s; //MessageBox.Show(s); i++; } } //MessageBox.Show(reciever_user_name); //MessageBox.Show(reciever_login_id); string find_ip_address_reciever = "select ip_address from Student_status where roll_no = '"+reciever_login_id+"'"; SqlConnection con = new SqlConnection(connectionstring); con.Open(); SqlDataAdapter da = new SqlDataAdapter(find_ip_address_reciever,con); DataSet ds = new DataSet(); da.Fill(ds); string reciever_ip_address = ds.Tables[0].Rows[0][0].ToString().Trim(); string sender_ip_address = getLocalIp(); string sender_role = Form1.role_id.ToString(); string sender_user_name = Form1.user_name; string sender_login_id = Form1.login_id; string reciever_role = "Student"; reciever_role = reciever_role.Trim(); string sender_port = "80" ; string reciever_port = "81" ; Chat_info info; /*var myDictionary = new Dictionary<KeyValuePair<string, string>, Offline_Chat.Chat_info>(); //Dictionary<Tuple<string, string>, List<string>> dict = new Dictionary<Tuple<string, string>, List<string>>(sender_login_id, reciever_login_id); Tuple<string,string> pair= new Tuple<string,string>(sender_login_id,reciever_login_id); KeyValuePair<string, string> pairs = new KeyValuePair<string, string>(sender_login_id,reciever_login_id); Chat_info info; if (myDictionary[pairs]!=null) { info = myDictionary[pairs]; } else { info = new Offline_Chat.Chat_info(sender_ip_address, reciever_ip_address, sender_port, reciever_port, sender_user_name, reciever_user_name, sender_login_id, reciever_login_id, sender_role, reciever_role); myDictionary.Add(pairs, info); }*/ //string exist = myDictionary.Add(new KeyValuePair<string, string>(sender_login_id, reciever_login_id),"sender"); //KeyValuePair<string, string> pairs = new KeyValuePair<string, string>(sender_login_id,reciever_login_id); bool flag=false; foreach(DictionaryEntry d in table_obj) { if(d.Key.ToString()==reciever_login_id) { info = (Offline_Chat.Chat_info)d.Value; flag = true; break; } } if(flag==false) { info = new Offline_Chat.Chat_info(sender_ip_address, reciever_ip_address, sender_port, reciever_port, sender_user_name, reciever_user_name, sender_login_id, reciever_login_id, sender_role, reciever_role); table_obj.Add(reciever_login_id, info); Send_message sm = new Send_message(sender_ip_address, reciever_ip_address, sender_port, reciever_port, sender_user_name, reciever_user_name, sender_login_id, reciever_login_id, sender_role, reciever_role); sm.ShowDialog(); } FormCollection fc = Application.OpenForms; foreach (Form frm in fc) { //MessageBox.Show(frm.ToString()); if(frm.ToString().Contains(reciever_user_name)) { // } else { table_obj.Remove(reciever_login_id); } } return "succesful"; } catch(Exception e) { MessageBox.Show(e.Message); return "Connecting......"; } }
private string open_friend_chat() { try { string reciever = listBox1.SelectedItem.ToString(); char[] delimiterChars = { ' ', ',', '.', ':', '\t', '\r', '=', '>', '\n', '-', }; string[] lines = reciever.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None); //string[] words = reciever.Split(delimiterChars); int i = 0; string reciever_user_name = null; string reciever_login = null; foreach (string s in lines) { if (i == 0) { reciever_user_name = s; } if (i == 2) { reciever_login = s; } i++; } string[] words = reciever_login.Split(delimiterChars); i = 0; string reciever_login_id = null; foreach (string s in words) { if (s != " " && s != "ID" && s != null) { reciever_login_id = s; //MessageBox.Show(s); i++; } } //MessageBox.Show(reciever_user_name); //MessageBox.Show(reciever_login_id); string find_ip_address_reciever = "select ip_address from Student_status where roll_no = '" + reciever_login_id + "'"; SqlConnection con = new SqlConnection(connectionstring); con.Open(); SqlDataAdapter da = new SqlDataAdapter(find_ip_address_reciever, con); DataSet ds = new DataSet(); da.Fill(ds); string reciever_ip_address = ds.Tables[0].Rows[0][0].ToString().Trim(); string sender_ip_address = getLocalIp(); string sender_role = Form1.role_id.ToString(); string sender_user_name = Form1.user_name; string sender_login_id = Form1.login_id; string reciever_role = "Student"; reciever_role = reciever_role.Trim(); string sender_port = "80"; string reciever_port = "81"; Chat_info info; /*var myDictionary = new Dictionary<KeyValuePair<string, string>, Offline_Chat.Chat_info>(); * //Dictionary<Tuple<string, string>, List<string>> dict = new Dictionary<Tuple<string, string>, List<string>>(sender_login_id, reciever_login_id); * Tuple<string,string> pair= new Tuple<string,string>(sender_login_id,reciever_login_id); * KeyValuePair<string, string> pairs = new KeyValuePair<string, string>(sender_login_id,reciever_login_id); * Chat_info info; * if (myDictionary[pairs]!=null) * { * info = myDictionary[pairs]; * * } * else * { * info = new Offline_Chat.Chat_info(sender_ip_address, reciever_ip_address, sender_port, reciever_port, sender_user_name, reciever_user_name, sender_login_id, reciever_login_id, sender_role, reciever_role); * myDictionary.Add(pairs, info); * }*/ //string exist = myDictionary.Add(new KeyValuePair<string, string>(sender_login_id, reciever_login_id),"sender"); //KeyValuePair<string, string> pairs = new KeyValuePair<string, string>(sender_login_id,reciever_login_id); bool flag = false; foreach (DictionaryEntry d in table_obj) { if (d.Key.ToString() == reciever_login_id) { info = (Offline_Chat.Chat_info)d.Value; flag = true; break; } } if (flag == false) { info = new Offline_Chat.Chat_info(sender_ip_address, reciever_ip_address, sender_port, reciever_port, sender_user_name, reciever_user_name, sender_login_id, reciever_login_id, sender_role, reciever_role); table_obj.Add(reciever_login_id, info); Send_message sm = new Send_message(sender_ip_address, reciever_ip_address, sender_port, reciever_port, sender_user_name, reciever_user_name, sender_login_id, reciever_login_id, sender_role, reciever_role); sm.ShowDialog(); } FormCollection fc = Application.OpenForms; foreach (Form frm in fc) { //MessageBox.Show(frm.ToString()); if (frm.ToString().Contains(reciever_user_name)) { // } else { table_obj.Remove(reciever_login_id); } } return("succesful"); } catch (Exception e) { MessageBox.Show(e.Message); return("Connecting......"); } }