テストリストボックス1.Items.RemoveメソッドはToStringを利用するかgethashCodeを利用して要素を位置決めするか
2564 ワード
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace testCombox
{
public partial class Form1 : Form
{
test t1 = new test("t1", 1);
test t2 = new test("t2", 2);
test t3 = new test("t3", 3);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnAdd_Click(object sender, EventArgs e)
{
this.listBox1.Items.Add(t1);
this.listBox1.Items.Add(t2);
this.listBox1.Items.Add(t3);
}
/// <summary>
/// Object t2
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnDel_Click(object sender, EventArgs e)
{
this.listBox1.Items.Remove(t2);// , listBox1.Items.Remove
// ToString gethashCode
}
/// <summary>
/// listBox1.Items.Remove(object) Object Tostring
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
t2.Name = "t22"; // , 。
this.listBox1.Items.Remove(t2);// , Remove getHashCode
}
}
/// <summary>
///
/// </summary>
public class test
{
public string Name { get; set; }
public override string ToString()
{
return this.Name;
}
public int testInt = 0;
public test(string name, int num)
{
this.Name = name;
testInt = num;
}
}
}
コードのダウンロード