62,046
社区成员
发帖
与我相关
我的任务
分享
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();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.btnAdd = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(40, 53);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(29, 12);
this.label1.TabIndex = 0;
this.label1.Text = "年级";
//
// comboBox1
//
this.comboBox1.FormattingEnabled = true;
this.comboBox1.Items.AddRange(new object[] {
"2004级",
"2005级"});
this.comboBox1.Location = new System.Drawing.Point(93, 53);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(121, 20);
this.comboBox1.TabIndex = 1;
this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
this.comboBox1.TextChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
//
// btnAdd
//
this.btnAdd.Location = new System.Drawing.Point(93, 99);
this.btnAdd.Name = "btnAdd";
this.btnAdd.Size = new System.Drawing.Size(75, 23);
this.btnAdd.TabIndex = 2;
this.btnAdd.Text = "添加";
this.btnAdd.UseVisualStyleBackColor = true;
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(265, 156);
this.Controls.Add(this.btnAdd);
this.Controls.Add(this.comboBox1);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "根据输入增加选项";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.Button btnAdd;
}
}
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 = "";
}
}
}
}
}