日期:2014-05-18 浏览次数:20464 次
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 Ex060
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//if (comboBox1.Text != "")
//{
// string newItem=comboBox1.Text.Trim();
// for (int i = 0; i < comboBox1.Items.Count; i++)
// {
// if (string.Compare(newItem, comboBox1.Items[i].ToString()) == 1)
// {
// MessageBox.Show("组合框中已有相同项,不能再添加!");
// return;
// }
// else
// comboBox1.Items.Add(comboBox1.Text.Trim());
// }
//}
}
private void btnAdd_Click(object sender, EventArgs e)
{
if (comboBox1.Text != "")
{
string newItem = comboBox1.Text.Trim();//获取输入文本
bool flag = false;//标识变量
for (int i = 0; i < comboBox1.Items.Count; i++)
{
//判断是否有相同项
if (string.Compare(newItem, comboBox1.Items[i].ToString()) == 0)
{
flag = true;
MessageBox.Show("已经有相同项,不能再添加");
}
}
if (flag == false)
{
comboBox1.Items.Add(newItem);//将输入文本添加到项中
comboBox1.Text = "";
}
}
}
}
}
------解决方案--------------------
这个 是设计界面代码
namespace Ex060
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();