在Windows.Forms.ListBox项右对齐 [英] Right alignment of items in Windows.Forms.ListBox

查看:309
本文介绍了在Windows.Forms.ListBox项右对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种合适的方式来调整项目的权利从.NET Windows.Forms的?

Is there a proper way to align items to the right for ListBoxes from the .net Windows.Forms?

您可以使用

example_box = ListBox()
example_box.RightToLeft = RightToLeft.Yes

这使右对齐,而且打印您的项目从右到左。同样的格式做一些非常奇怪的事情在这里:

This enables the right alignment, but also prints your items right to left. Also the formatting does some really strange things here:

%一二4.72被显示为一二4.72%

'% one two 4.72' gets displayed as 'one two 4.72 %'

有关数字只是这工作得很好,但感觉相当靠不住的。有没有更好的办法呢?

For digits only this works quite well, but feels rather wonky. Are there better solutions?

推荐答案

从右至左的布局是为在阿拉伯和Hewbrew文化,他们写的文字从右到左。这也会影响文本渲染,你找​​到了一个副作用。您需要使用所有者描述得到它,你想要的方式。添加一个新类到您的项目并粘贴下面所示的code。编译。从工具箱顶部的新控件到窗体中。

RightToLeft layout is meant for the Arabic and Hewbrew cultures, they write text right-to-left. It also affects how text is rendered, you found a side-effect. You need to use owner-draw to get it the way you want it. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form.

using System;
using System.Drawing;
using System.Windows.Forms;

class ReverseListBox : ListBox {
    public ReverseListBox() {
        this.DrawMode = DrawMode.OwnerDrawFixed;
    }
    protected override void OnDrawItem(DrawItemEventArgs e) {
        e.DrawBackground();
        if (e.Index >= 0 && e.Index < this.Items.Count) {
            var selected = (e.State & DrawItemState.Selected) == DrawItemState.Selected;
            var back = selected ? SystemColors.Highlight : this.BackColor;
            var fore = selected ? SystemColors.HighlightText : this.ForeColor;
            var txt = this.Items[e.Index].ToString();
            TextRenderer.DrawText(e.Graphics, txt, this.Font, e.Bounds, fore, back, TextFormatFlags.Right | TextFormatFlags.SingleLine);
        }
        e.DrawFocusRectangle();
    }
}

这篇关于在Windows.Forms.ListBox项右对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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