我发现了一篇完全符合我需要的文章。它在文本框的同一行上绘制多种颜色。但问题是它是用VB.NET编写的,我用C#编写程序。如果有可能,任何好的灵魂都可以将它转换为C#,如果不可能,你可以给我其他选择吗?感谢。
答案 0 :(得分:2)
这是您发布的Nicolas
的转换如果您需要更改/获取其他任何工作,您需要在最后进行测试..
快乐编码
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);
}
答案 1 :(得分:1)
首先,他们在本文中没有使用文本框,他们使用的是Listbox,但接下来是将代码从VB.Net转换为C#,就像你问的那样。它需要整理一下,但它能完成这项任务。
只需创建一个新的Windows窗体,将名为lstColor的Listbox放在此窗体上,在属性窗口中将DrawMode
属性更改为 OwnerDrawFixed ,然后为 DrawItem添加事件处理程序和 MeasureItem (您可以通过单击“属性”窗口中的闪电添加事件处理程序,然后双击列表中这两个单词旁边的空白区域。)
在 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 :(得分:0)
签出http://converter.telerik.com/它将代码从VB.NET转换为C#,将C#转换为VB.NET。它不适用于复杂的代码,但可能对您有用。