473,406 Members | 2,894 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Enable/Diasble button w/ 2 conditions.

Hi currently with 1 combobox,textbox 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 3274
IanWright
179 100+
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
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
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 Expert 2GB
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
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...
2
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...
3
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...
10
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 =...
3
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...
0
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
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 ...
1
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...
17
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
0
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.