14 Replies - 2350 Views - Last Post: 24 December 2011 - 11:21 AM Rate Topic: -----

#1 IamTw_  Icon User is offline

  • D.I.C Head

Reputation: 32
  • View blog
  • Posts: 238
  • Joined: 25-February 11

Check textbox on runtime

Posted 20 December 2011 - 08:18 AM

Hi
I'm creating a windows form applictation, and I'm in a situation where I need to check if my textboxes are empty or not. If just one of the two textboxes is empty, then I set the button's Enable to false. So the code I have so far is:
 if (textBox1.Text.Length == 0 || textBox2.Text.Length == 0)
                {
                    CalculateButton.Enabled = false;
                }


So, I need to write code that constantly checks if textBox1 and textBox2 are not empy, and in that case, set the button's Enabled to true. My question is, how do I go about with this? I have tried rewriting my code many times, but since the code is beeing checked on compile time, nothing happens under runtime when I fill in both textboxes.

Thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: Check textbox on runtime

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,355
  • Joined: 02-June 10

Re: Check textbox on runtime

Posted 20 December 2011 - 08:22 AM

You shouldn't be constantly checking anything. That type of polling is very 1980's. Its expensive in CPU cycles and resources.

Just react to events. That's the C# way. So when textbox1 or textbox2 raise their .TextChanged event you can then go to your method that checks their lengths.

Let me also throw in a couple tips:
  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.

  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)

  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like Index, TimeOut, Row, Column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.

  • Try to avoid having work actually take place in GUI control event handlers. It is usually better to have the GUI handler call other methods so those methods can be reused and make the code more readible.
    Spoiler

Was This Post Helpful? 2
  • +
  • -

#3 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4309
  • View blog
  • Posts: 7,457
  • Joined: 08-June 10

Re: Check textbox on runtime

Posted 20 December 2011 - 08:32 AM

As tlhIn`toq says, you'll need to use Events to solve this issue. Look into the Text Changed event on TextBoxes. You can subscribe to this with both of your TextBoxes, and in this, call a method that does what your initial method does.

By the way, you've got the wrong idea about "compile time" and "run time". Your checking is happening at run time. Nothing in the code happens at compile time. Compile time is during compilation. We talk about this in the context of error checking. If something prevents the code from compiling, it's called a "compile time exception". In your case, the code is (of course) executed when the program is run, so it's happening at "run time". It's just happening right away, and not over and over again when you want it to.
Was This Post Helpful? 2
  • +
  • -

#4 IamTw_  Icon User is offline

  • D.I.C Head

Reputation: 32
  • View blog
  • Posts: 238
  • Joined: 25-February 11

Re: Check textbox on runtime

Posted 20 December 2011 - 08:41 AM

Thank you very much sir, those are really useful information for a programmer that isn't too good yet. I'll definately use them.

View PosttlhIn`toq, on 20 December 2011 - 08:22 AM, said:

[*]Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)

}[/code]


Actually, I've been fighting with this. I tried to change the names as I dragged the button into the forms. However, the code still keep names like for example:
private void textBox1_TextChanged(object sender, EventArgs e)
        {
             
        }



Instead of (what I change it to):

private void SurnameBox_TextChanged(object sender, EventArgs e)
        {
             
        }


This annoyed me like hell, and if I tried manually changed the name in the code, the compiler (VS) complains, so in the end I gave up.
Was This Post Helpful? 0
  • +
  • -

#5 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Check textbox on runtime

Posted 20 December 2011 - 08:57 AM

Changing the names in the designer does not change the names of the events that have already been created. You will need to delete them and recreate the events. You could also go to the Event list in the Properties window, right-click --> Reset(or something like that) --> then double click on the event again to have it create another one.
Was This Post Helpful? 1
  • +
  • -

#6 IamTw_  Icon User is offline

  • D.I.C Head

Reputation: 32
  • View blog
  • Posts: 238
  • Joined: 25-February 11

Re: Check textbox on runtime

Posted 20 December 2011 - 09:00 AM

Thanks Curtis for clearing op the difference between runtime and compile time, I clearly had it all wrong.

Regarding the TextChanged() method. What is wrong with this:
 else if (textBox1.TextChanged())


I'm getting an error message (I guess it's a compile time error, right?) saying:

The event 'System.Windows.Forms.Control.TextChanged' can only appear on the left hand side of += or -=

What does this mean, and how will I fix it?
I just want to check if someone has typed in the textBox1, or changed the text.
Was This Post Helpful? 0
  • +
  • -

#7 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4309
  • View blog
  • Posts: 7,457
  • Joined: 08-June 10

Re: Check textbox on runtime

Posted 20 December 2011 - 09:05 AM

It's because it's an Event, not a Method. You can't check things like that. You still have to check the Text property manually.
Was This Post Helpful? 0
  • +
  • -

#8 IamTw_  Icon User is offline

  • D.I.C Head

Reputation: 32
  • View blog
  • Posts: 238
  • Joined: 25-February 11

Re: Check textbox on runtime

Posted 20 December 2011 - 09:08 AM

Jesus, Java is so much easier :D
Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4309
  • View blog
  • Posts: 7,457
  • Joined: 08-June 10

Re: Check textbox on runtime

Posted 20 December 2011 - 09:16 AM

Give me a break. Java is "easier" because you have fewer options. Java is "easier" because you're already familiar with it.

C# and Java's events follow a different model. In C#, events are a special type of Delegate, which is basically a function pointer. You have to understand what's happening. When you subscribe to an event, you give the event the name of a method. That method will be called when the event is triggered. So, when you drag your textbox on the form, you have to either give it the name of an existing method, or allow it to create one for you.

For the record, events and methods can have a many-to-many relationship. One method may be called by multiple events. Also, one event may contain references to multiple methods (although this is not exposed via the GUI builder).

Then, in that method, you do what you need to do. In your case, you need to check if two text boxes aren't empty. So, both of these text boxes can subscribe to the same handler. In the handler (or better, from another method you call from the handler), write the logic to check the textboxes for text. If they both have text, do something. If not, return.

Just as simple as in Java, just a different mechanism.
Was This Post Helpful? 2
  • +
  • -

#10 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,355
  • Joined: 02-June 10

Re: Check textbox on runtime

Posted 20 December 2011 - 09:39 AM

Let's be frank: You took the time to learn java. Then since the syntax looked very similar you just jumped in and started trying to write an application in C#. That didn't work.

Take the time to learn the new language. If you have a solid foundation in java then it won't take long. Do the tutorials and don't get upset when someone says "do the tutorials", thinking you already know it and don't need to. You should be able to breeze through the tutorials and quickly learn the differences between the two languages.




Resources, references and suggestions for new programmers. - Updated Nov 2011
Spoiler

This post has been edited by tlhIn`toq: 20 December 2011 - 09:39 AM

Was This Post Helpful? 1
  • +
  • -

#11 IamTw_  Icon User is offline

  • D.I.C Head

Reputation: 32
  • View blog
  • Posts: 238
  • Joined: 25-February 11

Re: Check textbox on runtime

Posted 20 December 2011 - 10:13 AM

I was just kidding about java beeing easier guys. Was not trying to offend the C# fans. In times of frustration over some code, you will just feel like saying that sometimes.

I made it work, thanks to you guys :-)

Anyway. I ran into another problem:

I have the code below. It has a return type of Int. If it had return type of String (which there is a reason to why it is not), it would return something like this:
String result = "\r\nHours: " + hours + "\r\nMinutes: " + minutes + "\r\nSeconds: " + seconds;

return res;



How would I make it return someting similar when it must return an Int? And is it possible to make a method that returns an int or double to have a String in it?
public int calcTime(double km, double timeSpent)
        {            
            double avgTime = (timeSpent * 60) / km;              

            int hours = (int) (avgTime / 3600);                  
            int minutes = (int) ((avgTime % 3600) / 60);         
            int seconds = (int)(avgTime % 60);
            
            return ?????????;
        }


This post has been edited by IamTw_: 20 December 2011 - 10:25 AM

Was This Post Helpful? 0
  • +
  • -

#12 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4309
  • View blog
  • Posts: 7,457
  • Joined: 08-June 10

Re: Check textbox on runtime

Posted 20 December 2011 - 10:15 AM

Everyone tends to get a bit defensive about their chosen language. If you want to see a real example of this, head over to the Functional Programming subforum and make a negative comment about Clojure. (actually, don't do this)
Was This Post Helpful? 0
  • +
  • -

#13 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 181
  • View blog
  • Posts: 777
  • Joined: 27-October 11

Re: Check textbox on runtime

Posted 20 December 2011 - 10:32 AM

Quick info on how to change the event handler names:

Oh and just a pointer on how to change the names of event handlers:

Go to your form designer, open the generated code, find the button you're looking for, find the 'subscription' to the event:

It looks something like this:
this.button1.Click += new System.EventHandler(this.button1_Click);



And if you want your event handler to be called 'CSharp_Better_than_Java' (:)) ( :devil: ) you can make it like this:

this.button1.Click += new System.EventHandler(this.CSharp_Better_than_Java);



NOTE: You must have a method to handle this event (an event handler) in the form code file.

This post has been edited by RexGrammer: 20 December 2011 - 10:34 AM

Was This Post Helpful? 1
  • +
  • -

#14 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,351
  • Joined: 29-May 08

Re: Check textbox on runtime

Posted 20 December 2011 - 10:39 AM

You can do that so longs as the Type Signature (the types the method takes an d returns) matches what the event expects.
Was This Post Helpful? 1
  • +
  • -

#15 cilaes  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 73
  • Joined: 12-December 11

Re: Check textbox on runtime

Posted 24 December 2011 - 11:21 AM

View PostIamTw_, on 20 December 2011 - 10:13 AM, said:

is it possible to make a method that returns an int or double to have a String in it?


You'd have to convert the int or double into a string and then make your return value a string formatted however you want to.

MSDN ToString Dox
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1