changing TextBox to enabled during runtime

  • Thread starter Thread starter Brandon McCombs
  • Start date Start date
B

Brandon McCombs

Hello,

I have a Form that contains some configuration information. One of the
settings is for SSL. There is a checkbox that I want to check to make 2
textboxes un-editable so that a user can type information into the
textboxes. If the SSL checkbox is unchecked the 2 textboxes should
become readonly again.

I have my event handler properly setup to listen for the
CheckStateChanged event. My debug print statement prints out information
as to whether the button is checked or not. The only problem is that the
2 textboxes don't change between writeable and readonly. Any ideas?

thanks
 
[...] The only problem is that the 2 textboxes don't change between
writeable and readonly. Any ideas?

Um. Did you bother to set the Enabled property?

In your description of what you've done, you list several things, and yet
nothing that would actually enable or disable the control. It's not at
all clear that you should have any reason to expect the textbox to "change
between writeable and readonly".

Also, note that there is a difference between "enabled"/"disabled" and
"writeable"/"read-only". A TextBox control can be any combination of
those; in particular, if it's enabled but read-only, you can't change the
text, but you can still interact with the control otherwise (for example,
select text to copy it, scroll around in the control, etc.).

If you want to enable or disable the control, use the Enabled property.
If you want to make the control writeble or read-only, use the ReadOnly
property.

Hope that helps.

Pete
 
Hi Brandon,

Based on my understanding, you have a CheckBox and two TextBoxes on a form.
If the CheckBox is not selected, you'd like to make the two TextBoxes
readonly; otherwise, make the two TextBox editable. If I'm off base, please
feel free to let me know.

Peter has given a good explanation about the conception of Enabled and
ReadOnly. If a TextBox is enabled, i.e. its Enabled property is set to true
and its ReadOnly property is set to true, we can not only type text in the
TextBox, but also select, copy and paste text in it; otherwise, we can do
neither of the above operations.

If a TextBox is enabled, and its ReadOnly property is set to false, we can
only select and copy the text in it.

From your description, you need to set the ReadOnly property of the two
TextBoxes to true to make them readonly.

The following is a sample. It requires that you add a CheckBox and two
TextBoxes on a form.

public partial class Form1 : Form
{
public Form1()
{
this.InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
this.textBox1.ReadOnly = (this.checkBox1.CheckState !=
CheckState.Checked);
this.textBox2.ReadOnly = (this.checkBox1.CheckState !=
CheckState.Checked);
this.checkBox1.CheckStateChanged += new
EventHandler(checkBox1_CheckStateChanged);
}

void checkBox1_CheckStateChanged(object sender, EventArgs e)
{
this.textBox1.ReadOnly = (this.checkBox1.CheckState !=
CheckState.Checked);
this.textBox2.ReadOnly = (this.checkBox1.CheckState !=
CheckState.Checked);
}
}

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
[...]
Peter has given a good explanation about the conception of Enabled and
ReadOnly. If a TextBox is enabled, i.e. its Enabled property is set to
true
and its ReadOnly property is set to true, we can not only type text in
the
TextBox, but also select, copy and paste text in it; otherwise, we can do
neither of the above operations.

If a TextBox is enabled, and its ReadOnly property is set to false, we
can
only select and copy the text in it.

Actually, you have that reversed. When ReadOnly is set to "true", you can
only select and copy the text in the TextBox, while when it is set to
"false", you can do that as well as cut, paste, delete and enter new
text. In either case, as long as the TextBox is enabled, you can
otherwise interact with the control (such as moving the cursor and
scrolling).

Pete
 
Peter said:
[...] The only problem is that the 2 textboxes don't change between
writeable and readonly. Any ideas?

Um. Did you bother to set the Enabled property?

In your description of what you've done, you list several things, and
yet nothing that would actually enable or disable the control. It's not
at all clear that you should have any reason to expect the textbox to
"change between writeable and readonly".

Here is the code:
public void sslButton_CheckStateChanged(object sender, EventArgs e) {
Console.WriteLine(":" + sslButton.CheckState + ":");
if (sslButton.CheckState == CheckState.Checked) {
trustStorePathField.ReadOnly = false;
trustStorePassField.ReadOnly = false;
trustStorePathField.Enabled = true;
trustStorePassField.Enabled = true;
}
else if (sslButton.CheckState.Equals("Unchecked")) {
trustStorePathField.ReadOnly = true;
trustStorePassField.ReadOnly = true;
trustStorePathField.Enabled = false;
trustStorePassField.Enabled = false;
trustStorePathField.Text = "";
trustStorePassField.Text = "";
}
}
Also, note that there is a difference between "enabled"/"disabled" and
"writeable"/"read-only".

Yes I know. In the end I want the fields to be disabled however I added
the ReadOnly properties in the above code to see if those got changed
and they did not. I'll take the ReadOnly lines out when I get the block
of code working properly.

Again, I know the handler is being called and the If statement works
because a print statement inserted at one point in the handler printed
info to the console so it seems that only the enabled/readonly lines are
not taking effect.

After looking at the code Linda posted it seems the error was what I
view as a bug in C# but maybe not. I didn't know there was a
CheckState.Checked and Unchecked constants to use so as you can see in
my code I was just comparing the CheckState value to strings. That
worked fine for "Checked" and since the CheckState would print
"Unchecked" I thought it should work for the else if portion of my
conditional but for some reason it did not. I had to switch the code to
using the CheckedState.Unchecked constant so that the else if portion
was hit. After that the code worked.

thanks Linda for posting that code since that got me on the right track,
just like your last bit of code you posted for me.
 
Hi Peter,

I am terribly sorry for my miswriting!

It should read "If a TextBox is enabled and its ReadOnly property is set to
false, we can not only type text in the TextBox, but also select, copy and
paste text in it. If a TextBox is enabled, and its ReadOnly property is set
to true, we can only select and copy the text in it".

Thank you for pointing out the mistake!

Sincerely,
Linda Liu
Microsoft Online Community Support
 
Hi Brandon,

The CheckBox.CheckState propery is of an enumeration type.

Every enumeration type has an underlying type, which can be any integral
type except char. The default underlying type of the enumeration elements
is int.

As you can see the underlying type of enumeration type is a value type.

When comparing two enumeration values, we can use the operator '==' in C#
or '=' in VB.NET. Alternatively, we can call the Equals method. The
following is the sample code:

if (checkBox1.CheckState == CheckState.Checked)
{...}

if (checkBox1.CheckState.Equals(CheckState.Checked))
{...}

Hope this helps.
If you have any other question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support
 
Back
Top