Enabling textboxes with code

cuemasterfl

Supreme [H]ardness
Joined
Jul 5, 2001
Messages
4,181
For the life of me, I don't know why this isn't working (VB). I have a singe line textbox that is disabled by default. When the program is running, and I click it, I want it to become enabled so I can edit the contents. I can't seem to make it enabled when I click it. I've tried .MouseClick and Click.

Code:
Private Sub txtPhrase1_MouseClick(sender As Object, e As MouseEventArgs) Handles txtPhrase1.MouseClick
        txtPhrase1.Enabled = True
    End Sub

Thanks in advance.
 
That's because when a control is disabled it doesn't get mouse events. It seems like a bizarre request.. disable something so it looks like you cannot interact with it, only to have interacting with it allow you to interact with it?!

Anyway, you may be able to catch the mouse events on the control behind it (owner). Translate the coordinates and check if they are within the control and then enable it. But I still probably disagree with what you are doing.

Edit: here's an example which demonstrates...

Code:
public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();

			textBox1.Enabled = false;
		}

		private void textBox1_MouseClick(object sender, MouseEventArgs e)
		{
			MessageBox.Show("txt click");
		}

                // This is the Form's mouse click handler
		protected override void OnMouseClick(MouseEventArgs e)
		{
			base.OnMouseClick(e);

			if (textBox1.Bounds.Contains(e.X, e.Y))
				textBox1.Enabled = true;
		}
	}
 
Last edited:
@ cuemasterfl -- aL_Mac's post will solve your question. But I'm wondering about how obvious the following UI rule would be to a user: "Click within the disabled text field to edit it."

Consider using something more obvious, such as an "edit" button next to the text field. (Alternatively, a LinkLabel control would be appropriate.)
 
Yeah I agree with PTNL that it doesn't seem like a good idea. A CheckBox or something to enable editing is maybe a better approach, but it is hard to say since you didn't describe why you wanted that behavior.

With that being said, I still answered your question because I do not believe in answers that just say "you shouldn't do that" especially when I have no idea what you are really up to..
 
Well here's what I am trying to do. I'm going to make a program that stores commonly used phrases/blocks of text etc. On the left will be a description of the phrase, and there will be a text box on the right that contains the full text. I wanted each description to be 'ghosted' so that you could only edit by clicking the field. When clicked, it would 'light up' and be editable.

You're right though, I should just make an edit button next to it.

If you're familiar with the program Phrase Express, that's kind of what I am aiming for.

Thanks for the input :)
 
Something to consider, though I'm not very familiar with VB/.Net forms so it may or may not apply:

in Java, text components have two properties, enabled and editable. When a component is disabled, it is really disabled and you cannot interact with it at all. When it is uneditable but enabled, you cannot change the text but you can still select and copy the text, which you cannot do if it is disabled. If VB/.Net forms work similarly, you may want to have the text box merely uneditable.
 
Something to consider, though I'm not very familiar with VB/.Net forms so it may or may not apply:

in Java, text components have two properties, enabled and editable. When a component is disabled, it is really disabled and you cannot interact with it at all. When it is uneditable but enabled, you cannot change the text but you can still select and copy the text, which you cannot do if it is disabled. If VB/.Net forms work similarly, you may want to have the text box merely uneditable.

Yes the property is called ReadOnly in .NET, but sounds like he needs to envision the desired effect and write some custom painting code to accomplish that.. maybe drawing a transparent rectangle over it to mask it or something.. whatever it is that is desired.
 
Back
Top