Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,352 members, 7,808,229 topics. Date: Thursday, 25 April 2024 at 09:09 AM

Help With This:checking For Blank Textboxes In C# - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Help With This:checking For Blank Textboxes In C# (3472 Views)

Creating UWP Application In C# Or C++ (xaml) / Build A JAMB Result Checking Website To Win 150,000 Naira / [problem] Write A Program In C++ That Finds The Hcf Of 2 Numbers Without Using A Recursive Function (2) (3) (4)

(1) (Reply) (Go Down)

Help With This:checking For Blank Textboxes In C# by Lakulos(m): 7:45pm On Aug 20, 2012
I am currently learning C# programming and i am using visual studio 2008.I created two textboxes and a submit button,i want a messagebox to popup telling the user to enter a value in the textbox whenever the submit button is clicked with the texboxes empty.I have tried to solve this but am encontering errors.The two error messages are "cannot implicitly convert type bool to int" and "operator '<' cannot be applied to operands of type int and bool".This is my code,please it might not be well arranged beacause am typing it with my phone.


Private void button1_click(object sender, eventargs e)
{
int answer=10;
bool start=false;
bool end=false;

start=int.tryparse(textbox1.text, out answer);

end=int.tryparse(textbox2.text,out answer);

if (!start)
{
messagebox.show("enter numbers in the textboxes"wink;
}
else
{
for(int i=start; i<end; i++)
{
answer=answer*i;

listbox1.items.add(i+"*"+answer+"="+answer.tostring());
}
}.

Thanks in advance
Re: Help With This:checking For Blank Textboxes In C# by naijaswag1: 12:09am On Aug 21, 2012
Though I do Java not C#.The principles are the same.You are trying to convert String values to boolean. I can see that you are trying to read those values and use them as the initial and end values of a for-loop.Check if your textboxes can be constrained to accepting on a set of characters.I use swt(eclipse gui framework) most times in Java,so I normally add a verifylistener to ensure only numbers or any set of characters I want are entered.After that,you have to read the entered numbers as String and parse it to int and use in your for-loop.The error you are getting is trying to convert String to boolean(true or false).They are not the same data type and moreover implicit conversion cannot even take place between Strings and boolean.
Re: Help With This:checking For Blank Textboxes In C# by gistme24(m): 12:11am On Aug 21, 2012
If(TextBox1.Text.Lenght == 0)
////
}
Or
If(TextBox1.Text.Equals(""))
{
}

Or
If(TextBox1.Text = String.Empty)
{
}
Any of this should do perferctly
Re: Help With This:checking For Blank Textboxes In C# by Lakulos(m): 12:56am On Aug 21, 2012
Thanks for the reply,what if i want to limit the textbox characters to only integers,that is i only want integer numbers to be entered in the textbox.How will i do that?
Re: Help With This:checking For Blank Textboxes In C# by Kobojunkie: 4:28am On Aug 21, 2012
Lakulos: Thanks for the reply,what if i want to limit the textbox characters to only integers,that is i only want integer numbers to be entered in the textbox.How will i do that?
I believe in the property window, there is a sort of input Mask property that you can use or use the Validation rule property.
Re: Help With This:checking For Blank Textboxes In C# by gistme24(m): 6:59am On Aug 21, 2012
Like kobojunkie said, one of the properties of the textbox or one of the validation controls gives you what you want. But try:

try{
int val = Convert.ToInt32(TextBox1.Text);
}
Catch(Exception ex)
{
MessageBox.Show("wrong input" ) ;

}
Re: Help With This:checking For Blank Textboxes In C# by Lakulos(m): 11:29am On Aug 21, 2012
I have tried the codes but none of it is working,i also did not see the input mark and validation rule in the property window.
Re: Help With This:checking For Blank Textboxes In C# by gistme24(m): 11:41am On Aug 21, 2012
Sorry bro... The earlier written were for Java. My codes have been modified.

int val = Convert.ToInt32(TextBox1.Text);

Not Integer.Parse which is for java.
The above should work perfectly.

1 Like

Re: Help With This:checking For Blank Textboxes In C# by Lakulos(m): 12:44pm On Aug 21, 2012
gistme24: Sorry bro... The earlier written were for Java. My codes have been modified.

int val = Convert.ToInt32(TextBox1.Text);

Not Integer.Parse which is for java.
The above should work perfectly.


sory to bother you but am a bit confused,d program is running but say format exception unhandled and it highlighted d "int start=convert.ToInt32(textbox1.text)" in yellow.Also do i still need to use a conditional statement to execute dis program? Pls bear with me,dis is d first programming language am learning
Re: Help With This:checking For Blank Textboxes In C# by gistme24(m): 12:54pm On Aug 21, 2012
Let's say:
If(TextBox1.Text.Lenght > 0)
{
Try{
Int stat = Convert.ToInt(TextBox1.Text) ;
}
Catch(Exception ex)
{
MessageBox.Show("wrong input" ) ;
}
}
Else
MessageBox.show("Field cannont be blank" ) ;


If ur still having that exception, copy and paste ur codes here let's see.
Re: Help With This:checking For Blank Textboxes In C# by Lakulos(m): 1:09pm On Aug 21, 2012
gistme24: Let's say:
If(TextBox1.Text.Lenght > 0)
{
Try{
Int stat = Convert.ToInt(TextBox1.Text) ;
}
Catch(Exception ex)
{
MessageBox.Show("wrong input" ) ;
}
}
Else
MessageBox.show("Field cannont be blank" ) ;


If ur still having that exception, copy and paste ur codes here let's see.


Its now working,thanks very much,i really appreciate your time.

(1) (Reply)

How To Connect Vb.net Windows Application To Php And My Sql Database / Beginning Microsoft C#.net / I'm About To Start Applying For Remote Jobs. Any Tips?

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 20
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.