【发布时间】:2014-01-19 00:07:30
【问题描述】:
我在将信息从其他类“传递”到程序的主窗体时遇到问题。它是一个客户端-服务器应用程序。我想在列表框中的服务器主窗体上创建一个“日志接口”(或类似的东西,并不重要......),例如当一个客户端连接到服务器时显示。所以在我的应用程序中,它涉及两个类:Datebase.cs 和 Form.cs。当来自 database.cs 的呼叫时,我应该使用自定义文本修改 listbox1.Text。我试过 Form1.listBox1.Items.Add(logon);但它失败了:/
Database.cs
// Other classes and methods...
public class Database
{
public string logon;
public string logoff;
// some methods
internal bool Logon(string username,string password)
{
// blah blah...
logon = DateTime.Now.ToString() + " - Username " + username + " just logged on";
Form.listBox1.Items.Add(logon);
}
internal bool Logoff(string username)
{
// blah blah...
logoff = DateTime.Now.ToString() + " - Username " + username + " just logged off";
Form.listBox1.Items.Add(logoff);
}
}
而另一个类只是一个带有 listBox1 的主窗体。它设置为公共的,它在同一个命名空间中,只是不同的类。
Form.cs(设计师部分)
partial class Form
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.listBox1 = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.AllowDrop = true;
this.listBox1.FormattingEnabled = true;
this.listBox1.Items.AddRange(new object[] {
"Blah blah...",
"BLAH BLAH!"});
this.listBox1.Location = new System.Drawing.Point(12, 78);
this.listBox1.Name = "listBox1";
this.listBox1.ScrollAlwaysVisible = true;
this.listBox1.SelectionMode = System.Windows.Forms.SelectionMode.MultiSimple;
this.listBox1.Size = new System.Drawing.Size(429, 329);
this.listBox1.TabIndex = 1;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(532, 465);
this.Controls.Add(this.listBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
public System.Windows.Forms.ListBox listBox1;
【问题讨论】: