版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cd1202/article/details/51585763
具体实现见最后运行结果。
Form1
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 实例6_8列表框
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
listBox1.Items.Add("C#程序设计");
listBox1.Items.Add("网络技术");
listBox1.Items.Add("大学英语计");
listBox1.Items.Add("大学物理");
}
private void btAdd_Click(object sender, EventArgs e) //“添加”按钮
{
if (!this.listBox1.Items.Contains(textBox1.Text))
listBox1.Items.Add(textBox1.Text);
else
MessageBox.Show("已经存在");
}
private void btDel_Click(object sender, EventArgs e) //"删除"按钮
{
listBox1.Items.Remove(listBox1.Text);
}
private void btEdi_Click(object sender, EventArgs e) //"修改"按钮
{
textBox1.Text = listBox1.Text;
}
private void btOK_Click(object sender, EventArgs e) //"确认修改"按钮
{
listBox1.Items.Insert(listBox1.SelectedIndex, textBox1.Text);
listBox1.Items.Remove(listBox1.Text);
}
private void btExit_Click(object sender, EventArgs e) //"退出"按钮
{
Application.Exit();
}
private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button5_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
Form1.Designer
namespace 实例6_8列表框
{
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.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.listBox1 = new System.Windows.Forms.ListBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(204, 41);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(76, 25);
this.button1.TabIndex = 1;
this.button1.Text = "添加";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.btAdd_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(204, 82);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(76, 23);
this.button2.TabIndex = 2;
this.button2.Text = "删除";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.btDel_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(204, 123);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(76, 24);
this.button3.TabIndex = 3;
this.button3.Text = "修改";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.btEdi_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(204, 165);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(76, 24);
this.button4.TabIndex = 4;
this.button4.Text = "修改确定";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.btOK_Click);
//
// button5
//
this.button5.Location = new System.Drawing.Point(205, 207);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(75, 25);
this.button5.TabIndex = 5;
this.button5.Text = "退出";
this.button5.UseVisualStyleBackColor = true;
this.button5.Click += new System.EventHandler(this.btExit_Click);
//
// listBox1
//
this.listBox1.FormattingEnabled = true;
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(19, 75);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(144, 160);
this.listBox1.TabIndex = 6;
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(19, 31);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(144, 21);
this.textBox1.TabIndex = 7;
this.textBox1.Text = "C#设计教程";
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.button5);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.TextBox textBox1;
}
}
program.ca
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new 实例6_8列表框.Form1());
}
}
}
运行结果:
(1)原始:
(2)添加按钮
(3)删除按钮
(4)修改
(5)修改确定
总结:
这个程序我用了一节上机课的时间才弄出来,对很多问题总觉得知道到底要干啥却不知道具体怎么做,多练习。