2012-08-06 34 views

回答

1

首先,他們沒有在這篇文章中使用Textbox,他們使用的是ListBox,但接下來的內容是將代碼從VB.Net轉換爲C#,就像你問的那樣。它需要整理一下,但它完成了這項工作。

只需創建一個新的Windows窗體,一個叫lstColor列表框放到這種形式下,DrawMode屬性更改爲OwnerDrawFixed屬性窗口內,然後添加事件處理程序DRAWITEMMeasureItem(你可以添加事件處理程序通過單擊屬性窗口中的閃電,然後雙擊列表中這兩個單詞旁邊的空格)。

DRAWITEM事件處理程序,添加以下內容:

private void lstColor_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) 
{ 
    var size = g.MeasureString(data[e.Index], oFont, 500, sf); 
    var width = size.Width + 16; 

    e.DrawBackground(); 
    e.DrawFocusRectangle(); 
    e.Graphics.DrawString(data[e.Index], oFont, new SolidBrush(color[e.Index]), e.Bounds.X, e.Bounds.Y); 
    e.Graphics.DrawString(data[data.Length - 1 - e.Index], oFont, new SolidBrush(color[color.Length - 1 - e.Index]), width, e.Bounds.Y); 
} 

MeasureItem事件處理程序,補充一點:

private void lstColor_MeasureItem(object sender, MeasureItemEventArgs e) 
{ 
    var size = g.MeasureString(data[e.Index], oFont, 500, sf); 
    var height = size.Height; 

    e.ItemHeight = Convert.ToInt32(height); 
} 

添加的任何範圍之外的五個私營領域方法,但在你的Form1(或任何你稱爲你的表格)類如下:

private string[] data; 
private Color[] color; 
private Font oFont; 
private Graphics g; 
private StringFormat sf; 

把下面三行您Form1_Load的事件中:

private void Form1_Load(object sender, EventArgs e) 
{ 
    oFont = new Font("Arial", 10); 

    data = new string[] { "This is Red", "This is Blue", "This is Green", "This is Yellow", "This is Black", "This is Aqua", "This is Brown", "This is Cyan", "This is Gray", "This is Pink" }; 
    color = new Color[] {Color.Red, Color.Blue, Color.Green, Color.Yellow, Color.Black, Color.Aqua, Color.Brown, Color.Cyan, Color.Gray,Color.Pink}; 
    lstColor.DataSource = data; 
    g = Graphics.FromHwnd(lstColor.Handle); 
    sf = new StringFormat(StringFormat.GenericTypographic); 
} 

而你所有的設置。

希望這有助於

2

這裏是你貼什麼薩科

如果您需要更改/獲取別的工作,你將需要測試它在你的最終轉換..

快樂編碼

private void MeasureItemHandler(object sender, MeasureItemEventArgs e) 
{ 
    Graphics g = Graphics.FromHwnd(lstColor.Handle); 
    StringFormat sf = new StringFormat(StringFormat.GenericTypographic); 
    SizeF size = default(SizeF); 
    float height = 0; 
    Font oFont = new Font("Arial", 10); 

    //measure the height of what you are about to draw 
    //and let the listbox know this 
    size = g.MeasureString(data(e.Index), oFont, 500, sf); 
    height = size.Height + 5; 
    e.ItemHeight = height; 
} 

private void DrawItemHandler(object sender, DrawItemEventArgs e) 
{ 

    Graphics g = Graphics.FromHwnd(lstColor.Handle); 
    StringFormat sf = new StringFormat(StringFormat.GenericTypographic); 
    SizeF size = default(SizeF); 
    float width = 0; 
    Font oFont = new Font("Arial", 10); 


    //get the width of the string you are about to write 
    //this info is needed so that we can offset the next 
    //string that will be drawn in a different color. 
    size = g.MeasureString(data(e.Index), oFont, 500, sf); 
    width = size.Width + 16; 

    //prepare the list for drawing 
    e.DrawBackground(); 
    e.DrawFocusRectangle(); 

    //draw the first string in a certain color 
    e.Graphics.DrawString(data(e.Index), oFont, new SolidBrush(color(e.Index)), e.Bounds.X, e.Bounds.Y); 

    //draw the second string in a different color 
    e.Graphics.DrawString(data(data.Length - 1 - e.Index), oFont, new SolidBrush(color(color.Length - 1 - e.Index)), width, e.Bounds.Y); 
} 
相關問題