同样概述列表框的值在C#与PadLeft功能 [英] Equally outlining listbox values in C# with the PadLeft function

查看:177
本文介绍了同样概述列表框的值在C#与PadLeft功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我跟我的列表框和值列表框中有一段时间了摔跤我碰到的另一个问题绊倒了。

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

我从我的Access数据库获取数据(字段名称和值),并在一个列表框显示它们。我想字段名的值勾勒字段名称的一个等距离的权利(所以他们都低于对方)。我试图使用PadLeft功能这一点,但由于某种原因,这并不勾勒出他们下方对方。我觉得这事做的字段名的长度。

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:

正如你所看到的值的大纲不低于对方。看来,字段名长度影响的填充计数。

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:

作为你可以看到新的代码已在大纲的差异有一定影响,但它仍然没有做它应该。大纲仍然不等于(低于对方)。任何人有一个想法,什么不顺心吗?

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?

推荐答案

要立即解决的问题,只需使用一个固定宽度字体在列表框中

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:

考虑汉斯的点虽然,检查到已经显示多列数据,像ListView控件或一个DataGridView控件。

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

这篇关于同样概述列表框的值在C#与PadLeft功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆