473,396 Members | 1,590 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Combobox contains only item Message box pop up

I am trying to show a Messagebox to come up if the combobox contains only one item and then terminate the program execution when the user dismisses the message box. This is done in Winform.

Expand|Select|Wrap|Line Numbers
  1. private void btn_add_Click(object sender, EventArgs e)
  2.         {
  3.             if (combobox.SelectedIndex != -1)
  4.                 ListBox.Items.Add(combobox.SelectedItem);
  5.             combobox.Items.RemoveAt(combobox.SelectedIndex);
  6.  
  7.  
  8.             else if(combobox.SelectionLength = null) 
  9.  
  10.             MessageBox.Show("ComboBox must have one item left", " Warning", MessageBoxButtons.OKCancel);
  11.         }
Apr 5 '10 #1
9 5364
GaryTexmo
1,501 Expert 1GB
I think you need to tell us what the problem is; however, I will mention that you need to put braces around your if statements.

Expand|Select|Wrap|Line Numbers
  1. private void btn_add_Click(object sender, EventArgs e)
  2. {
  3.   if (combobox.SelectedIndex != -1)
  4.   {
  5.     ListBox.Items.Add(combobox.SelectedItem);
  6.     combobox.Items.RemoveAt(combobox.SelectedIndex);
  7.   }
  8.   else if(combobox.SelectionLength = null)
  9.   {
  10.     MessageBox.Show("ComboBox must have one item left", " Warning", MessageBoxButtons.OKCancel);
  11.   }
  12. }
Apr 5 '10 #2
tlhintoq
3,525 Expert 2GB
TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.
Apr 5 '10 #3
tlhintoq
3,525 Expert 2GB
I have xxx, then do yyy then zzz happens. Here's my code ...
Ok. You know what you have.
What you don't have is a question. Nobody here knows why you posted this since you haven't asked anything. What is it you are looking for?
I recommend you read the FAQ about How to ask a good question so the volunteers will be able to help you.
Apr 5 '10 #4
Thanks Gary for correcting me. I did however, put the braces in there but somehow did not show up here. My question is still unanswered.
Question is I want my program to ensure that the ComboBox contains at least one item. If it does not print a message , using a messagebox, then terminate program execution when the user dismisses the message box. Thanks for all your help guys.

Expand|Select|Wrap|Line Numbers
  1. private void btn_add_Click(object sender, EventArgs e) 
  2.   if (combobox.SelectedIndex != -1) 
  3.   { 
  4.     ListBox.Items.Add(combobox.SelectedItem); 
  5.     combobox.Items.RemoveAt(combobox.SelectedIndex); 
  6.   } 
  7.   else if(combobox.SelectionLength = null) 
  8.   { 
  9.     MessageBox.Show("ComboBox must have one item left", " Warning", MessageBoxButtons.OKCancel); 
  10.   } 
Apr 5 '10 #5
tlhintoq
3,525 Expert 2GB
Question is I want my program to ensure that the ComboBox contains at least one item. If it does not print a message , using a messagebox, then terminate program execution when the user dismisses the message box.
What Gary is saying is : That isn't a question. Its a list of wanted behavior. There is no mention of what you have already coded to accomplish there. There is no mention of errors or exceptions.

The way it reads now, it sounds like you are saying "Please write my code / fix my program for me" and I'm sure that isn't your intent.
Apr 5 '10 #6
I have already accomplish adding and removing an item from the combobox and adding it to the listbox. However, I have failed to get a messagebox to work properly when the combobox contains at least one item. Probably my choice of selectionlength = null is not appropriate. I am new to C sharp.
Apr 5 '10 #7
tlhintoq
3,525 Expert 2GB
Ok... Let me see if I have this correct.
You want to produce the MessageBox if and only if the combo box text entry area is blank. Is that right?
Can you simply change the combobox style from DropDown to DropDownList?
This way there is *no* text area: The user is forced to select one of the .Items from the combo box.

I generally feel it is better to avoid a problem then try to compensate for one.
Apr 5 '10 #8
Combobox must have one last item and a messagebox should pop up indicating to terminate the program if one tries to remove the last item. I wish it would be simple to just use the dropdownlist as you sugggested but its not as simple as that. Thanks for your suggestions
Apr 5 '10 #9
tlhintoq
3,525 Expert 2GB
If there must be at least 1 item, then you should be checking the count of items, not the selection.

Items.Count > 0
Apr 5 '10 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: David Sworder | last post by:
Hi, I have a ComboBox that contains 1,400 sorted items . Here's the list: abba apple ... cabba caccaa capple
4
by: Beeeeeeeeeeeeves | last post by:
I have an ownerdrawn combo box which I am drawing with an image and some text, this is all working beautifully apart from the difference in the Brush I have to draw the background and the text with...
19
by: Bernie Yaeger | last post by:
Everyone misses the point on this - what we need is a combobox autocomplete that is a dropdownlist only. When in dropdown mode, you can enter text - making that autocomplete is trivial. But when...
9
by: Don | last post by:
Is there any way to detect when an item has been added to the Items collection of a combobox or listbox? I am inheriting a Combobox and want to validate items before they are added to the...
7
by: Simon Verona | last post by:
I posted this in dotnet.languages.vb.controls but thought I'd post here as well.. I have a combobox that is bound to a dataview generated from a dataset. The dataset has a single table...
6
by: Matt | last post by:
I'm not entirely sure how to describe this issue. I have a number of ComboBoxes in my application which have their text properties bound to a field in a data set. The items loaded in the ComboBox...
3
by: Magnus | last post by:
Im using a set combobox (ComboBox1) to provide a selection of records from a database table. I have a typed dataset (DataSet1) that contains the typed datatable (DataTable1) that the combobox is...
2
by: spowel4 | last post by:
Here's my code thus far: Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim objxmldoc As New Xml.XmlDataDocument() 'Load all customer numbers...
1
by: Andrus | last post by:
I need to enter null value from combobox to business object property. My combobox datasource does not contain ValueMember with null value. So I tried to create combobox which stores null to bound...
5
by: Kevinp | last post by:
I've spent the last four hours Google searching for a way to disable items in a Combobox. I found one example in C++ which I can't get to work and another in C# that I couldn't get to work either....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.