473,472 Members | 2,241 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

changing TextBox to enabled during runtime

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
Jun 21 '07 #1
6 8350
On Wed, 20 Jun 2007 19:38:56 -0700, Brandon McCombs <no**@none.comwrote:
[...] 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
Jun 21 '07 #2
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/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 21 '07 #3
On Wed, 20 Jun 2007 23:57:09 -0700, Linda Liu [MSFT]
<v-****@online.microsoft.comwrote:
[...]
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
Jun 21 '07 #4
Peter Duniho wrote:
On Wed, 20 Jun 2007 19:38:56 -0700, Brandon McCombs <no**@none.comwrote:
>[...] 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.
Jun 22 '07 #5
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

Jun 22 '07 #6
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

Jun 22 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Tom Rahav | last post by:
Hello. I use visual basic .net 2003 and Crystal Reports for creating win-reports. My question is if there is any possibility to set font properties (type, color and size) of a text control or...
4
by: Weinand Daniel | last post by:
i'd like to monitor changes of the "Control.Name" porperty during designtime. if the user changes the name in designer my event musst fire. i have created a own button control. with an event...
1
by: Jeff N | last post by:
It it possible to change a button's or text box's font size during runtime? When I try to build my application with this code: this.ActiveControl.Font.Size = "8.25"; I get this error message:...
3
by: sean | last post by:
Hi There, I am trying to display some textboxes on my page, When I run the code the textbox is written to the page (when I view source) but not to the screen. Could someone tell me if I have the...
1
by: ross_smith | last post by:
Hi, I created a class in ap.net using vb.net that inherits from Textbox. I added a few of my own properties and methods, set it up to appear in the toolbox. And have used it. My properties that...
32
by: deko | last post by:
I have a popup form with a textbox that is bound to a memo field. I've been warned about memo fields so I'm wondering if I should use this code. Is there any risk with changing the form's...
4
by: Francis | last post by:
Hello i have a continuos form, and i have a command button to help insert information on a control textbox. But when the text box is not null, i dont want the command button to show (to the current...
1
by: ton | last post by:
Hi, I want to add several textbox to my form during runtime: the code is very simple Lab = New TextBox Lab.ID = "A" & i Lab.Height = 200 Lab.Visible = True Lab.Text = "test"
1
by: zergziad | last post by:
Hi All, I really need help to solve my problem. My case is on looping through textbox in gridview. I had a gridview placed in my page. The gridview contains template field which I created it...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.