Search code examples
c#wpfchatactive-window

Send information between 2 wpf windows


We have 2 windows open, like a chat

enter image description here

This is what the textBox and the button looks like:

private void textBox_chat_TextChanged(object sender, TextChangedEventArgs e)
{
}

private void button_enviar_Click(object sender, RoutedEventArgs e)
{
    string chatMessage = textBox_chat.Text;
}

I would like to know how can I send information insered in a textbox by pressing the button "button_enviar". And to be printed to the other window. I have been looking things like Application.Current.Windows... but still don't found the way to make it.

My code looks actually like this:

MainWindow

   namespace WpfApplication1
{
    /// <summary>
    /// Lógica de interacción para MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }

            // automatic code generated by the button
            private void button_entrar_Click(object sender, RoutedEventArgs e)
            {
                // we catch the taxt input in the texBox
                string userLoginName = textBox_pantalla_inicial.Text;

                // We call the chat window
                Window window1 = new Window1();
                // we put the user name as the title of the chat window
                window1.Title = userLoginName;
                // show the chat window
                window1.Show();            
            }       
        }
    }

ChatWindow

namespace WpfApplication1
{
    /// <summary>
    /// Lógica de interacción para Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            // inicialize chatWindow
            InitializeComponent();            
        }

        private void textBox_chat_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void button_enviar_Click(object sender, RoutedEventArgs e)
        {
            string chatMessage = textBox_chat.Text;

        }       

        private void button_erase_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}

Solution

  • First, you should look into binding with XAML, such as here. That way in your C# code, you won't need to care about the UI controls used--and you can easily change those controls in the XAML if you don't like something or want to improve your window.

    You only need to think of your MainWindow and ChatWindow as objects. There are many ways you can make this work. One of the simplest ways is to have an event in your chat window that your main window subscribes to when it creates the chat window. Whenever a user enters his message, the chat window raises the event and passes the text through arguments in the event, which the main window catches and then can call a method (or set a property) in all the chat windows it is tracking so that the message gets passed to all chat windows.

    A simple example (free-typed, not tested):

    public class MainWindow : Window
    {
        List<ChatWindow> chatWindows = new List<ChatWindow>();
        public void AddChatWindow()
        {
            ChatWindow win = new ChatWindow();
            win.NewMessage += MessageReceived;
            win.Show();
            chatWindows.Add(win);
        }
        void MessageReceived(object sender, MessageEventArgs e)
        {
            ChatWindow me = sender as ChatWindow;
            if (me != null)
            {
                foreach (ChatWindow win in chatWindows)
                {
                    if (win != me)
                    {
                        win.Add(e.Message);
                    }
                }
            }
        }
    }
    
    public class ChatWindow : Window
    {
        public event EventHandler<MessageEventArgs> NewMessage;
    
        public void Add(string message)
        {
            Messsage += message;
        }
        public void UpdateText(string text)
        {
            if (NewMessage != null)
            {
                NewMessage(this, new MessageEventArgs(Message = text));
            }
        }
        public string Message {get;set;}
    }
    public class MessageEventArgs : EventArgs
    {
         public string Message{get;set;}
    }