Solved

Winform combobox validate for empty string

Posted on 2012-04-05
3
1,169 Views
Last Modified: 2012-04-05
I have a winform with 4 comboboxes. I used the IDE and "Items" and added the values manually to the combo box. 3 or 4 values in each combobox.

I open the form and nothing is selected. User has to selected something from the combox box. I have checked for "combo.SelectedValue", "combo.text", "combo.selectedtext", and "combo.selectedindex" . But, even when I select something from the dropdown, the values show as blank. I don't have an event or anything. I click a button and want to validate all combo boxes at the same time...

what do i need to do??
0
Comment
Question by:Camillia
3 Comments
 
LVL 16

Expert Comment

by:Vikram Singh Saini
ID: 37813243
Try combo.SelectedItem.ToString():

private void btnShow_Click(object sender, EventArgs e)
		{
			label1.Text = "Combobox 1: " + comboBox1.SelectedItem.ToString() +
				"Combobox 2: " + comboBox2.SelectedItem.ToString() +
				"Combobox 3: " + comboBox3.SelectedItem.ToString() +
				"Combobox 4: " + comboBox4.SelectedItem.ToString();
		}

Open in new window

0
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 500 total points
ID: 37813460
to validate for empty string just add on click method and use this code:-

public void button1_click(object sender, EventArgs e)
{
if(IsValid())
{
//Button click processing here
}
else
{
//Validation Failure message here
}
}

private bool IsValid()
{
return (cmb1.Text!=String.Empty) && (cmb2.Text!=String.Empty);
}

Open in new window

0
 
LVL 7

Author Comment

by:Camillia
ID: 37813746
let me try
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question