zoukankan      html  css  js  c++  java
  • 列表设计

    列表(ListBox)用来显示一个列表,它让我们使用很少的C#代码就可以在列表中添加或删除列表项。接下来是我在学习时做的一个小项目。

    控件的添加以及其属性的设置我就不具体交代了,献上我的代码吧。

     1 using System;
     2 using System.Collections.Generic;
     3 using System.ComponentModel;
     4 using System.Data;
     5 using System.Drawing;
     6 using System.Linq;
     7 using System.Text;
     8 using System.Windows.Forms;
     9 
    10 namespace ListBoxTest
    11 {
    12     public partial class ListForm : Form
    13     {
    14         public ListForm()
    15         {
    16             InitializeComponent();
    17         }
    18         //添加数据项
    19         private void btnAdd_Click(object sender, EventArgs e)
    20         {
    21             string tmp=textDel.Text;
    22             //判断文本框传入的数据是否为空,不为空就插入列表
    23             if (tmp.Length > 0)
    24             {
    25                 lstTest.Items.Add(tmp);
    26                 textAdd.Text = null;
    27             }
    28             else MessageBox.Show("数据填写错误!");
    29         }
    30 
    31         private void btnDel_Click(object sender, EventArgs e)
    32         {
    33             string tmp=textDel.Text;
    34             //判断文本框传入的数据是否为空,不为空就查询列表中是否有该项数据,有则删除,无则报错
    35             if (tmp.Length > 0)
    36             {
    37                 if (lstTest.Items.Contains(tmp))
    38                 {
    39                     lstTest.Items.Remove(tmp);
    40                     MessageBox.Show(" 删除成功!");
    41                     textAdd.Text = null;
    42                 }
    43                 else
    44                 {
    45                     MessageBox.Show(" 无此项:" + tmp);
    46                     textDel.Text = null;
    47                 }
    48             }
    49             else MessageBox.Show("数据填写错误!");
    50 
    51         }
    52     }
    53 }

    我这里没涉及到数据存储方面的知识,有兴趣的博友可以试试实现存储列表数据功能。

  • 相关阅读:
    js数组去重五种方法
    wm_concat 多行字符串拼接
    ORACLE WITH AS 简单用法
    layui laytpl 语法
    看懂Oracle执行计划
    GIT RM -R --CACHED 去掉已经托管在GIT上的文件
    sourceTree使用教程--拉取、获取
    SourceTree忽略文件和文件夹
    layui table 详细讲解
    利用POI实现下拉框级联
  • 原文地址:https://www.cnblogs.com/Failbs/p/4106334.html
Copyright © 2011-2022 走看看