It would be helpful to see the stack trace from the exception, and the
code for the ChatWindow class.
I'm unable to provide the stacktrace info at this point because I'm
unable to catch the exception, but I'll keep looking and I'll post it
if I can find which block is throwing the exception.
Here is the source for the ChatWindow class that I'm using, please let
me know if I need to post more code.
// Description of ChatWindow
public class ChatWindow : Form
{
protected System.Windows.Forms.RichTextBox rtbChatSummary;
protected System.Windows.Forms.RichTextBox rtbMessageText;
protected System.Windows.Forms.Button btnSend;
protected string m_contactName;
protected string m_senderName;
// Events
public event PrivateMessageEventHandler PrivateMessageSent;
public void PrivateMessageSend(MessageEventArgs e)
{
if (PrivateMessageSent != null)
PrivateMessageSent(this, e);
}
// Delegates
public delegate void PrivateMessageEventHandler(object sender,
MessageEventArgs e);
#region InitializeComponent
/************************************************** ********************
* Required for Windows Forms designer support.
************************************************** ********************/
private void InitializeComponent()
{
this.rtbChatSummary = new
System.Windows.Forms.RichTextBox();
this.rtbMessageText = new
System.Windows.Forms.RichTextBox();
this.btnSend = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// rtbChatSummary
//
this.rtbChatSummary.Anchor =
((System.Windows.Forms.AnchorStyles)((((System.Win dows.Forms.AnchorStyles.Top
| System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.rtbChatSummary.BackColor =
System.Drawing.SystemColors.Window;
this.rtbChatSummary.BorderStyle =
System.Windows.Forms.BorderStyle.FixedSingle;
this.rtbChatSummary.Location = new System.Drawing.Point(7,
4);
this.rtbChatSummary.Name = "rtbChatSummary";
this.rtbChatSummary.ReadOnly = true;
this.rtbChatSummary.ScrollBars =
System.Windows.Forms.RichTextBoxScrollBars.Vertica l;
this.rtbChatSummary.Size = new System.Drawing.Size(347,
150);
this.rtbChatSummary.TabIndex = 0;
this.rtbChatSummary.TabStop = false;
this.rtbChatSummary.Text = "";
this.rtbChatSummary.LinkClicked += new
System.Windows.Forms.LinkClickedEventHandler(this. rtbChatSummary_LinkClicked);
//
// rtbMessageText
//
this.rtbMessageText.Anchor =
((System.Windows.Forms.AnchorStyles)(((System.Wind ows.Forms.AnchorStyles.Bottom
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.rtbMessageText.BorderStyle =
System.Windows.Forms.BorderStyle.FixedSingle;
this.rtbMessageText.Location = new System.Drawing.Point(7,
160);
this.rtbMessageText.Name = "rtbMessageText";
this.rtbMessageText.ScrollBars =
System.Windows.Forms.RichTextBoxScrollBars.Vertica l;
this.rtbMessageText.Size = new System.Drawing.Size(347,
66);
this.rtbMessageText.TabIndex = 0;
this.rtbMessageText.TabStop = false;
this.rtbMessageText.Text = "";
this.rtbMessageText.LinkClicked += new
System.Windows.Forms.LinkClickedEventHandler(this. rtbMessageText_LinkClicked);
this.rtbMessageText.KeyDown += new
System.Windows.Forms.KeyEventHandler(this.rtbMessa geText_KeyDown);
//
// btnSend
//
this.btnSend.Anchor =
((System.Windows.Forms.AnchorStyles)((System.Windo ws.Forms.AnchorStyles.Bottom
| System.Windows.Forms.AnchorStyles.Right)));
this.btnSend.Font = new System.Drawing.Font("Tahoma",
8.25F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnSend.Location = new System.Drawing.Point(279, 232);
this.btnSend.Name = "btnSend";
this.btnSend.Size = new System.Drawing.Size(75, 30);
this.btnSend.TabIndex = 0;
this.btnSend.TabStop = false;
this.btnSend.Text = "Send";
this.btnSend.UseVisualStyleBackColor = true;
this.btnSend.Click += new
System.EventHandler(this.btnSend_Click);
//
// ChatWindow
//
this.ClientSize = new System.Drawing.Size(361, 267);
this.Controls.Add(this.btnSend);
this.Controls.Add(this.rtbMessageText);
this.Controls.Add(this.rtbChatSummary);
this.Font = new System.Drawing.Font("Tahoma", 8.25F,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
((byte)(0)));
this.Name = "ChatWindow";
this.StartPosition =
System.Windows.Forms.FormStartPosition.CenterScree n;
this.Text = "Chat Window";
this.ResumeLayout(false);
}
#endregion
public string Recipient
{
get { return m_contactName; }
}
/************************************************** ********************
* Class constructor
************************************************** ********************/
public ChatWindow()
{
InitializeComponent();
}
/************************************************** ********************
* Class Constructor
************************************************** ********************/
public ChatWindow(string contactName, string senderName)
{
InitializeComponent();
this.Text = (contactName + " - Chat Window");
this.m_contactName = contactName;
this.m_senderName = senderName;
}
/************************************************** ********************
* Class Constructor
************************************************** ********************/
public ChatWindow(string contactName, string senderName, string
messageText)
{
InitializeComponent();
this.Text = (contactName + " - Chat Window");
this.m_contactName = contactName;
this.m_senderName = senderName;
}
/************************************************** ********************
* Adds a message to the chat window
************************************************** ********************/
public void AddMessage(string messageText)
{
try
{
this.rtbChatSummary.AppendText(this.m_contactName + ":
" + messageText + "\n");
}
catch (InvalidOperationException ioe)
{
MessageBox.Show(ioe.StackTrace);
}
}
/************************************************** ********************
* Handles the Click event of the btnSend control.
************************************************** ********************/
private void btnSend_Click(object sender, EventArgs e)
{
try
{
this.SendMessage(this.rtbMessageText.Text);
this.rtbMessageText.Focus();
}
catch (InvalidOperationException ioe)
{
MessageBox.Show(ioe.StackTrace);
}
}
/************************************************** ********************
* Handles necessary functions when a message is sent.
************************************************** ********************/
protected void SendMessage(string messageText)
{
try
{
if (this.rtbMessageText.Text != "")
{
MessageEventArgs messArgs = new
MessageEventArgs(m_senderName, m_contactName, messageText);
this.rtbChatSummary.ScrollToCaret();
this.rtbChatSummary.AppendText(m_senderName + ": "
+ messageText + "\n");
this.rtbMessageText.Text = "";
this.PrivateMessageSend(messArgs);
}
}
catch (InvalidOperationException ioe)
{
MessageBox.Show(ioe.StackTrace);
}
}
public class MessageEventArgs : EventArgs
{
private string clientName;
private string receiverName;
private string messageText;
public string Sender
{
get { return clientName; }
}
public string Recipient
{
get { return receiverName; }
}
public string MessageText
{
get { return messageText; }
}
public MessageEventArgs(string sender, string recipient, string
message)
{
this.clientName = sender;
this.receiverName = recipient;
this.messageText = message;
}
}