ComboBox SelectionStart Enter Event Question

M

Mark

Hello

I've created a .NET 2.0 form and added 6 text boxes, 1 combo box and 2
button controls.

The combo box has a DropDownStyle of DropDown, so the user can still type in
the combo box.

The combo box has the follows values in it's drop down list:
"Dear Sir,"
"Dear Madam,"

I want the text between "Dear " and "," to be highlighted when the combo box
gets focus.

I've tried adding code into the ComboBox Enter event to try and force it to
select just the text between "Dear " and "," using the SelectionStart and
SelectionLength properties, but it keeps selecting all the text.

How can I just select the text between "Dear " and "," in my ComboBox?

Thank you for your help.
 
N

Nicholas Paldino [.NET/C# MVP]

Mark,

Well, could you post a complete example which details the problem? We
could make guesses as to what you are doing, but we might not be anywhere
close.
 
M

Mark

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

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