I've pasted some example code below.
When you run the code you'll notice Form1 has 2 text boxes and 1 combo box
and textBox1 has focus. Press the TAB key and you'll notice that Sir is
highlighted in textBox2, but when you press TAB again all the text is
highlighted in the ComboBox (even thought ComboBox1_Enter contains the same
code as TextBox2_Enter)
How can I make it select just "Sir" in the ComboBox when it gets focus.
Form1.cs
----------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void comboBox1_Enter(object sender, EventArgs e)
{
if (comboBox1.Text.IndexOf("Dear ") == 0 &&
comboBox1.Text.Substring(comboBox1.Text.Length - 1) == ",")
{
comboBox1.SelectionStart = "Dear ".Length;
comboBox1.SelectionLength = comboBox1.Text.Length -
comboBox1.SelectionStart - 1;
}
}
private void textBox2_Enter(object sender, EventArgs e)
{
if (textBox2.Text.IndexOf("Dear ") == 0 &&
textBox2.Text.Substring(textBox2.Text.Length - 1) == ",")
{
textBox2.SelectionStart = "Dear ".Length;
textBox2.SelectionLength = textBox2.Text.Length -
textBox2.SelectionStart - 1;
}
}
}
}
Form1.Designer.cs
---------------------
namespace WindowsApplication2
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
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.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(48, 13);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.TabIndex = 0;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(48, 40);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(100, 20);
this.textBox2.TabIndex = 1;
this.textBox2.Text = "Dear Sir,";
this.textBox2.Enter += new
System.EventHandler(this.textBox2_Enter);
//
// comboBox1
//
this.comboBox1.FormattingEnabled = true;
this.comboBox1.Items.AddRange(new object[] {
"Dear Sir,",
"Dear Madam,"});
this.comboBox1.Location = new System.Drawing.Point(48, 67);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(121, 21);
this.comboBox1.TabIndex = 2;
this.comboBox1.Text = "Dear Sir,";
this.comboBox1.Enter += new
System.EventHandler(this.comboBox1_Enter);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.comboBox1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.ComboBox comboBox1;
}
}