TextBox 比對 ListBox,TextBox 字串去比對 ListBox 是否有這筆資料,如果沒有就加入到 ListBox 內
namespace ListBoxAdd
{
public partial class ListBoxAdd : Form
{
public ListBoxAdd()
{
InitializeComponent();
}
private void btnAdd_Click(object sender, EventArgs e)
{
// 利用 indexof 來判斷使用者輸入是否存在於 ListBox 內
string inputValue = txtInput.Text;
if (lstResult.Items.IndexOf(inputValue) != -1) return;
lstResult.Items.Add(inputValue);
txtInput.Text = "";
}
}
}
- 延伸閱讀
- [C#] 移除 Arrary 中的重覆值
- 參考資料
- 論壇問題出處
![[C#] ListBox 內新增 Item](https://farm3.staticflickr.com/2907/14795918974_fd77d0f4d4_o.jpg)
沒有留言:
張貼留言