   GUI using RichTextBox as source
  Has anyone here successfully implemented a textbox using a RichTextBox control the way the CustomUI sample in the SDK does it?I am trying to get this to work and am having troubles with things not scrolling as they should. This is a single line textbox with WordWrap turned off.As I type, when my string gets longer than the textbox everything appears to work fine, letters on the left dissappear to allow the new letters to appear.However, when I scroll back to the left, my cursor goes off of my custom control because the RichTextBox continues to report -X values no matter what I do.I've tried setting the SelectionStart and various other things and I don't get what I want. If I start a WindowsApplication and drop a RTB on the form it works as it should, scrolling left will cause a shift of 4-5 characters when the cursor hits the letter on the left end of the textbox. However, this isn't happening when I use the control with XNA.Here's my version of the PlaceCaret function derived from the CustomUI project to suit my needs.[SOURCE]private void PlaceCaret(int pos)   {  // Store caret position  _caretPosition = pos;  // First find the first visible char  for (int i = 0; i < _richTextBox.Text.Length; i++)  { System.Drawing.Point p = _richTextBox.GetPositionFromCharIndex(i); if (p.X >= 0) {_firstVisible = i; // This is the first visible characterbreak; }  }  // if the new position is smaller than the first visible char   // we'll need to scroll  if (_firstVisible > _caretPosition)  { _firstVisible = _caretPosition;  }  if (_richTextBox.GetPositionFromCharIndex(_caretPosition).X > _richTextBox.Width)  { _richTextBox.ScrollToCaret();  }  if (_richTextBox.GetPositionFromCharIndex(_caretPosition).X < 0)  { _richTextBox.SelectionStart = _caretPosition; _richTextBox.ScrollToCaret();  }   }[/SOURCE]Any thoughts?Thanks for looking,Webby   
  After further testing I find that the HScroll event of the RichTextBox is firing in my PlaceCaret function on the line _richTextBox.ScrollToCaret();This function is called from my Key handler when I enter text, hit left or right arrows, HOME, END, etc.However, only new text is causing the event to fire. When I hit the LEFT arrow my PlaceCaret function is called and the ScrollToCaret() is then called but no event is triggered. Any idea why the event is only called on text entry in my app but in a normal windows app the event is fired as it should (on text entry or when I LEFT past the edge of the control?Thanks,Webby   ;  this maybe a dumb question, but is it possible to just monitor the actual coordinates of your caret and update the text position as it moves?On my GUI engine that's what it does. There is always a "selected character" in an edit control which is a reference to my caret position. so if my edit control is in focus and i press up/down/left/right, backspace and press a key char, the "selected character" index changes and so as my caret position. then i just make sure to update the text position to make sure caret is in a legal position.Have a look at a quick demo i put up.Download ui_demo.zip;  Well, as I was writing code to handle all of these situations it quickly became obvious that my design was getting very bloated by handling all of the cases required to make a fully functional textbox.I'm dealing with SpriteFonts in XNA and so I found myself having to write code making a bunch of assumptions about spacing. If you aren't familiar with SpriteFont then you should know thatSpriteFont.MeasureString("A").X + SpriteFont.MeasureString("B").X does not equalSpriteFont.MeasureString("AB").X. This is because there is a certain amount of spacing placed between each character in a string. This value is available from the SpriteFont object, however, deciding when and when not to apply the spacing is not exactly cut and dry.For instance, say I want to determine if my caret just went left off the textbox. I'd have to do something like this.for(int i = _caretPos; i>0; i--){   if(SpriteFont.MeasureString(_text.Substring(i, _caretPos - i) > _caretX)   { //Do something to move all text to the right}}OR, on my LEFT key handler I would have to do something like{  //Move the caret LEFT one letter.  _caretX -= SpriteFont.MeasureString(_caretPos - 1, 1);  //What about spacing here, do I include it or not? It depends you say?  if(_caretX < 0)  {//Scroll text to the right a certain amount  }}Now, this is only one case, when you take into account the myriad of cases, control left, control right, home, end, etc, etc, you (or at least I did) end up with a big bloated piece of code I wasn't happy with.So, I started looking for other methods and that's when I found the CustomUI sample in the SDK using the RichTextBox to determine what text was visible. Seemed promising, a tried and tested control, all I have to do is poll for some values.Now, for a lot of cases this works. Type text and it will scroll the text to the left so that the new text is visible (and my UI correctly displays this). However, LEFT and RIGHT arrow keys do not seem to be interacting with the RichTextBox control for anything other than movement. I check my position in the RTB control and it's where it's supposed to be, except when it goes below 0 on the X axis, it keeps going.If I make a simple windows app with a RTB and a button, I can set the button to put the caret at position 2 of a string and the RTB scrolls as it should, once it is granted focus. And that's where the issue seems to lie with me. In XNA I can't seem to give the RTB control focus. I can call _myRTBControl.Focus() all day long and nothing happens...Thus, my reason for this post. :)If I can't find a solution for this in another day or so I'll have to revert back to writing all of this logic myself, which would suck...Sorry for the long post, thanks for any info.Webby   