Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8338437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:35:30+00:00 2026-06-09T04:35:30+00:00

I found an article that does exactly what I need. It draws multiple colors

  • 0

I found an article that does exactly what I need. It draws multiple colors on the same line on a text box. But the problem is that it was written in VB.NET and I’m writing my program in C#. Any good soul can convert this to C# if it is possible and if it isn’t can you give me other options? Thanks.

This is the article: http://www.vbrad.com/article.aspx?id=34.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T04:35:31+00:00Added an answer on June 9, 2026 at 4:35 am

    First of all, they aren’t using a Textbox in this article, they are using a Listbox but what follows is a conversion of the code from VB.Net to C# like you asked. It needs tidied up a bit but it does the job.

    Just create a new Windows Form, place a Listbox called lstColor onto this form, change the DrawMode property to OwnerDrawFixed inside the properties window, then add event handlers for DrawItem and MeasureItem (you can add event handlers by clicking on the lightning bolt in the Properties window, and double clicking the whitespace beside these two words in the list).

    In the DrawItem event handler, add the following:

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

    In the MeasureItem event handler, add this:

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

    Add five private fields outside the scope of any methods but inside your Form1 (or whatever you’ve called your form) class like so:

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

    Put the following three lines inside your Form1_Load event:

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

    And you are all set.

    Hope this helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I copied come text from a The Huffington Post article and found that the
I'm having trouble grouping a set of nodes. I've found an article that does
I have found article that skims over this. But my main question is do
I found this article but it does not contain support for run as admin..
I recently found an article online that told me about this: RewriteRule ^mock-up/([^/]+)/([^/]+) /mock-up/index.php?page=$1&section=$2
I poked around StackOverflow and Google, but I found articles little too older that
I found something that works with updating one field at here: http://www.karlrixon.co.uk/articles/sql/update-multiple-rows-with-different-values-and-a-single-sql-query/ UPDATE person
I would need to write an Authentication Module for IIS7 that behaves exactly like
Here's an interesting article that I found on the web. It talks about how
Does anyone know what version of Java that RIM modified JVM used ?I found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.