[BUG?] TextBox.Text.Insert(...) -> TextBox.SelectionStart = 0

P

Peter B

Below is a small project that shows how the SelectionStart value changes
when using Insert on a TextBox's Text property.

1. Why is SelectionStart set to 0 when performing the insert?
2. Does it have to do with the nature of strings and the fact that you set
the Text property with the return value of the Insert function?

The only solution I have come up with is to save the position of the caret
(SelectionStart) before performing the insert and then update SelectionStart
with the old position +1 (which could have complications if the textbox has
limited amount of character...).

Code:
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;

namespace SmartDeviceApplication1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.MainMenu mainMenu1;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
this.textBox1.Text = "TheTest";
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.textBox1 = new System.Windows.Forms.TextBox();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(64, 84);
this.textBox1.Text = "textBox1";
this.textBox1.KeyDown += new
System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);
//
// Form1
//
this.Controls.Add(this.textBox1);
this.Menu = this.mainMenu1;
this.Text = "Form1";

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>

static void Main()
{
Application.Run(new Form1());
}

private void textBox1_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
if( e.KeyCode == Keys.F9 )
{
string msg;
TextBox tBox = ((TextBox)sender);

tBox.SelectionStart = 3;
msg = "SelectionStart = " + tBox.SelectionStart.ToString();
MessageBox.Show( msg );
tBox.Text = tBox.Text.Insert( tBox.SelectionStart, "Q" );
msg = "SelectionStart = " + tBox.SelectionStart.ToString();
MessageBox.Show( msg );
}

}
}
}
 
S

Serg Kuryata [MS]

Hello Peter,

This behavior is actually correct. If you run your code against the .NET
Framework, you will see the same result. I believe that this is due to
implementation details of the native control that is used by both
frameworks.

Thank you,
Segiy.


This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
| From: "Peter B" <[email protected]>
| Subject: [BUG?] TextBox.Text.Insert(...) -> TextBox.SelectionStart = 0
| Date: Mon, 1 Mar 2004 12:19:07 +0100
| Lines: 103
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.compactframework
| NNTP-Posting-Host: h113n2fls34o264.telia.com 217.208.187.113
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.compactframework:47283
| X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework
|
| Below is a small project that shows how the SelectionStart value changes
| when using Insert on a TextBox's Text property.
|
| 1. Why is SelectionStart set to 0 when performing the insert?
| 2. Does it have to do with the nature of strings and the fact that you set
| the Text property with the return value of the Insert function?
|
| The only solution I have come up with is to save the position of the caret
| (SelectionStart) before performing the insert and then update
SelectionStart
| with the old position +1 (which could have complications if the textbox
has
| limited amount of character...).
|
| Code:
| using System;
| using System.Drawing;
| using System.Collections;
| using System.Windows.Forms;
| using System.Data;
|
| namespace SmartDeviceApplication1
| {
| /// <summary>
| /// Summary description for Form1.
| /// </summary>
| public class Form1 : System.Windows.Forms.Form
| {
| private System.Windows.Forms.TextBox textBox1;
| private System.Windows.Forms.MainMenu mainMenu1;
|
| public Form1()
| {
| //
| // Required for Windows Form Designer support
| //
| InitializeComponent();
| this.textBox1.Text = "TheTest";
| //
| // TODO: Add any constructor code after InitializeComponent call
| //
| }
| /// <summary>
| /// Clean up any resources being used.
| /// </summary>
| protected override void Dispose( bool disposing )
| {
| base.Dispose( disposing );
| }
| #region Windows Form Designer generated code
| /// <summary>
| /// Required method for Designer support - do not modify
| /// the contents of this method with the code editor.
| /// </summary>
| private void InitializeComponent()
| {
| this.mainMenu1 = new System.Windows.Forms.MainMenu();
| this.textBox1 = new System.Windows.Forms.TextBox();
| //
| // textBox1
| //
| this.textBox1.Location = new System.Drawing.Point(64, 84);
| this.textBox1.Text = "textBox1";
| this.textBox1.KeyDown += new
| System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);
| //
| // Form1
| //
| this.Controls.Add(this.textBox1);
| this.Menu = this.mainMenu1;
| this.Text = "Form1";
|
| }
| #endregion
|
| /// <summary>
| /// The main entry point for the application.
| /// </summary>
|
| static void Main()
| {
| Application.Run(new Form1());
| }
|
| private void textBox1_KeyDown(object sender,
| System.Windows.Forms.KeyEventArgs e)
| {
| if( e.KeyCode == Keys.F9 )
| {
| string msg;
| TextBox tBox = ((TextBox)sender);
|
| tBox.SelectionStart = 3;
| msg = "SelectionStart = " + tBox.SelectionStart.ToString();
| MessageBox.Show( msg );
| tBox.Text = tBox.Text.Insert( tBox.SelectionStart, "Q" );
| msg = "SelectionStart = " + tBox.SelectionStart.ToString();
| MessageBox.Show( msg );
| }
|
| }
| }
| }
|
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top