473,569 Members | 3,063 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Enable/Diasble button w/ 2 conditions.

8 New Member
Hi currently with 1 combobox,textbo x and a OK button which the button will enabled once my textbox is not empty.
Expand|Select|Wrap|Line Numbers
  1. private void locacodeBox_TextChanged(object sender, EventArgs e)
  2.         {
  3.             TextBox textBox = sender as TextBox;
  4.             if (locacodeBox.Text.Length == 0)
  5.                 locacodeBox.Tag = false;
  6.             else
  7.                 locacodeBox.Tag = true;
  8.             ValidateButton();
  9.         }
  10.  
  11.         private void ValidateButton()
  12.         {
  13.             this.newlocaBtn.Enabled = (bool)locacodeBox.Tag;
  14.         }
But now i want to add the condition where both the combobox and textbox is also not empty then the button will enable...how can i do so?
Feb 13 '09 #1
4 3284
IanWright
179 New Member
A few points:

1) TextBox textBox = sender as TextBox;

You have cast the sender to a TextBox yet you never use it.

2) That looks like a really bad way to use a Tag. Why not have something more like this:

2)

Expand|Select|Wrap|Line Numbers
  1. private void locacodeBox_TextChanged(object sender, EventArgs e)
  2. {
  3.    if((String.IsNullOrEmpty(this.textbox.Text) && String.IsNullOrEmpty(this.locacodeBox.Text) == false)
  4.    {
  5.        this.newlocaBtn.Enabled = true;
  6.    }
  7.    else
  8.    {
  9.       this.newlocaBtn.Enabled = false;
  10.    }
  11. }
  12.  
or

Expand|Select|Wrap|Line Numbers
  1. private void locacodeBox_TextChanged(object sender, EventArgs e)
  2. {
  3.        // Notice the ! to invert the result. If both are not empty...
  4.        this.newlocaBtn.Enabled = !(String.IsNullOrEmpty(this.textbox.Text) && String.IsNullOrEmpty(this.locacodeBox.Text);
  5. }
  6.  
Feb 13 '09 #2
csharp learner
8 New Member
Thanks IanWright for your suggestions.

But currently the button enable is still not based on when the comboBox and twxtBox is not empty.
Feb 16 '09 #3
csharp learner
8 New Member
I've accomplish my goal in this code but I'm sure there is a shorter and easier way to the solution.

Expand|Select|Wrap|Line Numbers
  1.         public Boolean validatetext;
  2.         public Boolean validacombobox;
  3.  
  4.  
  5. private void locacodeBox_TextChanged(object sender, EventArgs e) 
  6. {
  7.     if (textBox1.Text.Length == 0)
  8.         validatetext = false;
  9.     else
  10.         validatetext = true;
  11.     ValidateButton(); 
  12. }
  13.  
  14. private void comboBox1_TextChanged(object sender, EventArgs e)
  15. {
  16.     if (comboBox1.Text.Length == 0)
  17.         validacombobox = false;
  18.     else
  19.         validacombobox = true;
  20.     ValidateButton();
  21. }
  22.  
  23. private void ValidateButton()
  24. {
  25.     if (validacombobox == true && validatetext == true)
  26.         this.button2.Enabled = true; 
  27.     else
  28.         this.button2.Enabled = false; 
  29. }
Feb 16 '09 #4
tlhintoq
3,525 Recognized Expert Specialist
Expand|Select|Wrap|Line Numbers
  1. button2.Enabled = comboBo1.SelectedItem > -1 && string.IsNullOrEmpty(TextBox1.Text);
Feb 16 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
11891
by: cotton_gear | last post by:
Hi, Depending on the user input I need to disbale/enable some of the links in my page. When disbled I need to display the links as normal text with cursor changed to mouse pointer style and when enabled I need to change it back to the normal link. I tried using document.getElementsById('myLink').style but I am getting 'Object doesnt...
2
2295
by: xazos79 | last post by:
Hi All, I've come across the problem of not being able to re-enable a radio button with javascript if its initial state has been disabled in the Page_Load() method of the code behind. Might i add that it behaves fine in Firefox, but IE is unable to re-enable the radio button. IE is fine if the radio button starts in an enabled state.
3
7076
by: James | last post by:
Hello, I am having a problem with a MS Access 2000 Database that I partially helped create. I am recording visits that registered customers make to our store. I have a list of all the elidgible people that can shop, and when I click a button, it records a visit for today in Date/Time format. My problem is that the button that records the...
10
8072
by: Gary Jefferson | last post by:
Suppose I have 3 modules that belong to a project, 'A', 'A.a' and 'B', and each module has its own logger, created with: module1logger = logging.getLogger('project.A') and module2logger = logging.getLogger('project.A.a') and
3
3943
by: Donald A. Fisher | last post by:
Hello. I've been working a vb project and have a form with a button on it that performs some actions after disabling the button when clicked: Code disabling button and starting actions: Private Sub btnCreateExcel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateExcel.Click Me.btnCreateExcel.Enabled = False...
0
1131
MikeJaskov
by: MikeJaskov | last post by:
Hi. I'm working on some project so how to diasble system tray icons and clock or how to hide system tray icons and clock. Many thanks . . .
3
7919
by: =?Utf-8?B?UGxhdGVyaW90?= | last post by:
I have link buttons in a Gridview that, depending on the value in another column (Not the Key column) will need to be disabled. For example, (Column2 has link buttons) Column1 Column2 Books 54 Videos 6 Audio 3
1
950
shek124
by: shek124 | last post by:
Hai to everybody, I have maintained the user rights in my application. when the user logins the application, user right has maintained as session variable. according to session variable , i have to enable / disable the buttons in my page.. i have done at Page_Load Event. but its not worked successfully.. So in which event i...
17
10310
by: govolsbaby | last post by:
Is there a way to leave the button forecolor unchanged when it is disabled? I have multiple buttons on the form and depending on various user inputs, some will or will not be enabled but I'd rather their text not go to gray. Thanks.
0
7703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7930
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8138
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6290
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5228
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3662
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1229
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
950
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.