Solved

Winform combobox validate for empty string

Posted on 2012-04-05
3
1,134 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now