簡體   English   中英

如何在Winform C#中將文本框數據顯示到具有特定索引的列表視圖中?

[英]How to display textbox Data in to listview with particular index in winform C#?

button click使用特定索引將text box數據顯示到list view控件中

if (lvone.SelectedItems != null) {
  ListViewItem item = lvone.SelectedItems[0];
  item.SubItems[1].Text = textboxone.Text;
}

我想我明白了您要的是什么。 秘密在於,您需要a column for each SubItem要顯示的a column for each SubItem

  • 復制/粘貼下面的代碼
  • 運行
  • 在文本框中輸入“ key0”,然后按Enter
  • 再次嘗試相同的密鑰,或嘗試其他密鑰...

我建議您將問題的標題更改為類似“ How to display SubItems in ListView以便它更能代表您所要詢問的內容。

using System;
using System.Drawing;
using System.Windows.Forms;

namespace ListViewSubItems
{
    public partial class Form1 : Form
    {

        TextBox txtbx = new TextBox();//this will be the textbox in which the user will type something
        ListView lv = new ListView();//this will be our listview
        Button btn = new Button();//this will be our button
        Button btnChange = new Button();
        public Form1()
        {
            InitializeComponent();
            InitializeOurStuff();
            AddInitalEntriesAgain();
        }

        /// <summary>
        /// this is where the magic happens
        /// </summary>
        private void AddSubItems()
        {
            if (string.IsNullOrEmpty(txtbx.Text))
            {
                //nothing to do
                return;
            }

            /*
             * You can add SubItems all you want. 
             * But you'll never see them unless you have column
             * foreach of the SubItem you whish to show
             * To do so, you need to keep track of 
             * how many SubItems are listed per row,
             * so that you append the SubItem to the right column,
             * and so that you add a column only when needed
             */


            if (lv.Items.ContainsKey(txtbx.Text))
            {
                lv.Items[txtbx.Text].SubItems.Add("coolbeans");
                lv.Items[txtbx.Text].SubItems.Add("COOLBEANS");

                /*
                 * Again, if you don't have a column to host
                 * your SubItem, then your SubItem will simply not show up
                 * Having said that, now add as many columns as you need
                 */
                while ((lv.Columns.Count) < lv.Items[txtbx.Text].SubItems.Count)
                {
                    lv.Columns.Add("subcol" + lv.Columns.Count);
                }
            }
        }

        /// <summary>
        /// In this method, we simply update a SubItem
        /// </summary>
        private void UpdateTheSubItem()
        {
            if (lv.Items.ContainsKey(txtbx.Text))
            {
                if (lv.Items[txtbx.Text].SubItems.Count > 1)
                {
                    lv.Items[txtbx.Text].SubItems[1].Text = "you just got replaced! " + txtbx.Text;
                }
            }
        }

        /// <summary>
        /// For the purpose of this snippet
        /// Everything on the form is created in here
        /// NOTHING created using Designer
        /// </summary>
        private void InitializeOurStuff()
        {
            //set some textbox properties
            txtbx.Location = new Point(5, 5);
            txtbx.KeyUp += Txtbx_KeyUp;

            //set some ListView properties
            lv.Location = new Point(txtbx.Location.X, txtbx.Location.Y + txtbx.Height + 5);
            lv.HideSelection = false;
            lv.View = View.Details;
            lv.Width = this.Width - 20;

            //set some button properties
            btn.Location = new Point(lv.Location.X, lv.Location.Y + lv.Height + 5);
            btn.Text = "Add SubItms";
            btn.Click += Btn_Click;

            btnChange.Location = new Point(btn.Location.X + btn.Width + 5, btn.Location.Y);
            btnChange.Text = "Change SubItm";
            btnChange.Click += BtnChange_Click;

            //add the controls to the forms
            this.Controls.Add(txtbx);
            this.Controls.Add(lv);
            this.Controls.Add(btn);
            this.Controls.Add(btnChange);
        }

        private void BtnChange_Click(object sender, EventArgs e)
        {
            UpdateTheSubItem();
        }

        /// <summary>
        /// keyboard navigation for keyboard junkies
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Txtbx_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Btn_Click(null, null);
            }
            else if (e.KeyCode == Keys.Q && e.Modifiers == Keys.Control)
            {
                this.Close();
            }
        }

        /// <summary>
        /// I assume you have your own way of populating the listview
        /// though don't forget to add the initial column
        /// </summary>
        private void AddInitalEntriesAgain()
        {
            lv.Columns.Add("maincol");
            for (int i = 0; i < 5; i++)
            {
                lv.Items.Add("key" + i, "value" + i, null);
            }
        }

        /// <summary>
        /// the button click event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_Click(object sender, EventArgs e)
        {
            AddSubItems();
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM