public static bool pathFits(string path, TextBox text) { string tempPath = text.Text; int prefLoc = text.Right - text.Location.X; text.Text = path + "[]"; int currentLoc = text.GetPositionFromCharIndex(text.Text.Length - 1).X; text.Text = tempPath; return currentLoc < prefLoc; }
///for System.Windows.Forms.TextBox public static System.Drawing.Point GetCaretPoint(System.Windows.Forms.TextBox textBox) { int start = textBox.SelectionStart; if (start == textBox.TextLength) { start--; } return(textBox.GetPositionFromCharIndex(start)); }
/// <include file='doc\ToolStripTextBox.uex' path='docs/doc[@for="ToolStripTextBox.GetPositionFromCharIndex"]/*' /> public System.Drawing.Point GetPositionFromCharIndex(int index) { return(TextBox.GetPositionFromCharIndex(index)); }
/// <summary> /// Make sure that the end of the text in the given text box is visible. /// An (undesired) side effect is to focus the box and put the selection at the end of it. /// I cannot find any portable way to achieve the desired scrolling without doing this. /// </summary> /// <param name="textBox"></param> private void MakeEndOfTextVisibleAndFocus(TextBox textBox) { if (textBox.Text.Length == 0) return; // It would seem logical that we would not want the -1, so we would be asking for the position of the // imaginary character at the very end. However, that just always returns (0,0). Point endPosition = textBox.GetPositionFromCharIndex(textBox.Text.Length - 1); if (endPosition.X > textBox.Width) { textBox.Focus(); textBox.Select(textBox.Text.Length, 0); textBox.ScrollToCaret(); } }
public void RetrieveHotSpots_NoHotspotsReturned_NoHotSpotsVisible() { using (TextBox textBox = new TextBox()) { textBox.Text = "Now is the time."; _hotSpotProvider.SetEnableHotSpots(textBox, true); _hotSpotProvider.RetrieveHotSpots += delegate { // give back no hot spots }; //if we scan the entire text for hot spots we shouldn't find any for (int i = 0;i != textBox.Text.Length;++i) { Point position = textBox.GetPositionFromCharIndex(i); List<HotSpot> hotSpots = new List<HotSpot>( _hotSpotProvider.GetHotSpotsFromPosition(textBox, position)); Assert.AreEqual(0, hotSpots.Count); } } }
[NUnit.Framework.Category("KnownMonoIssue")] // review: WS-???? public void RetrieveHotSpots_GiveSomeHotspots_HotSpotsVisible() { using (TextBox textBox = new TextBox()) { textBox.Text = "Now is the time for all good men to come to the aid..."; _hotSpotProvider.SetEnableHotSpots(textBox, true); _hotSpotProvider.RetrieveHotSpots += delegate(object sender, RetrieveHotSpotsEventArgs e) { e.AddHotSpot(new HotSpot(e.Control, 7, 3)); e.AddHotSpot(new HotSpot(e.Control, 16, 3)); }; Point position = textBox.GetPositionFromCharIndex(8); List<HotSpot> hotSpots = new List<HotSpot>( _hotSpotProvider.GetHotSpotsFromPosition(textBox, position)); Assert.AreEqual(1, hotSpots.Count); Assert.AreEqual(7, hotSpots[0].Offset); Assert.AreEqual("the", hotSpots[0].Text); position = textBox.GetPositionFromCharIndex(16); hotSpots = new List<HotSpot>( _hotSpotProvider.GetHotSpotsFromPosition(textBox, position)); Assert.AreEqual(1, hotSpots.Count); Assert.AreEqual(16, hotSpots[0].Offset); Assert.AreEqual("for", hotSpots[0].Text); } }
/// ------------------------------------------------------------------------------------ /// <summary> /// Processes down key when a grid cell is in the edit mode. This overrides the default /// behavior in a grid cell when it's being edited so using the down arrow will move the /// IP down one line rather than moving to the next row. /// </summary> /// ------------------------------------------------------------------------------------ protected virtual bool ProcessDownKey(TextBox txtBox) { // Don't override the default behavior if all the text is selected or not multi-line. if (txtBox.SelectedText == txtBox.Text || !txtBox.Multiline) return false; int chrIndex = txtBox.SelectionStart; Point pt = txtBox.GetPositionFromCharIndex(chrIndex); pt.Y += TextRenderer.MeasureText("x", txtBox.Font).Height; var proposedNewSelection = txtBox.GetCharIndexFromPosition(pt); if (proposedNewSelection <= chrIndex) return false; // Don't let "down" take you *up*. (See SP-220.) txtBox.SelectionStart = proposedNewSelection; return true; }
/// ------------------------------------------------------------------------------------ /// <summary> /// Processes up key when a grid cell is in the edit mode. This overrides the default /// behavior in a grid cell when it's being edited so using the up arrow will move the /// IP up one line rather than moving to the previous row. /// </summary> /// ------------------------------------------------------------------------------------ protected virtual bool ProcessUpKey(TextBox txtBox) { // Don't override the default behavior if all the text is selected or not multi-line. if (txtBox.SelectedText == txtBox.Text || !txtBox.Multiline) return false; int selectionPosition = txtBox.SelectionStart; // Getting the position after the very last character doesn't work. if (selectionPosition == txtBox.Text.Length && selectionPosition > 0) selectionPosition--; Point pt = txtBox.GetPositionFromCharIndex(selectionPosition); if (pt.Y == 0) return false; pt.Y -= TextRenderer.MeasureText("x", txtBox.Font).Height; txtBox.SelectionStart = txtBox.GetCharIndexFromPosition(pt); return true; }