如何在checkedListBox C#中的同一行上填充两个字符串?

时间:2017-09-29 10:45:09

标签: c#

我有多个字符串如下所示:"MyApp.exe"以及其他类似字符:"0x1234567A"

当然,尺寸会有所不同。

我想将它们排列在checkedListBox

的一行中

我使用以下代码:

processes_checkedListBox.Items.Add(element[1] + element[2], false);
{p> element[1] "MyApp.exe"element[2] "0x1234567A"

因此,如果我应用该代码,结果显然是这样的:

MyApp.exe0x1234567A
MyOtherApp.exe0x1234567B

我试过这个:

processes_checkedListBox.Items.Add(element[1] + element[2].PadLeft(5,' '), false);

此案例的结果如下:

MyApp.exe     0x1234567A
MyOtherApp.exe     0x1234567B

如果我将填充应用于左侧,则结果与第一个相同。 同样,如果我在第一个元素的右侧应用填充。 字体是默认的checkedListBox,没有任何改变, 填充是40个空格,我只是作为一个例子在5以上插入。

理想的结果当然应该是:

MyApp.exe        0x1234567A
MyOtherApp.exe   0x1234567B

我不知道该怎么做,有什么建议吗? (.NET 4.5 Framework和MVS 2015 - Windows窗体应用程序)

稍后编辑

private void Display_Processes_Button_Click(object sender, EventArgs e)
    {
        processes_checkedListBox.Items.Clear();

        string test = File.ReadAllText("test.txt");
        string[] testArray = test.Split('\n');

        for (int i = 3; i < testArray.Length-1; ++i)
        {
            string[] element = testArray[i].Split('|');
            element[1] = element[1].Trim();
            element[2] = element[2].Trim();

            processes_checkedListBox.Items.Add(string.Format("{0,-20} {1,-20}",element[1] , element[2]), false);
        }
    }

这是我的实际代码,我正在从包含以下内容的文件中读取信息:

+----------------------------------------+-----------+
|             Process Name               | PID       |
+----------------------------------------+-----------+
| securityAccessModule.exe               | 0x127F003A|
| CMD.EXE                                | 0x77430012|
| ps.exe                                 | 0x77010062|
+----------------------------------------+-----------+
尽管我已经尝试过您的解决方案,但我仍然无法使其正常运行。我在控制台看到它工作正常。 :(

7 个答案:

答案 0 :(得分:3)

您似乎需要monospaced font

请尝试以下代码:

<VirtualHost *:80>
    ServerName customer.mydomain1.com
    ErrorLog "logs/error_mydomain1_log"
    CustomLog "logs/access_mydomain1_log" common

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^customer.mydomain1.com
    RewriteRule ^(.*) http://customer.mydomain2.com/$1 [P]

</VirtualHost>

<VirtualHost *:80>
    ServerName customer.mydomain2.com
    ErrorLog "logs/error_mydomain2_log"
    CustomLog "logs/access_mydomain2_log" common

    DocumentRoot "/home/tools/apache/htdocs/mydomain2"
    DirectoryIndex index.html
</VirtualHost>

这显示如下:

enter image description here

答案 1 :(得分:2)

String.Format("{0,-20}{1}", element[1], element[2]);

https://msdn.microsoft.com/en-us/library/system.string.format(v=vs.110).aspx

答案 2 :(得分:1)

我认为你希望结果具有相同的总长度,其间的间距不小于5,但越接近5个空格越好。由于这个事实,您需要找到应用名称和hexa的最大总长度。我们假设它是n。在这种情况下,所有文本的长度将为n + 5.对于每对,计算n + 5与文件名和hexa的长度之和之间的差值。结果将是您需要在左右之间放置的空间量。

答案 3 :(得分:1)

您可以使用字符串格式执行此操作:

public function getRecords(){

    $query = $this->db->select('*')
                    ->where("('startdate <' NOW())", NULL, FALSE)
                    ->where("('enddate <' NOW())", NULL, FALSE)
                    ->get();

    if($query->num_rows()>0)
    {
       return $query->result();

    }

    }

这会给你

    var items1 = new []{"MyApp.exe", "0x1234567A" };
    var items2 = new []{"MyAwesomeApp.exe", "0x1234567B"};
    var items3 = new []{ "MyApp3.exe",  "0x1234567C"};

    Console.WriteLine(string.format("{0,-20} {1,-20}", items1));
    Console.WriteLine("{0,-20} {1,-20}", items2);
    Console.WriteLine("{0,-20} {1,-20}", items3);

答案 4 :(得分:0)

以下是使用PadRight

的答案
string s1= "MyApp.exe";
string s2= "0x1234567A";
string s3= "MyOtherApp.exe";
string s4= "0x1234567B";

Console.WriteLine(s1.PadRight(20,' ') + s2);
Console.WriteLine(s3.PadRight(20,' ') + s4);

//melya's answer
Console.WriteLine(String.Format("{0,-20}{1}", s1, s2));
Console.WriteLine(String.Format("{0,-20}{1}", s3, s4));

输出:两者都会导致相同的输出:

 MyApp.exe           0x1234567A
 MyOtherApp.exe      0x1234567B

Fiddle

上查看 @ plya的答案也很好。

答案 5 :(得分:0)

首先你需要索引= 0的find元素,最大长度。此MyOtherApp.exe的最大长度为14个字符。让我们说

var lngth = 14;

然后你需要在所有列表项的第一个元素上执行PadRight(将+ 1添加一个空格):

 processes_checkedListBox.Items.Add(element[1].PadRight(lngth+1,' ') + element[2], false);

PadRight = 5不执行任何操作,因为在此示例中,字符串的总长度将超过5。

答案 6 :(得分:0)

使用标签 \t

 class Program
{
    static void Main(string[] args)
    {
        StringBuilder builder = new StringBuilder();
        List<Entry> entries = new List<Entry>();

        entries.Add(new Entry()
        {
            App = "MyApp.exe",
            Value = "0x1234567A"
        });

        entries.Add(new Entry()
        {
            App = "MyOtherApp.exe",
            Value = "0x1234567B"
        });

        foreach (Entry entry in entries)
        {
            //append multiple tabs here - the longer your appname is.
            builder.AppendLine(string.Format("{0}\t{1}", entry.App, entry.Value));
        }
        Console.Write(builder.ToString());
        Console.ReadLine();
    }
}

class Entry
{
    public string App { get; set; }

    public string Value { get; set; }
}

输出:

MyApp.exe       0x1234567A
MyOtherApp.exe  0x1234567B