Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / January 2008

Tip: Looking for answers? Try searching our database.

ComboBox SelectionStart Enter Event Question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mark - 29 Jan 2008 14:53 GMT
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.
Nicholas Paldino [.NET/C# MVP] - 29 Jan 2008 16:10 GMT
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.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> Hello
>
[quoted text clipped - 21 lines]
>
> Thank you for your help.
Mark - 29 Jan 2008 16:27 GMT
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;
   }
}

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.