在哈希表中存储表单 [英] Storing a Form in a Hashtable

查看:69
本文介绍了在哈希表中存储表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在聊天客户端,这就是我收到

消息时我想要做的事情:


private void ReceivedMessage_Event(对象发送者,

ClientEventArgs e)

{

// ChatWindow类派生自一个简单的表格

ChatWindow pm;


尝试

{

if(e.CommandType == CommandType.PrivateMessage)

{

//检查是否已有聊天窗口

open

if(this.m_ChatTable.ContainsKey (e.SenderName))

{

//将新消息添加到聊天窗口

pm =

(ChatWindow)this.m_ChatTable [e.SenderName];

pm.AddMessage(e.MessageText);

}

else

{

//没有窗口打开所以我们必须创建

一个并显示表单和消息

pm = new ChatWindow(e.SenderName,

this.m_userName);

pm.PrivateMessageSent + = n ew

ChatWindow.PrivateMessageEventHandler(ChatWindow_P rivateMessageSent);

pm.FormClosing + = new

FormClosingEventHandler(ChatWindow_FormClosing);

this.m_ChatTable.Add(e.SenderName,pm);

pm.AddMessage(e.MessageText);

pm.ShowDialog();

}

}

}

catch(InvalidOperationException ioe)

{

MessageBox.Show(ioe.StackTrace);

}


我在
中遇到System.InvalidOperationException
MyProgram.exe"信息。但是,当我关闭在else中创建的ChatWindow时,只会抛出异常。块。我已经在我的应用程序的每个语句周围放置了

try / catch块,但它仍然没有处理好。我愿意提供任何可以帮助你的代码

了解我正在尝试做什么。现在,这是我相信最相关的代码。如果我需要提供更多信息,请告知我们。

解决方案



tcomer写道:


我正在聊天客户端这就是我想要做的事情
$ b收到$ b消息:


private void ReceivedMessage_Event(对象发件人,

ClientEventArgs e)

{

// ChatWindow类派生自一个简单的表格

ChatWindow pm;


试试

{

if(e.CommandType == CommandType.PrivateMessage)

{

//检查是否已有聊天窗口

open

if(this.m_ChatTable.ContainsKey(e.SenderName))

{

//将新消息添加到CH在窗口

pm =

(ChatWindow)this.m_ChatTable [e.SenderName];

pm.AddMessage(e.MessageText); < br $>
}

其他

{

//没有窗口打开所以我们必须创建

one并显示表格和消息

pm = new ChatWindow(e.SenderName,

this.m_userName);

pm.PrivateMessageSent + = new

ChatWindow.PrivateMessageEventHandler(ChatWindow_P rivateMessageSent);

pm.FormClosing + = new

FormClosingEventHandler(ChatWindow_FormClosing);

this.m_ChatTable.Add(e.SenderName,pm);

pm.AddMessage(e.MessageText);

pm.ShowDialog() ;

}

}

}

catch(InvalidOperationException ioe)

{

MessageBox.Show(ioe.StackTrace);

}


我在

MyProgram.exe中遇到System.InvalidOperationException信息。但是,当我关闭在else中创建的ChatWindow时,只会抛出异常。块。我已经在我的应用程序的每个语句周围放置了

try / catch块,但它仍然没有处理好。我愿意提供任何可以帮助你的代码

了解我正在尝试做什么。现在,这是我相信最相关的代码。如果我需要提供更多信息,请告知我们。



查看异常的堆栈跟踪以及ChatWindow类的

代码会很有帮助。


查看异常的堆栈跟踪和ChatWindow类的


代码会很有帮助。



此时我无法提供堆栈跟踪信息,因为我是

无法捕获异常,但我我会继续寻找,我会发布它

如果我能找到哪个块正在抛出异常。


这是ChatWindow的源代码我正在使用的课程,请让我知道是否需要发布更多代码。


// ChatWindow的描述

公共类ChatWindow:表格

{

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;


//事件

公共事件PrivateMessageEventHandler PrivateMessageSent;

public void PrivateMessageSend(MessageEventArgs e)

{

if(PrivateMessage发送!= null)

PrivateMessageSent(this,e);

}


//代表

public delegate void PrivateMessageEventHandler(object sender,

MessageEventArgs e);

#region InitializeComponent


/ **** ********************************************** **** ****************

* Windows窗体设计师支持需要。


****** ******************************************** ****** ************** /

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); < br $>
//

// rtbMessageText

//

this.rtbMessageT ext.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。大小(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);

/ /

// btn发送

//

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(&q uot; 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


公共字符串收件人

{

get {return m_contactName; }

}

/ ******************************* ******************* ********************

*类构造函数


************************************** ************ ******************** /

公共ChatWindow()

{

InitializeComponent();

}

/ **************** ********************************** **************** ****

*班级建设者


*********************** *************************** ******************** /

public ChatWindow(string contactName,string senderName)

{

InitializeComponent();

this.Text =(contactName) +" - 聊天窗口");

this.m_contactName = contactName;

this.m_senderName = senderName;

}

/ ********************************************* ***** ********************

*班级建设者


** ************************************************ ** ******* *********** /

公共ChatWindow(字符串contactName,字符串senderName,字符串

messageText)

{

InitializeComponent();

this.Text =(contactName +" - 聊天窗口);

this.m_contactName = contactName;

this.m_senderName = senderName;

}

/ ********************************************* ** ********************

*在聊天窗口添加消息


************************************************** ******************** /

public void AddMessage(string messageText)

{

试试

{

this.rtbChatSummary.AppendText(this.m_contactName +":

" + messageText +" \\ \\ n");

}

catch(InvalidOperationException ioe)

{

MessageBox.Show(ioe。 StackTrace);

}

}

/ ******************** ****************************** ********************

*处理btnSend控件的Click事件。


******************** ****************************** ******************** /

private void btnSend_Click(object sen der,EventArgs e)

{

试试

{

this.SendMessage(this.rtbMessageText.Text) ;

this.rtbMessageText.Focus();

}

catch(InvalidOperationException ioe)

{

MessageBox.Show(ioe.StackTrace);

}

}

/ ******** ****************************************** ******** ************

*发送信息时处理必要的功能。


******** ****************************************** ******** ************ /

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);

}

}


公共类MessageEventArgs:EventArgs

{

private string clientName;

private string receiverName;

private string messageText;


public string Sender

{

get { return clientName; }

}


公共字符串收件人

{

get {return receiverName; }

}


公共字符串MessageText

{

get {return messageText; }

}


public MessageEventArgs(字符串发件人,字符串收件人,字符串

消息)

{

this.clientName =发件人;

this.receiverName =收件人;

this.messageText = message;

}

}




tcomer写道:


查看异常的堆栈跟踪以及ChatWindow类的

代码会很有帮助。



此时我无法提供堆栈跟踪信息,因为我是

无法捕获异常,但我我会继续寻找,我会发布它

如果我能找到哪个块正在抛出异常。



如果您让应用程序死亡,它应该在生成的错误对话框中打印堆栈跟踪




I''m working on a chat client and this is what I''m trying to do when a
message is received:

private void ReceivedMessage_Event(object sender,
ClientEventArgs e)
{
// the ChatWindow class is derived from a simple form
ChatWindow pm;

try
{
if (e.CommandType == CommandType.PrivateMessage)
{
// check to see if there is already a chat window
open
if (this.m_ChatTable.ContainsKey(e.SenderName))
{
// add the new message to the chat window
pm =
(ChatWindow)this.m_ChatTable[e.SenderName];
pm.AddMessage(e.MessageText);
}
else
{
// there is no window open so we have to create
one and show the form and the message
pm = new ChatWindow(e.SenderName,
this.m_userName);
pm.PrivateMessageSent += new
ChatWindow.PrivateMessageEventHandler(ChatWindow_P rivateMessageSent);
pm.FormClosing += new
FormClosingEventHandler(ChatWindow_FormClosing);
this.m_ChatTable.Add(e.SenderName, pm);
pm.AddMessage(e.MessageText);
pm.ShowDialog();
}
}
}
catch (InvalidOperationException ioe)
{
MessageBox.Show(ioe.StackTrace);
}

I''m getting a "System.InvalidOperationException occured in
MyProgram.exe" message. But the exception is only thrown when I close
the ChatWindow that is created in the "else" block. I''ve placed
try/catch blocks around every statement in my application but it still
goes unhandled. I''m willing to provide any code that may help you
understand what I''m trying to do. Right now this is the code that I
beleive is the most relevant. Please let me know if I need to provide
more information.

解决方案


tcomer wrote:

I''m working on a chat client and this is what I''m trying to do when a
message is received:

private void ReceivedMessage_Event(object sender,
ClientEventArgs e)
{
// the ChatWindow class is derived from a simple form
ChatWindow pm;

try
{
if (e.CommandType == CommandType.PrivateMessage)
{
// check to see if there is already a chat window
open
if (this.m_ChatTable.ContainsKey(e.SenderName))
{
// add the new message to the chat window
pm =
(ChatWindow)this.m_ChatTable[e.SenderName];
pm.AddMessage(e.MessageText);
}
else
{
// there is no window open so we have to create
one and show the form and the message
pm = new ChatWindow(e.SenderName,
this.m_userName);
pm.PrivateMessageSent += new
ChatWindow.PrivateMessageEventHandler(ChatWindow_P rivateMessageSent);
pm.FormClosing += new
FormClosingEventHandler(ChatWindow_FormClosing);
this.m_ChatTable.Add(e.SenderName, pm);
pm.AddMessage(e.MessageText);
pm.ShowDialog();
}
}
}
catch (InvalidOperationException ioe)
{
MessageBox.Show(ioe.StackTrace);
}

I''m getting a "System.InvalidOperationException occured in
MyProgram.exe" message. But the exception is only thrown when I close
the ChatWindow that is created in the "else" block. I''ve placed
try/catch blocks around every statement in my application but it still
goes unhandled. I''m willing to provide any code that may help you
understand what I''m trying to do. Right now this is the code that I
beleive is the most relevant. Please let me know if I need to provide
more information.

It would be helpful to see the stack trace from the exception, and the
code for the ChatWindow class.


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;
}
}



tcomer wrote:

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.

If you just let your application die, it should print the stack trace
in the resulting error dialog.


这篇关于在哈希表中存储表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆