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 SharmaPosted Oct 14, 2013, 1:38 AM
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 :)
Sunny SharmaPosted Oct 14, 2013, 3:01 AM
Bineesh ViswanathPosted Oct 14, 2013, 2:44 AM
Sunny SharmaPosted Oct 14, 2013, 2:24 AM
-----------------------------------------------------
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 ViswanathPosted Oct 14, 2013, 2:11 AM
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