|  |
03-16-2009, 10:51 PM
|
#1 (permalink)
|
Super Techie Join Date: Nov 2005 Posts: 269
| C# Question I am using C# in visual studio 2005 and working on a windows project. I need to get a bunch of data in to a list box for a user. The user would then be able to delete data selecting the row and then double click on the selection.
As far as the data that need to be shown in the list box, I was thinking of creating a text document containing the data and linking the data to the list box. When the project runs the data is automatically shown in the list box.
Is there something other than a list box that should be used?
Please help me get started on this. Thanks.
__________________ 12 Gb GSkill DDR3 Tripple Channel
2 500 Gb SATA II HD
2 BluRay Burners
2 9800GTX+ in SLI
ASUS Rampage II Extreme
Intel Core i7 920 @3.8 GHz |
| |
03-17-2009, 09:00 AM
|
#2 (permalink)
|
Software Developer Join Date: Mar 2006 Location: Columbus, OH Posts: 569
| Re: C# Question You can use whatever control you want. If you have a list of data in your text file, then a list box sounds like a reasonable choice. If it's a paragraph, then no, it doesn't make sense to display it as a list. |
| |
03-17-2009, 05:00 PM
|
#3 (permalink)
|
Super Techie Join Date: Nov 2005 Posts: 269
| Re: C# Question I am new to programming so where do I start to get the list of data into the list box? Also each row of data has multiple colums as well. So I would need to display it like this:
xxxxx yyyyy zzzzz
__________________ 12 Gb GSkill DDR3 Tripple Channel
2 500 Gb SATA II HD
2 BluRay Burners
2 9800GTX+ in SLI
ASUS Rampage II Extreme
Intel Core i7 920 @3.8 GHz |
| |
03-17-2009, 11:52 PM
|
#4 (permalink)
|
Super Techie Join Date: Nov 2005 Posts: 269
| Re: C# Question Here is some code I wrote to populate a list box from a text file but the text file is not populating when executed. At this time I am only testing out how to populate a single colum of data. Can someone tell me what I am doing wrong. Thanks.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace List Box Populate
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
StreamReader objReader = new StreamReader("c:\\data.txt");
string sLine = "";
while (sLine != null)
{
sLine = objReader.ReadLine();
if (sLine != null)
listBox1.Items.Add(sLine);
}
objReader.Close();
}
}
}
__________________ 12 Gb GSkill DDR3 Tripple Channel
2 500 Gb SATA II HD
2 BluRay Burners
2 9800GTX+ in SLI
ASUS Rampage II Extreme
Intel Core i7 920 @3.8 GHz |
| |
03-18-2009, 07:59 AM
|
#5 (permalink)
|
It's all just 1s and 0s Join Date: Jan 2004 Location: in the lab Posts: 4,389
| Re: C# Question you'll want to create a listviewitem. add any sub items. and then add the lvitem to the list view. set the list box in details mode to show multiple columns.
Someone else might have a better way, but this is how i tackled it for a recent project.
I wanted to show a list of files in the list box. column 1 is a counter, column 2 is the url, colmuns three is the status. here's the code Code: do
{
servPath = FolderURL.Peek().ToString();
DirList = FtpDirList(FolderURL.Pop());
Match fi;
List<string> Fname = new List<string>();
try
{
for (int i = 0; i < DirList.Length; i++)
{
fi = FileInfo(DirList[i]);
ListViewItem item = new ListViewItem((lvFiles.Items.Count + 1).ToString());
if (fi.Groups["dir"].Value.Equals("<DIR>"))
{
item.SubItems.Add(servPath + "/" + fi.Groups["name"].Value + "/");
FolderURL.Push(servPath + "/" + fi.Groups["name"].Value);
}
else
item.SubItems.Add(servPath + "/" + fi.Groups["name"].Value);
item.SubItems.Add("FTP");
lvFiles.Items.Add(item);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
} while (FolderURL.Count > 0);
|
| |
03-19-2009, 01:16 AM
|
#6 (permalink)
|
Super Techie Join Date: Nov 2005 Posts: 269
| Re: C# Question I got the data from the text file to load in to the list box with the code listed in my last post. However, when I run the program the data in each row does not line up properly. anyone help on this?
__________________ 12 Gb GSkill DDR3 Tripple Channel
2 500 Gb SATA II HD
2 BluRay Burners
2 9800GTX+ in SLI
ASUS Rampage II Extreme
Intel Core i7 920 @3.8 GHz |
| |
03-19-2009, 02:38 AM
|
#7 (permalink)
|
Super Techie Join Date: Nov 2005 Posts: 269
| Re: C# Question the following code works except for one thing, it populates a list box vertically instead of horizontally. I have posted the code below, can someone tell me how to populate the list box horizontally? The text file contains data in the following format x/y/z and I want to show it in the list box as x y z then start a new row that follows the same format.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
String LB1String = "";
StreamReader objReader = new StreamReader("C:/Data.txt");
{
while (LB1String != null)
{
LB1String = objReader.ReadLine();
String[] txtData;
Char [] Delimiter = new Char []{'/'};
txtData = LB1String.Split(Delimiter, 4);
if ( txtData != null)
{
listBox1.
listBox1.Items.AddRange(txtData);
}
}
}
}
}
__________________ 12 Gb GSkill DDR3 Tripple Channel
2 500 Gb SATA II HD
2 BluRay Burners
2 9800GTX+ in SLI
ASUS Rampage II Extreme
Intel Core i7 920 @3.8 GHz
Last edited by WWS; 03-19-2009 at 02:40 AM.
|
| |
03-19-2009, 10:19 AM
|
#8 (permalink)
|
Software Developer Join Date: Mar 2006 Location: Columbus, OH Posts: 569
| Re: C# Question A list box is used for the purpose of displaying content as a list, which is a vertical structure. When you pass a collection of items to the list box it naturally assumes that each item is a list item. If you don't want that behavior, format your data as one item and then add it to the list. |
| |
03-19-2009, 02:17 PM
|
#9 (permalink)
|
Super Techie Join Date: Nov 2005 Posts: 269
| Re: C# Question In my third post the code listed took each row as one item and placed it in the list box, however not all the columns in the test box lined up properly in the list box. How would I go about lining up data in a list box.
I am learning C# on the run so I am not familiar with all of the language tools. Please let me know if there is a better way.
__________________ 12 Gb GSkill DDR3 Tripple Channel
2 500 Gb SATA II HD
2 BluRay Burners
2 9800GTX+ in SLI
ASUS Rampage II Extreme
Intel Core i7 920 @3.8 GHz |
| |
03-23-2009, 10:54 AM
|
#10 (permalink)
|
Software Developer Join Date: Mar 2006 Location: Columbus, OH Posts: 569
| Re: C# Question Quote: |
In my third post the code listed took each row as one item and placed it in the list box, however not all the columns in the test box lined up properly in the list box. How would I go about lining up data in a list box.
| I was specifically referring to your 5th post, which called the AddRange() method with an array of strings. In that case, each item in the array would be a separate list item. Quote: |
How would I go about lining up data in a list box.
| Typically, you wouldn't because that's not the purpose of a ListBox. If you want columns, use a ListView. Quote: |
I am new to programming......I am learning C# on the run so I am not familiar with all of the language tools. Please let me know if there is a better way.
| The better way is for you to take baby steps and start with the basics. Get a good book and start reading. |
| |  | | Thread Tools | | | | Display Modes | Linear Mode |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | |