I have a textbox and two events. The first event selects all text in the textbox if there is any. The second event deselects the text.

My to events...

private void Textbox_enter( object s, EventArgs e )
{
            TextBox box = s as TextBox;
            box.SelectionStart = 0;
            box.SelectionLength = box.Text.Length;
            box.HideSelection = false;
}
 private void Textbox_leave( object s, EventArgs e )
{
            TextBox box = s as TextBox;
            box.HideSelection = true;
}

this works fine when i use my mouse to enter and leave the textbox. Once entered I can not TAB out of that Textbox.

The code below has the same result...

private void Textbox_enter( object s, EventArgs e )
        {
            TextBox box = s as TextBox;
            box.SelectAll();
            box.HideSelection = false;
        }

        private void Textbox_leave( object s, EventArgs e )
        {
            TextBox box = s as TextBox;
            box.DeselectAll();
            box.HideSelection = true;

        }

Please help me to solve this the correct way.

Recommended Answers

All 2 Replies

Try this, unwire all of your existing events and wire up these:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace daniweb
{
  public partial class frmText : Form
  {

    public frmText()
    {
      InitializeComponent();
      textBox1.GotFocus += new EventHandler(textBox1_GotFocus);
      textBox1.LostFocus += new EventHandler(textBox1_LostFocus);
      textBox1.MouseUp += new MouseEventHandler(textBox1_MouseUp);
    }

    void textBox1_MouseUp(object sender, MouseEventArgs e)
    {
      textBox1.MouseDown -= new MouseEventHandler(textBox1_MouseDown);
    }

    void textBox1_GotFocus(object sender, EventArgs e)
    {
      TextBox box = sender as TextBox;
      box.SelectAll();
      textBox1.MouseDown += new MouseEventHandler(textBox1_MouseDown);
    }

    void textBox1_MouseDown(object sender, MouseEventArgs e)
    {
      textBox1.SelectAll();
      textBox1.MouseDown -= new MouseEventHandler(textBox1_MouseDown);
    }

    void textBox1_LostFocus(object sender, EventArgs e)
    {
      TextBox box = sender as TextBox;
      box.DeselectAll();
    }

  }
}

It was pritty simple afterall. The mouse down event did the trick instead of the enter event.

thx scot...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.