Sir, I need your help to find any control's value empty in a Win Form.
This is the code I seen commonly:-
(controlName).Text==string.Empty || (controlName).Text!=string.Empty
I want the code to check all the controls value in a single code. If have that kind of code, please send the code.
Loading


Sunny SharmaRank #223 · 11.1K reputationPosted Oct 13, 2013, 9:38 PM EDT
The way you're trying to access the value is not OK. How can you be sure if its always a Text Box, it could be anything, for example a Label, a Button, a Web Browser or anything.
So, put a check for specific control type and then try to access it's property otherwise it will raise an error.
Check it as:
----------------------------
foreach(Control ctrl in myForm.Controls)
{
if(ctrl is TextBox)
{
(TextBox) ctrl.Text==string.Empty?"//No Text":"//It has Text"
}
}
---------------------------
Hope it helps :)
Bineesh ViswanathRank #1,681 · 907 reputationPosted Oct 13, 2013, 10:11 PM EDT
Here we go:- 1. (controlName).SelectedValue!=string.Empty
2. (controlName).selectedValue==string.Empty
I need this function in order to check the controls empty or not. If empty, I want to focus to that control. How it possible to focus to the controls in case of there are more than one empty valued controls?
I put forward an idea to focus from index wise. is this possible?. Here how focus would be:-
control index focus
txtName 1 first
txtAge 2 second
txtDepartment 3 Third
Sunny SharmaRank #223 · 11.1K reputationPosted Oct 13, 2013, 10:24 PM EDT
-----------------------------------------------------
Here we go:- 1. (controlName).SelectedValue!=string.Empty
2. (controlName).selectedValue==string.Empty
--------------------------------------------------------------------------
Yes, you may check it like that. Also, as an alternate, you can check for SelectedText also of a combobox if no values are selected.
Now, to answer your second question,
You can NOT Focus on all the controls having invalid values at one time. You can go one by one.
You should go like this:
If the FIRST control has invalid value, then set focus on control and return.
If the SECOND control has invalid value, then set focus on control and return.
If the THIRD control has invalid value, then set focus on control and return.
Hope you got the point :)
Bineesh ViswanathRank #1,681 · 907 reputationPosted Oct 13, 2013, 10:44 PM EDT
Sunny SharmaRank #223 · 11.1K reputationPosted Oct 13, 2013, 11:01 PM EDT