Click here to Skip to main content
15,896,154 members
Articles / Web Development / ASP.NET

Chat Application using Web services in C#

Rate me:
Please Sign up or sign in to vote.
4.85/5 (38 votes)
15 May 2008CPOL5 min read 385.3K   41.8K   139  
This is a cool chat application created in DotNet using web services having all the functionalities.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Collections;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using ChatClient.ChatServices;

namespace ChatClient
{
    public partial class Form1 : Form
    {
        // Creating the object of the web service
        ChatService chat = new ChatService();
        // Arraylist to hold all the currently logged users.
        static public ArrayList arrOUsers = new ArrayList();
        static public string strMe = string.Empty;
        static public string strMess = string.Empty;
        private bool userFlag = true;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // Retrieving the Windows UserName.
            lblUserName.Text = System.Windows.Forms.SystemInformation.UserName.ToString();
            strMe = lblUserName.Text;
            // Calling the web service for 'AddUser' for adding himself after logging.
            chat.AddUser(lblUserName.Text);
            timer1.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            ShowUsers();
            RecvMessage();
        }

        private void ShowUsers()
        {
            lstUsers.Items.Clear();
            // Calling the web service for 'GetUsers' for retrieving all the currently logged users. 
            string[] strUsers = chat.GetUsers().Split('|');
            for (int i = 0; i < strUsers.Length-1; i++)
            {
                if(strUsers[i].ToUpper() != lblUserName.Text.Trim().ToUpper())
                    lstUsers.Items.Add(strUsers[i], 1);
            }            
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            // On closing hide the form that can be re-opened from the icon in the system tray.
            Hide();
        }

        private void lstUsers_DoubleClick(object sender, EventArgs e)
        {
            userFlag = true;
            // To check whether the chat window for this user is already open.
            for (int i = 0; i < arrOUsers.Count; i++)
            {
                if (lstUsers.FocusedItem.Text.ToString() == arrOUsers[i].ToString())
                    userFlag = false;
            }
            if (userFlag == true)
            {
                arrOUsers.Add(lstUsers.FocusedItem.Text.ToString());
                Form f = new PrivateMessage(lstUsers.FocusedItem.Text.ToString());
                f.Show();
            }
        }

        private void lstUsers_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        private void RecvMessage()
        {
            strMess = chat.ReceiveMessage(lblUserName.Text);
            if (strMess.Trim().Length > 0)
            {
                string[] strUser = strMess.Split(':');
                userFlag = true;
                for (int i = 0; i < arrOUsers.Count; i++)
                {
                    if (strUser[0] == arrOUsers[i].ToString())
                        userFlag = false;
                }
                if (userFlag == true)
                {
                    arrOUsers.Add(strUser[0]);
                    Form f = new PrivateMessage(strUser[0], strMess);
                    f.Show();
                }
            }
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            chat.RemoveUser(lblUserName.Text);
            notifyIcon1.Visible = false;
            notifyIcon1.Dispose();
            Environment.Exit(0);           
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Form f = new About();
            f.Show();
        }

        private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Show();
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
United States United States
He has a total experience of more than 6 years in programming on different technologies. Currently working as a Systems Analyst.

Comments and Discussions