C# Question

Status
Not open for further replies.

WWS

In Runtime
Messages
274
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.
 
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.
 
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
 
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();

}
}
}
 
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);
 
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?
 
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);


}
}

}

}
}
 
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.
 
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.
 
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.

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.

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.
 
Status
Not open for further replies.
Back
Top Bottom