【问题标题】:How to make a ListBox Control Display Added Items Vertically instead of Horizontally?如何使列表框控件垂直而不是水平显示添加的项目?
【发布时间】:2017-07-18 17:38:15
【问题描述】:

我的代码将一个项目添加到 ListBox:

Item anItem;

protected void btnAddItem_Click(object sender, EventArgs e)
{
    List theList = List.GetList();

    if (theList != null)
    {
        thelist.AddItem(anItem);
        Response.Redirect("anotherWebForm.aspx");
    }
 }

现在,我的问题是,每当我向列表框添加多个项目并按如下方式显示该列表框时:

 private void displayList()
 {
      string itemInfo = "";

      for (int x=0; x < anItem.TheList.Count; x++) // this line is working fine.
      {
          itemInfo = itemInfo + anItem.TheList[x].itemName + .... // this line is working fine.
      }

      lbList.Items.Add(itemInfo); //This line adds the next selected item right beside the previous item in a horizontal manner.  I want to display it in a vertical way.
  }

lbList.Items.Add(itemInfo); 在前一个项目旁边添加下一个项目,并且不会换行。我想让它在新行中显示,而不是在输出中水平显示如下:

 Item listbox:
 - item1
 - item2 
 - item3

但我得到的是这个:

 Item listbox:
 - item1- item2- item3

我尝试将 lbList.Item.Add(itemInfo) 放在 For 循环中,但它所做的只是产生以下内容:

  Item listbox:
  - item1
  - item1- item2
  - item1- item2- item3

【问题讨论】:

    标签: c# asp.net webforms listbox controls


    【解决方案1】:

    试试这个。

    private void displayList()
    {
      for (int x=0; x < anItem.TheList.Count; x++) 
      {
          
       string itemInfo = anItem.TheList[x].itemName 
       lbList.Items.Add(itemInfo); 
         
      }
    
    }
    

    【讨论】:

      【解决方案2】:

      在 Form_Load 中,只需输入以下代码。 uxTablesListBox 是列表框名称。但是在绑定函数中,需要绑定包含“TableName”列的数据表。

      这样,您的列表框项目将垂直显示。

          uxTablesListBox.View = View.Details
          uxTablesListBox.GridLines = False
          uxTablesListBox.FullRowSelect = True
          uxTablesListBox.HideSelection = False
          uxTablesListBox.MultiSelect = False
          uxTablesListBox.Columns.Add("TableName", 259)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-11-19
        • 2013-05-26
        • 1970-01-01
        • 2012-11-10
        • 2011-01-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多