这段代码有问题吗?(50分)

  • 主题发起人 帐号不能用了?
  • 开始时间

帐号不能用了?

Unregistered / Unconfirmed
GUEST, unregistred user!
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace WindowsApplication7
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.ListBox listBox2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
ArrayList list1 = new ArrayList();
private System.Windows.Forms.Button button3;
private C1.Win.C1Command.C1MainMenu c1MainMenu1;
private C1.Win.C1Command.C1CommandHolder c1CommandHolder1;
private C1.Win.C1Command.C1CommandLink c1CommandLink1;
private C1.Win.C1Command.C1CommandLink c1CommandLink2;
private C1.Win.C1Command.C1CommandMenu c1CommandMenu1;
private C1.Win.C1Command.C1CommandLink c1CommandLink3;
private C1.Win.C1Command.C1CommandLink c1CommandLink4;
private C1.Win.C1Command.C1CommandControl c1CommandControl1;
private C1.Win.C1Command.C1CommandLink c1CommandLink5;
private C1.Win.C1Command.C1Command c1Command1;
private C1.Win.C1Command.C1CommandLink c1CommandLink6;
private C1.Win.C1Command.C1Command c1Command2;
private C1.Win.C1Command.C1CommandLink c1CommandLink7;
private C1.Win.C1Command.C1CommandMenu c1CommandMenu2;
private C1.Win.C1Command.C1CommandLink c1CommandLink8;
private C1.Win.C1Command.C1CommandLink c1CommandLink9;
private C1.Win.C1Command.C1CommandMenu c1CommandMenu3;
private C1.Win.C1Command.C1CommandLink c1CommandLink10;
ArrayList list2 = new ArrayList();
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.listBox1 = new System.Windows.Forms.ListBox();
this.listBox2 = new System.Windows.Forms.ListBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.c1MainMenu1 = new C1.Win.C1Command.C1MainMenu();
this.c1CommandHolder1 = new C1.Win.C1Command.C1CommandHolder();
this.c1CommandLink1 = new C1.Win.C1Command.C1CommandLink();
this.c1CommandLink2 = new C1.Win.C1Command.C1CommandLink();
this.c1CommandMenu1 = new C1.Win.C1Command.C1CommandMenu();
this.c1CommandLink3 = new C1.Win.C1Command.C1CommandLink();
this.c1CommandLink4 = new C1.Win.C1Command.C1CommandLink();
this.c1CommandControl1 = new C1.Win.C1Command.C1CommandControl();
this.c1CommandLink5 = new C1.Win.C1Command.C1CommandLink();
this.c1Command1 = new C1.Win.C1Command.C1Command();
this.c1CommandLink6 = new C1.Win.C1Command.C1CommandLink();
this.c1Command2 = new C1.Win.C1Command.C1Command();
this.c1CommandLink7 = new C1.Win.C1Command.C1CommandLink();
this.c1CommandMenu2 = new C1.Win.C1Command.C1CommandMenu();
this.c1CommandLink8 = new C1.Win.C1Command.C1CommandLink();
this.c1CommandLink9 = new C1.Win.C1Command.C1CommandLink();
this.c1CommandMenu3 = new C1.Win.C1Command.C1CommandMenu();
this.c1CommandLink10 = new C1.Win.C1Command.C1CommandLink();
((System.ComponentModel.ISupportInitialize)(this.c1CommandHolder1)).begin
Init();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(40, 56);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(120, 88);
this.listBox1.TabIndex = 0;
//
// listBox2
//
this.listBox2.ItemHeight = 12;
this.listBox2.Location = new System.Drawing.Point(280, 64);
this.listBox2.Name = "listBox2";
this.listBox2.Size = new System.Drawing.Size(120, 88);
this.listBox2.TabIndex = 1;
//
// button1
//
this.button1.Location = new System.Drawing.Point(176, 72);
this.button1.Name = "button1";
this.button1.TabIndex = 2;
this.button1.Text = "Add";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(176, 104);
this.button2.Name = "button2";
this.button2.TabIndex = 3;
this.button2.Text = "Del";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(456, 136);
this.button3.Name = "button3";
this.button3.TabIndex = 4;
this.button3.Text = "button3";

// c1MainMenu1
//
this.c1MainMenu1.CommandHolder = this.c1CommandHolder1;
this.c1MainMenu1.CommandLinks.Add(this.c1CommandLink2);
this.c1MainMenu1.CommandLinks.Add(this.c1CommandLink7);
this.c1MainMenu1.CommandLinks.Add(this.c1CommandLink1);
this.c1MainMenu1.Dock = System.Windows.Forms.DockStyle.Top;
this.c1MainMenu1.Location = new System.Drawing.Point(0, 0);
this.c1MainMenu1.Name = "c1MainMenu1";
this.c1MainMenu1.Size = new System.Drawing.Size(648, 19);
this.c1MainMenu1.Text = "c1MainMenu1";
//
// c1CommandHolder1
//
this.c1CommandHolder1.Commands.Add(this.c1CommandMenu1);
this.c1CommandHolder1.Commands.Add(this.c1CommandControl1);
this.c1CommandHolder1.Commands.Add(this.c1Command1);
this.c1CommandHolder1.Commands.Add(this.c1Command2);
this.c1CommandHolder1.Commands.Add(this.c1CommandMenu2);
this.c1CommandHolder1.Commands.Add(this.c1CommandMenu3);
this.c1CommandHolder1.Owner = this;
//
// c1CommandLink1
//
this.c1CommandLink1.Text = "New Command";
//
// c1CommandLink2
//
this.c1CommandLink2.Command = this.c1CommandMenu1;
//
// c1CommandMenu1
//
this.c1CommandMenu1.CommandLinks.Add(this.c1CommandLink4);
this.c1CommandMenu1.CommandLinks.Add(this.c1CommandLink3);
this.c1CommandMenu1.CommandLinks.Add(this.c1CommandLink5);
this.c1CommandMenu1.CommandLinks.Add(this.c1CommandLink6);
this.c1CommandMenu1.CommandLinks.Add(this.c1CommandLink9);
this.c1CommandMenu1.Font = new System.Drawing.Font("Tahoma", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.World, ((System.Byte)(134)));
this.c1CommandMenu1.Name = "c1CommandMenu1";
this.c1CommandMenu1.Text = "File";
//
// c1CommandLink3
//
this.c1CommandLink3.Text = "New Command";
//
// c1CommandLink4
//
this.c1CommandLink4.Command = this.c1CommandControl1;
//
// c1CommandControl1
//
this.c1CommandControl1.Name = "c1CommandControl1";
this.c1CommandControl1.Text = "New";
//
// c1CommandLink5
//
this.c1CommandLink5.Command = this.c1Command1;
this.c1CommandLink5.Delimiter = true;
//
// c1Command1
//
this.c1Command1.Checked = true;
this.c1Command1.Name = "c1Command1";
this.c1Command1.Text = "New Command";
//
// c1CommandLink6
//
this.c1CommandLink6.Command = this.c1Command2;
//
// c1Command2
//
this.c1Command2.Name = "c1Command2";
this.c1Command2.Text = "New Command";
//
// c1CommandLink7
//
this.c1CommandLink7.Command = this.c1CommandMenu2;
//
// c1CommandMenu2
//
this.c1CommandMenu2.CommandLinks.Add(this.c1CommandLink8);
this.c1CommandMenu2.Font = new System.Drawing.Font("Tahoma", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.World, ((System.Byte)(134)));
this.c1CommandMenu2.Name = "c1CommandMenu2";
this.c1CommandMenu2.Text = "New Command";
//
// c1CommandLink8
//
this.c1CommandLink8.Text = "New Command";
//
// c1CommandLink9
//
this.c1CommandLink9.Command = this.c1CommandMenu3;
//
// c1CommandMenu3
//
this.c1CommandMenu3.CommandLinks.Add(this.c1CommandLink10);
this.c1CommandMenu3.Font = new System.Drawing.Font("Tahoma", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.World, ((System.Byte)(134)));
this.c1CommandMenu3.Name = "c1CommandMenu3";
this.c1CommandMenu3.Text = "New Command";
//
// c1CommandLink10
//
this.c1CommandLink10.Text = "sdfsa d";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(648, 293);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.listBox2);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.c1MainMenu1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.c1CommandHolder1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private void Form1_Load(object sender, System.EventArgs e)
{
list1.Add (new listSource ("表1","a"));
list1.Add (new listSource ("表2","b"));
list1.Add (new listSource ("表3","c"));
list1.Add (new listSource ("表4","d"));
list1.Add (new listSource ("表5","e"));
list1.Add (new listSource ("表6","f"));
listBox1.DataSource = list1;
listBox1.DisplayMember ="ItemText";
listBox1.ValueMember ="ItemValue";
}
private void button1_Click(object sender, System.EventArgs e)
{

if (listBox1.SelectedIndex <0)
return;
if (!listBox2.Items .Contains (listBox1.SelectedItem ))
{

list2.Add (list1[listBox1.SelectedIndex]);
//刷新数据源
listBox2.DataSource =null;
listBox2.DataSource = list2;
listBox2.DisplayMember ="ItemText";
listBox2.ValueMember ="ItemValue";

}
}
private void button2_Click(object sender, System.EventArgs e)
{
int index ;
index =listBox2 .SelectedIndex;
if (index<0)
{
MessageBox.Show(this,"没有选中项","信息提示");
return;
}

list2.RemoveAt(index);
listBox2.DataSource =null;
listBox2.DataSource = list2;
listBox2.DisplayMember ="ItemText";
listBox2.ValueMember ="ItemValue";
}
[STAThread]
static void Main()
{
Application.Run (new Form1());

}

}
public class listSource
{
private string Text ;
private string Value ;

public listSource(string Text, string Value)
{
this.Text = Text;
this.Value = Value;
}
public string ItemValue
{
get
{
return Value;
}
}
public string ItemText
{

get
{
return Text ;
}
}
}
}
问题:
增加不存在问题,在删除的时候,第一次删除没问题,再删除的时候不能点listBox2,否则就报溢出,如果不点listBox2,一直删到底都没问题,谢谢各位大侠!
 
又長又。。還要用第三方的東西。。。。
 
又长又丑,哈哈
不过不好意思,第三方控件在这里没用,有用的就这么一点,别的是。net自己加上去的,我也就没删
private void Form1_Load(object sender, System.EventArgs e)
{
list1.Add (new listSource ("表1","a"));
list1.Add (new listSource ("表2","b"));
list1.Add (new listSource ("表3","c"));
list1.Add (new listSource ("表4","d"));
list1.Add (new listSource ("表5","e"));
list1.Add (new listSource ("表6","f"));
listBox1.DataSource = list1;
listBox1.DisplayMember ="ItemText";
listBox1.ValueMember ="ItemValue";
}
private void button1_Click(object sender, System.EventArgs e)
{

if (listBox1.SelectedIndex <0)
return;
if (!listBox2.Items .Contains (listBox1.SelectedItem ))
{

list2.Add (list1[listBox1.SelectedIndex]);
//刷新数据源
listBox2.DataSource =null;
listBox2.DataSource = list2;
listBox2.DisplayMember ="ItemText";
listBox2.ValueMember ="ItemValue";

}
}
private void button2_Click(object sender, System.EventArgs e)
{
int index ;
index =listBox2 .SelectedIndex;
if (index<0)
{
MessageBox.Show(this,"没有选中项","信息提示");
return;
}

list2.RemoveAt(index);
listBox2.DataSource =null;
listBox2.DataSource = list2;
listBox2.DisplayMember ="ItemText";
listBox2.ValueMember ="ItemValue";
}
好像大富翁上。net用的很少?
 
顶部