Frage

I tried to align data in listbox using \t but did'nt works for a data that is long

using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
    {
        myDatabaseConnection.Open();
        using (SqlCommand SqlCommand = new SqlCommand("Select ID, LastName from Employee", myDatabaseConnection))
        {
            SqlDataReader dr = SqlCommand.ExecuteReader();
            while (dr.Read())
            {
                listBox1.Items.Add((string)dr["LastName"] + "\t\t" + dr["ID"]);
            }
        }
    }

Result:

1
(source: akamaihd.net)

How to align the data like this in listbox?

2
(source: akamaihd.net)

I have to used listbox instead of using datagridview or listview for some purposes.

War es hilfreich?

Lösung

You should know the exact max length of the last name (designed in your table) and apply the appropriate length, such as + 10. Here I use 50 (for the max length) for demonstrative purpose.

using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
{
    myDatabaseConnection.Open();
    using (SqlCommand SqlCommand = new SqlCommand("Select ID, LastName from Employee", myDatabaseConnection))
    {
        SqlDataReader dr = SqlCommand.ExecuteReader();
        while (dr.Read())
        {
            listBox1.Items.Add(dr["LastName"].ToString().PadRight(50) + dr["ID"]);
        }
    }
}

Sorry, I didn't test it, but as rene said, using Fixed width font would help. However I have another solution using DrawItem, this is incomplete but can get you started, to complete it I think we need more test and custom code:

using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
{
    myDatabaseConnection.Open();
    using (SqlCommand SqlCommand = new SqlCommand("Select ID, LastName from Employee", myDatabaseConnection))
    {
        SqlDataReader dr = SqlCommand.ExecuteReader();
        while (dr.Read())
        {
            listBox1.Items.Add(string.Format("{0}\n{1}",dr["LastName"],dr["ID"]));
        }
    }
}
//DrawItem
//first, set listBox1.DrawMode = DrawMode.OwnerDrawFixed;
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
    {            
        e.DrawBackground();
        string[] ss = listBox1.Items[e.Index].ToString().Split(new char[]{'\n'});
        Rectangle rect = new Rectangle(e.Bounds.Left, e.Bounds.Top, (int) (e.Bounds.Width * 0.5), e.Bounds.Height);
        Rectangle rect2 = new Rectangle((int)(e.Bounds.Width * 0.5), e.Bounds.Top, e.Bounds.Width - (int)(e.Bounds.Width * 0.5), e.Bounds.Height);
        StringFormat sf = new StringFormat() { Alignment = StringAlignment.Near, LineAlignment = StringAlignment.Center };
        e.Graphics.DrawString(ss[0], listBox1.Font, new SolidBrush(listBox1.ForeColor), rect,sf);
        e.Graphics.DrawString(ss[1], listBox1.Font, new SolidBrush(listBox1.ForeColor), rect2, sf);
    } 

As I said, that's just for you to get started, not complete and perfect code to use. For example, I used 50% width of list box for drawing the first 'virtual column' and the remaining for the second 'virtual column'. So that's your part to customize it.

Andere Tipps

you have more flexibility with your columns if you use a ListView like so:

// initialize once (use the designer)
ListView lv = new ListView
    {
        Top = 200, 
        Left = 10, 
        Width = 300, 
        Height = 300,
        View = View.Details // this does the trick for multiple columns
    };
// add two Columns in the designer
lv.Columns.Add(
    new ColumnHeader {Name = "ch1", Text = "Lastname"});
lv.Columns.Add(
    new ColumnHeader { Name = "ch2", Text = "Id" });

            this.Controls.Add(lv);

// once you have that you can add ListViewItems to the view
using (var myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
{
    myDatabaseConnection.Open();
    using (var SqlCommand = new SqlCommand("Select ID, LastName from Employee", myDatabaseConnection))
    {
        var dr = SqlCommand.ExecuteReader();
        while (dr.Read())
        {
            // listBox1.Items.Add((string)dr["LastName"] + "\t\t" + dr["ID"]);
            lv.Items.Add(new ListViewItem(new string[] { dr["LastName"], dr["ID"] }));
        }
    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top