Nicolas
Nicolas

Reputation: 2386

Equally outlining listbox values in C# with the PadLeft function

I am wrestling with my listbox and values in the listbox for some time now and I stumbled across another problem.

I am fetching data from my Access database (both the field names and the values) and displaying them in a listbox. I am trying to outline the values of the field names to the right of the field name on a equal distance (so they are all below each other). I am trying to use the PadLeft function for this but for some reason this doesn't outline them below each other. I think this has something to do with the Length of the field names.

The result what i am getting with this line of code:

try
            {

                connection.Open();
                OleDbCommand command = new OleDbCommand();
                command.Connection = connection;
                string query = "select * from Sparen WHERE Id=1";

                command.CommandText = query;
                OleDbDataReader reader = command.ExecuteReader();

                var columns2 = listBox6;
                while (reader.Read())
                {
                    for (int i = 0; i < reader.FieldCount; i++)
                    { 
                        columns2.Items.Add(reader.GetName(i) + reader.GetValue(i).ToString().PadLeft(15));       
                    }
                }

                connection.Close();
            }

Is this: Listbox example

As you can see the outlining of the values aren't below each other. It seems that the field name length affects the padding count.

So I thought I could fix this by subtracting the padding with the length of the field name like this:

try
            {

                connection.Open();
                OleDbCommand command = new OleDbCommand();
                command.Connection = connection;
                string query = "select * from Sparen WHERE Id=1";

                command.CommandText = query;
                OleDbDataReader reader = command.ExecuteReader();
                int x;
                var columns2 = listBox6;
                while (reader.Read())
                {
                    for (int i = 0; i < reader.FieldCount; i++)
                    {
                        x = reader.GetName(i).Length;                        
                        columns2.Items.Add(reader.GetName(i) + reader.GetValue(i).ToString().PadLeft(15 - x));                    
                    }
                }
                connection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error" + ex);
            }

The result is the following:

listbox example 2

As you can see the new code has had some effect on the outlining difference but it still didn't do what it was supposed to. The outlining is still not equal (below each other). Anyone has an idea what goes wrong here?

Upvotes: 0

Views: 712

Answers (2)

rory.ap
rory.ap

Reputation: 35328

What you're experiencing is a result of the fact that the font is not "fixed width", so some characters are wider than other characters.

The list box control is not really designed to display multiple columns, which is really what it seems like you are looking for here. I would suggest you use a System.Windows.Forms.DataGridView instead.

Upvotes: 1

Grant Winney
Grant Winney

Reputation: 66501

To fix your immediate issue, just use a fixed-width font in the ListBox:

listBox1.Font = new Font(FontFamily.GenericMonospace, listBox1.Font.Size);

Now your padding logic will work as you expect:

enter image description here

Consider Hans's point though; check into controls that already display multiple columns of data, like the ListView or a DataGridView.

Upvotes: 1

Related Questions