简体   繁体   English

C#将ListBox设置为RTL

[英]c# set ListBox to RTL

As an attempt to color text in ListBox i found this guide C# : change listbox items color (i am using windows forms application on visual studio 2012). 为了给ListBox中的文本着色,我找到了本指南C#:更改listbox项目的颜色 (我在Visual Studio 2012中使用Windows窗体应用程序)。 The code is working but the problem is that i want to use the textbox in a Right to Left mode, but when i change it in the ListBox settings it does not work, so i assume that it needs to be changed in the code somehow, this is what i need your help for. 该代码正在运行,但是问题是我想在从右到左的模式下使用该文本框,但是当我在ListBox设置中对其进行更改时,它不起作用,因此我认为需要以某种方式在代码中对其进行更改,这就是我需要您的帮助。 Thank you very much! 非常感谢你! Alon 阿隆

Your y position is 0, so everytime you insert a message it's on the left side. 您的y位置为0,因此每次您插入一条消息都在左侧。 To put it on the right side you have recalculate the postion. 要把它放在右边,您已经重新计算了位置。

Look at the following example. 看下面的例子。

private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
    MyListBoxItem item = listBox1.Items[e.Index] as MyListBoxItem; // Get the current item and cast it to MyListBoxItem
    if (item != null)
    {
        e.Graphics.DrawString( // Draw the appropriate text in the ListBox
                   item.Message, // The message linked to the item
                   listBox1.Font, // Take the font from the listbox
                   new SolidBrush(item.ItemColor), // Set the color 
                   width - 4, // X pixel coordinate
                   e.Index * listBox1.ItemHeight,
                   new StringFormat(StringFormatFlags.DirectionRightToLeft)); // Y pixel coordinate.  Multiply the index by the ItemHeight defined in the listbox.                
    }
    else
    {
        // The item isn't a MyListBoxItem, do something about it
    }
}

A list box is natively left-justified and you cannot change this in the UIR editor. 列表框本身是左对齐的,您不能在UIR编辑器中更改它。 You can apply the appropriate justification elements while creating the string to pass to the list box: see the online help for Item Label parameter of the InsertListItem function. 您可以在创建要传递到列表框的字符串时应用适当的对齐元素:请参见InsertListItem函数的Item Label参数的联机帮助。 All escape codes are to be applied to every line that you insert into the list box; 所有转义码都将应用于您插入列表框中的每一行; there is no way to apply a default formatting style to the control. 无法将默认格式样式应用于控件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM