I thought I'd take a stab at my own simple TextBox Control. I'm not quite sure how they're written, but I took a look at the Reference Source for the Windows Forms TextBox, and it's just added a whole lot more questions so I thought I would ask my own questions which are based on how I think a TextBox Control is made:
How are characters/text drawn? Since I'll be creating the TextBox from scratch, I'll obviously need to draw. So knowing that when you draw something in WinForms, you can't just select that text, you need to handle the MouseDown and MouseMove events, get the Location where the mouse is being pressed down, and then determine which, if any, character is at that location. But we can't really do that unless we've saved that character somewhere along with its coordinates. Which means we'll probably need to use a list to store everything that the user types:
List<Character> characters = new List<Character>();
class Character
{
public string Text { get; set; }
public Location { get; set; }
public Size { get; set; }
}
Now that we've got the location of that character, we'll need to draw a filled rectangle so the user knows what they're selecting. We can do that by getting the Size and Location of the character at the coordinates we've determined previously.
Is this basically how a TextBox works?
1) When user types something, we use DrawString to draw what was "typed" and then store its Size and Location in a List for future reference? 2) When the user "selects" text, we lookup the coordinates the user "selected" in the List and then draw a filled rectangle at that Location?
Stringrather than individual characters, and useGraphics.DrawString(...)to do the rendering. The reason is for international characters individually appear different than when combined in a word. That does bring up an interesting question on how to determine the correct caret position.textbox.csis a not a from-scratch project. And it usesTextRendererfor output.