Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Am am trying to display the user selected item in a messagebox.
 
The following command works find.
 
MessageBox.Show(listBox1.SelectedItem.ToString());
 
I would like to suppress part of the Selected Item after a blank space by using the Split Command like this:
string test=listBox1.SelectedItem.ToString().Split(' ');
  MessageBox.Show(test[0]);
 
When attempting to compile this I get the following error message:
 
Error 1 Cannot implicitly convert type 'string[]' to 'string' C:\WindowsFormsApplication6\WindowsFormsApplication6\Form1.cs 41 25 WindowsFormsApplication6
 
Is there another property or method I should be using to do this?
Posted 19-Feb-13 10:02am
Comments
richcb at 19-Feb-13 16:07pm
   
Your variable "test" is a string and you are using it like an array in the message box. Just do this: MessageBox.Show(test);
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Split returns an array which you are trying to set into string test. You almost have it. Combine your two lines of code and do this:
 
string test=listBox1.SelectedItem.ToString().Split(' ')[0];
 
This will return the first item in the array as a string.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

string[] test = myListBox.SelectedItem.ToString().Split(new char[] {' '});
if (test.Length > 0)
   MessageBox.Show(test[0]);
or, in some cases,
string[] test = myListBox.SelectedItem.ToString().Split(new char[] {' '}, System.StringSplitOptions.RemoveEmptyEntries);
if (test.Length > 0)
   MessageBox.Show(test[0]);
 
By the way, pay attention for violation of the naming conventions in listBox1. Auto code generation by designer simply cannot (and should not) do better, but it's your business to rename all auto-generated names to something semantically sensible; never use auto-generated names as is.
 
—SA
  Permalink  
Comments
Jibesh at 19-Feb-13 16:18pm
   
+5. Length checking before accessing the array is always advisable.
Sergey Alexandrovich Kryukov at 19-Feb-13 16:23pm
   
Thank you,
—SA
Sk. Tajbir at 19-Feb-13 16:49pm
   
nice 5+
Sergey Alexandrovich Kryukov at 19-Feb-13 16:59pm
   
Thank you,
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi,
Split function always return array of string. So use:
string[] test=listBox1.SelectedItem.ToString().Split(' ');
 
I hope this will help.
Thanks Smile | :)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

string test=listBox1.SelectedItem.ToString().Split(' ')[0];
 
Works Great!
 
Thank you very much!
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

hello,
 
try
 
string[] test=listBox1.SelectedItem.ToString().Split(' ');
 
the Split() method returns an array so your variable needs to be an array.
 
see here: http://msdn.microsoft.com/en-gb/library/system.string.split.aspx[^]
 
Valery.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web04 | 2.8.141015.1 | Last Updated 19 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100