Validation of ComboBox (unbound?)

  • Thread starter Thread starter John Sitka
  • Start date Start date
J

John Sitka

Winforms

DataTable comboBoxsaleordersData = new DataTable();
comboBoxsaleordersData.Columns.Add("salesorder", typeof(string));
DataRow drow;
cn.Open();
SqlDataReader rdr = cmd.ExecuteReader();
if ( rdr != null)
{
while (rdr.Read())
{
// insert rows/values to DataTable comboBoxsaleordersData
drow = comboBoxsaleordersData.NewRow();
drow[0] = Convert.ToString(rdr["fsono"]);
comboBoxsaleordersData.Rows.Add(drow);
//at the same time build the combobox
comboBoxsaleorders.Items.Add(rdr["fsono"]);
}
}
comboBoxsaleorders.SelectedIndex = 0;
rdr.Close();



Behavior desired...

In the interface much of the dataentry is done from the numeric keypad.
So I need what is typed in the text part of the combo box to be validated
against comboBoxsaleordersData or the Items collection of the
comboBoxsaleorders.
Validation should occur on "enter" or "tab". Loose focus I guess.

Thanks.
 
Hi John,

Based on my understanding, you want to do validating for your WinForm
combobox control, when the user pressed "Enter" or "Tab" keys.

Normally, you should hook into the ComboBox's KeyDown event and do the
validation, I have writen a sample for you like this:

private void comboBox1_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
bool valid=false;
if(e.KeyCode==Keys.Enter|| e.KeyCode==Keys.Tab)
{
string txt=this.comboBox1.Text;
foreach (object obj in this.comboBox1.Items)
{
string str=obj as string;
if(str.Equals(txt))
{
valid=true;
}
}

if(valid==false)
{
MessageBox.Show("You entered an invalid value!!");
this.comboBox1.Text="";
}
}
}

If you still want to do validation when the combobox lose focus, you also
should hook its LostFocus event.

=========================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Sweetness. Got to go rebuild an SQL Server right now but that example was
crystal clear.
Thanks, that was the direction I needed. Like I mentioned I didn't know
whether to
compare against the comboBox1.Items Collection or the
DataTable..OR...instantiate some yet
unknown to me .NET Class <PowerValidator> with constructors identifing the
comboBox1 that
would handle such things.

J.
 
Hi John,

Thanks very much for your feedback.

I am glad my reply can help you. Yes, in my sample, I ignored the Database
data retrieving operation, but that is not the obstacle. :-)

To loop through datatable to do validation, you can loop through all the
rows, do like this:

DataTable dt=new DataTable();
dt.Columns.Add("column", typeof(string));
foreach(DataRow dr in dt.Rows)
{
if(((string)dr["column"]).Equals(txt))
{
valid=true;
}
}

If you still need help, please feel free to post, I will help you. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top