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

  • Home
  • SEARCH
  • 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 8667813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:03:53+00:00 2026-06-12T18:03:53+00:00

I tried this code but its not working: private void LoadKeys(Dictionary<string,List<string>> dictionary, string FileName)

  • 0

I tried this code but its not working:

private void LoadKeys(Dictionary<string,List<string>> dictionary, string FileName)
        {

               string line = System.String.Empty;
               using (StreamReader sr = new StreamReader(keywords))
               {
                   while ((line = sr.ReadLine()) != null)
                   {
                       string[] tokens = line.Split(',');
                       dictionary.Add(tokens[0], tokens.Skip(1).ToList());
                       listBox1.Items.Add(new MyListBoxItem(Color.Green, "Url: " + tokens[0] + " --- " + "Localy KeyWord: " + tokens[1]));
                   }
               }

        }

Now the class MyListBoxItem:

public class MyListBoxItem 
        {
            public MyListBoxItem(Color c, string m)
            {
                ItemColor = c; Message = m;
            }
            public Color ItemColor
            {
                get;
                set;
            }
            public string Message
            {
                get;
                set;
            }
        }

And the listBox1_DrawItem event:

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
                    0, // X pixel coordinate
                    e.Index * listBox1.ItemHeight // Y pixel coordinate.  Multiply the index by the ItemHeight defined in the listbox.
                );
            }
            else
            {
                // The item isn't a MyListBoxItem, do something about it
            } 
        } 

In the ListBox before trying this with the colors and draw item i used this line:

listBox1.Items.Add("Url: " + tokens[0] + " --- " + "Localy KeyWord: " + tokens[1]);

The result was for example: Url: http://www.google.com — Localy KeyWord: google

Now when trying to color it this line in Green the color is still black and the text in the listBox is now:

GatherLinks.Form1+MyListBoxItem strange.

What i wanted ot do is to color in the first line in the listBox the Url: in Red the — in blue and the localykeyword: in yellow
In the second line the Url: in Green the — in Red and the value for example google in Blue.

How can i do it ?

  • 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-12T18:03:55+00:00Added an answer on June 12, 2026 at 6:03 pm

    I am doing this in one of my projects. As per Turnkey above I have the lines:

     tasksListBox.DrawMode = DrawMode.OwnerDrawFixed;
      tasksListBox.DrawItem += listBox_DrawItem;
    

    In the initialization code of my form. The code for DrawItem deals with things like the selection color

    private void listBox_DrawItem(object sender, DrawItemEventArgs e)
            {
                if (e.Index == -1)
                {
                    return;
                }
    
                e.DrawBackground();
                Graphics g = e.Graphics;
    
                var selected = (e.State & DrawItemState.Selected) == DrawItemState.Selected;
                var lb = (ListBox)sender;
    
                if (selected)
                {
                    g.FillRectangle(new SolidBrush(Color.DarkBlue), e.Bounds);
                    g.DrawString(lb.Items[e.Index].ToString(), e.Font, new SolidBrush(Color.White), new PointF(e.Bounds.X, e.Bounds.Y));
                    return;
                }
    
                var item = (ProjectToDoRecord)lb.Items[e.Index];
                var textColor = item.TrafficLight();
                g.FillRectangle(new SolidBrush(Color.White), e.Bounds);
                g.DrawString(lb.Items[e.Index].ToString(), e.Font, new SolidBrush(textColor), new PointF(e.Bounds.X, e.Bounds.Y));
            }
    

    The color of each item drawn is determined by textColor. This is set to a color defined in the item being drawn.

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

Sidebar

Related Questions

i have tried this code to redirect a php page.but it s not working
I tried this, but could not get through:- code behind protected HtmlTableRow trComment; protected
I tried to understand this line of code, but it is failed. $this->request->{self::FLAG_SHOW_CONFIG} ===
I have this button click code wich is working now: private void button6_Click(object sender,
I tried to use this code but it doesn't work. http://developer.apple.com/library/ios/#samplecode/PageControl/Introduction/Intro.html Ld /Users/waitonza/Library/Developer/Xcode/DerivedData/Dr_Ngoo-aanknxmuodcgjicaigxevljxokeq/Build/Products/Debug-iphonesimulator/Dr Ngoo.app/Dr
i've tried using this code and this to make a random quote generator, but
I tried to compile Ercia Sadun's sample code here , but this error came
I'm using this code to delete a row in a table, but it's not
This is my Form1 code for rotating and its working the image i can
I have tryed to run this code in my console: script/plugin install git://github.com/apotonick/cells.git ...but

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.