PC Review


Reply
Thread Tools Rate Thread

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

 
 
Peter B
Guest
Posts: n/a
 
      1st Mar 2004
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 );
}

}
}
}


 
Reply With Quote
 
 
 
 
Serg Kuryata [MS]
Guest
Posts: n/a
 
      2nd Mar 2004
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" <(E-Mail Removed)>
| 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: <O42AT83$(E-Mail Removed)>
| 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 );
| }
|
| }
| }
| }
|
|
|

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
wrong values from TextBox.SelectionStart property mosquito.dotnet Microsoft C# .NET 1 10th Sep 2006 12:13 AM
Insert text into textbox at caret? =?Utf-8?B?QmVuIFIu?= Microsoft VB .NET 4 29th Jun 2006 01:23 PM
RE: Insert text into a TextBox =?Utf-8?B?bWF2aXNhaGRhcw==?= Microsoft Dot NET Framework Forms 1 17th Sep 2004 07:31 PM
Insert text into a TextBox =?Utf-8?B?RGV2ZWxvcGVy?= Microsoft Dot NET Framework Forms 0 16th Sep 2004 10:23 PM
How can I insert text in a textbox automatically? Lee Microsoft Access Forms 4 25th May 2004 09:33 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:18 AM.