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 6110825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:33:37+00:00 2026-05-23T14:33:37+00:00

I’m using C# WinForms and GDI+ to do something I hoped wouldn’t be too

  • 0

I’m using C# WinForms and GDI+ to do something I hoped wouldn’t be too much problem but…

I’m basically trying to draw a string within a rectangle that has highlighted sections within the string. This all works fine when printing on one line, but I have issues when trying to wrap the text onto the next line within the rectangle.

The algorithm used is as follows: –

Split strings into a collection of highlight and not highlight.

Do

  If Highlightedtext Then

    DrawString(HighLightedText);
    Move X position forward to next character space

  Else

    DrawString(NormalText);
    Move X position forward to next character space

  End If

Loop

I would put the code in, but it’s messy and long (i’m maintaining it). It’ll print out find if the text is one string of either highlighting or not, as it’ll wrap it within the bounds of the rectangle without issue if it’s too long. If it’s multiple highlighting and the string is bigger than the rectangle, it’ll write outside of it… this is because the “move X position forward…” just moves the rectangle on which is a problem!

I want to essentially move the point the text is printed within the original rectangle and print it on the next line if wrapping is required. Can anyone assist with this? It’s a real pain!

  • 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-05-23T14:33:37+00:00Added an answer on May 23, 2026 at 2:33 pm

    I’ve managed to sort this by having to make my function do one character at a time.

    To do this, I made a function to get an array (which is the length of the string itself) of boolean values which have set any highlighted characters to true.

    private bool[] Get_CharacterArray(string text)
        {
            // Declare the length of the array, all set to false
            bool[] characters = new bool[text.Length];
    
            // Get the matching points
            List<Point> wordLocs = FindMatchingTerms(text);
            wordLocs.Sort(PointComparison);
    
            int position = 0;
            foreach (Point loc in wordLocs)
            {
                // We're only setting the array for matched points
                for (position = loc.X; position <= loc.Y; position++)
                {
                    characters[position] = true;
                }
            }
    
            // Return the array
            return characters;
        }
    

    (FindMatchingTerms() is a function that will look in the string and return the matches found into a collection).

    I then loop this array to draw it out to screen but keeping track of my rectangle border width. When it reduces to the relevant size, I reset the position of drawing back to the start and then move the starting Y position down a bit.

    private void RenderFormattedText(Graphics g, RectangleF bounds, string text, string matchText, Font font, Color colour, bool alignTextToTop)
                {
                    const string spaceCharacter = " ";
                    const string hyphenCharacter = "-";
                    Font fr = null;
                    Font fi = null;
                    try
                    {
                        // Get teh matching characters.
                        bool[] charactersMatched = Get_CharacterArray(text);
    
                        // Setup the fonts and bounds.
                        fr = new Font(font.FontFamily, font.Size, FontStyle.Regular);
                        fi = new Font(font.FontFamily, font.Size, FontStyle.Bold | FontStyle.Underline);
                        SizeF fontSize = g.MeasureString(text, fi, 0, StringFormat.GenericTypographic);
                        RectangleF area = bounds;
    
                        // Loop all the characters of the phrase
                        for (int pos = 0; pos < charactersMatched.Length; pos++)
                        {
                            // Draw the character in the appropriate style.
                            string output = text.Substring(pos, 1);
                            if (charactersMatched[pos])
                            {
                                area.X += DrawFormattedText(g, area, output, fi, colour);
                            }
                            else
                            {
                                area.X += DrawFormattedText(g, area, output, fr, colour);
                            }
    
                            // Are we towards the end of the line?
                            if (area.X > (bounds.X + bounds.Width - 1))
                            {
                                // are we in the middle of a word?
                                string preOutput = spaceCharacter;
                                string postOutput = spaceCharacter;
    
                                // Get at the previous character and after character
                                preOutput = text.Substring(pos - 1, 1);
                                if ((pos + 1) <= text.Length)
                                {
                                    postOutput = text.Substring(pos + 1, 1);
                                }
    
                                // Are we in the middle of a word? if so, hyphen it!
                                if (!preOutput.Equals(spaceCharacter) && !postOutput.Equals(spaceCharacter))
                                {
                                    if (charactersMatched[pos])
                                    {
                                        area.X += DrawFormattedText(g, area, hyphenCharacter, fi, colour);
                                    }
                                    else
                                    {
                                        area.X += DrawFormattedText(g, area, hyphenCharacter, fr, colour);
                                    }
                                }
                            }
    
                            // Are we at the end of the line?
                            if (area.X > (bounds.X + bounds.Width))
                            {
                                area.X = bounds.X;
                                area.Y += fontSize.Height + 2;
                            }
                        }
                    }
                    finally
                    {
                        fr.Dispose();
                        fi.Dispose();
                    }
                }
    

    Hopefully someone else will find this useful 🙂 I’ve got some constants in there for spaceCharacter and hypenCharacter which should be self explanatory! There are custom functions to draw the string, but it should make sense nonetheless, hope it helps anyone else.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I am reading a book about Javascript and jQuery and using one of the

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.