【问题标题】:c# set ListBox to RTL时间:2019-01-10 标签:c#setListBoxtoRTL
【发布时间】:2012-08-17 12:22:26
【问题描述】:

作为在 ListBox 中为文本着色的尝试,我找到了本指南 C# : change listbox items color(我在 Visual Studio 2012 上使用 Windows 窗体应用程序)。 代码正在工作,但问题是我想在从右到左模式下使用文本框,但是当我在 ListBox 设置中更改它时它不起作用,所以我假设它需要以某种方式在代码中更改,这就是我需要你帮助的原因。 非常感谢! 阿隆

【问题讨论】:

    标签: c# listbox right-to-left


    【解决方案1】:

    您的 y 位置为 0,因此每次插入消息时它都位于左侧。 要将其放在右侧,您需要重新计算位置。

    看下面的例子。

    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
        }
    }
    

    【讨论】:

    • 不行,如果句子长的话右边显示最后几个字母,否则什么都不显示
    • msdn.microsoft.com/en-gb/library/… 解释了如何获得水平滚动条。
    • 太棒了!它可以工作但并不完美,你能想到任何方法可以使 ListBox 中的文本更清晰吗?看起来很模糊......编辑:如果我将滚动条始终放在属性中,它会显示但不活动......你不能点击它
    • 起初找到了更好的解决RTL问题的办法。 e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; 用于模糊问题。使用滚动条查看其他问题或打开一个新问题。
    【解决方案2】:

    列表框本身是左对齐的,您无法在 UIR 编辑器中更改它。您可以在创建要传递给列表框的字符串时应用适当的对齐元素:请参阅InsertListItem 函数的项目标签参数的在线帮助。所有转义码都将应用于您插入到列表框中的每一行;无法将默认格式样式应用于控件。

    【讨论】:

    • 我并没有真正理解你的答案......我应该怎么做才能使它成为 RTL(如果可能的话)?
    猜你喜欢
    • 2012-09-18
    • 2015-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多