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 );
}
}
}
}
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 );
}
}
}
}