17893

Arrange 2 List to list box

Question:

i have 2 List that i want to put into my Listbox the first List contain names and the second contain numbers my problem is that some of the names long so the numbers cannot a display in the same line how can i put in in appropriate way ?

listBox.Items.Add("Name" + "\t\t\t" + "Number"); for (int i = 0; i < lists.Count; i++) { listBox.Items.Add(lists._namesList[i] + "\t\t\t" + lists._numbersList[i]); }

<strong>Update</strong> here is what I tried with a ListView

listViewProtocols.View = View.Details; listViewProtocols.Columns.Add("Name"); listViewProtocols.Columns.Add("Number"); for (int i = 0; i < lists._protocolsList.Count; i++) { listViewProtocols.Items.Add(new ListViewItem{ lists._nameList[i], lists._numbersList[i].ToString()}); }

Answer1:

Consider using a ListView component, with Details style. As @Yuck mentioned in the comments it will give you the effect you need.

<img alt="ListView example" class="b-lazy" data-src="https://i.stack.imgur.com/DqOdz.png" data-original="https://i.stack.imgur.com/DqOdz.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

It is a bit akward to populate from 2 separate lists but it is doable with the code below:

listView1.View=View.Details; listView1.Columns.Add("Name"); listView1.Columns.Add("Number"); string[] names= { "Abraham", "Buster", "Charlie" }; int[] numbers= { 1018001, 1027400, 1028405 }; for(int i=0; i<names.Length; i++) { listView1.Items.Add( new ListViewItem(new string[] { names[i], numbers[i].ToString() })); } <hr />

I would strongly recommend doing an array of structures instead of separate lists like this:

public struct Record { public string name; public int number; public string[] ToStringArray() { return new string[] { name, number.ToString() }; } }

and used like this:

listView1.View=View.Details; listView1.Columns.Add("Name"); listView1.Columns.Add("Number"); Record[] list=new Record[] { new Record() { name="Abraham", number=1018001 }, new Record() { name="Buster", number=1027400 }, new Record() { name="Charlie", number=1028405 } }; for(int i=0; i<list.Length; i++) { listView1.Items.Add( new ListViewItem(list[i].ToStringArray())); }

Answer2:

There are couple of options I can think of:

<ol><li>Make the listbox wider so that it can accomodate longer text or add a horizontal scrollbar to it.</li> <li>Constrain the max length of names to, let's say, 20 chars and replace extra characters with .... </li> <li>Probably the best solution is to use grid instead of listbox - you need to display two columns of data, which is exactly the grid is for.</li> </ol>

Recommend

  • HoverItem returing null in ListView
  • .NET: is there a Click-and-drag “Desktop-Like” control?
  • Listview selection color
  • Bind events to Item ViewModel
  • Android ListFragment update/refresh and onItemClick
  • Load Project Settings from .NET Assembly
  • Binding a list with another list with WPF
  • Fancy dynamic list in Android: TableLayout vs ListView
  • React Native fetch is not a function
  • UWP change ComboBox position when opening
  • Aptana 3 remove bundle (jquery)
  • Syntax error on tokens, AnnotationName expected instead - error on query
  • Lock Horizontal View
  • Hash Code in SQL Server?
  • WPF version of .ScaleControl?
  • How to pass a value from ASP.NET MVC controller to ASP.NET webforms control inside MVC View?
  • Application level floating views with navigation in Android
  • ASP.NET MVC Application won't update some controllers
  • android.support.v7.widget.Toolbar VectorDrawableCompat IllegalStateException when using support lib
  • How can I enlarge video fullscreen without the affected interface project in as3?
  • Webgrid not refreshing after delete MVC
  • Custom Tabgroup Appcelerator
  • Disable Enter in editText android
  • Android fill_parent issue
  • Control modification in presentation layer
  • Email format validation in mvc3 view
  • Projection media query: browser support and workarounds?
  • Android screen density dpi vs ppi
  • Deselecting radio buttons while keeping the View Model in synch
  • Deleting and Updating values from a cusrsor adapter
  • Volley JsonObjectRequest send headers in GET Request
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • Javascript convert timezone issue
  • Cannot Parse HTML Data Using Android / JSOUP
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Getting Messege Twice Using IMvxMessenger
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • apache spark aggregate function using min value
  • How to load view controller without button in storyboard?