General Discussion I am trying to retrieve a selected value from a listbox and store it in a string variable using Win API<font class="w_Dgrey_small">I am trying to retrieve a selected value from a listbox and store it in <br>a string  variable.<br></font><font class="w_Dgrey_small">I am using the following code: <br>            int count = 0;<br>            List&lt;string&gt; retVal = new List&lt;string&gt;();<br>            string item;<br>            IntPtr zero = new IntPtr(0);           <br><br>            //Get the count of items in the list<br>            IntPtr ptr = SendMessage(listBox1.Handle, LB_GETCOUNT, zero, zero);<br>            <br>            count = ptr.ToInt32();<br><br>            StringBuilder sbText = new StringBuilder(256 + 1);<br>            SendMessage(listBox1.Handle, LB_GETTEXT, (count - 1), sbText);<br>            string str = sbText.ToString();<br><br>            MessageBox.Show(&quot;Text:&quot; + str.Trim());<br>    </font> <font class="w_Dgrey_small">This does not work and when I look at the value of Text it is <br>    &quot;[0&#14;&#1;9]&quot; and not as I expected.<p>im passing the listbox handle to VC dll, which in turn will post back message to listbox.the problem is &quot;listbox.count&quot; shows me zero.so im using WIN API to retrieve the text from listbox. using LB_GETCOUNT im able to get the counts correctly but using LB_GETTEXT it gives me some junk value.</p></font> © 2009 Microsoft Corporation. All rights reserved.Tue, 27 Jan 2009 23:32:41 Z6e03aa2c-5fde-4d40-a212-ddcaeb1e1938http://social.microsoft.com/Forums/en-US/interopconversations/thread/6e03aa2c-5fde-4d40-a212-ddcaeb1e1938#6e03aa2c-5fde-4d40-a212-ddcaeb1e1938http://social.microsoft.com/Forums/en-US/interopconversations/thread/6e03aa2c-5fde-4d40-a212-ddcaeb1e1938#6e03aa2c-5fde-4d40-a212-ddcaeb1e1938ChandrashekarYalagihttp://social.microsoft.com/Profile/en-US/?user=ChandrashekarYalagiGeneral Discussion I am trying to retrieve a selected value from a listbox and store it in a string variable using Win API<font class="w_Dgrey_small">I am trying to retrieve a selected value from a listbox and store it in <br>a string  variable.<br></font><font class="w_Dgrey_small">I am using the following code: <br>            int count = 0;<br>            List&lt;string&gt; retVal = new List&lt;string&gt;();<br>            string item;<br>            IntPtr zero = new IntPtr(0);           <br><br>            //Get the count of items in the list<br>            IntPtr ptr = SendMessage(listBox1.Handle, LB_GETCOUNT, zero, zero);<br>            <br>            count = ptr.ToInt32();<br><br>            StringBuilder sbText = new StringBuilder(256 + 1);<br>            SendMessage(listBox1.Handle, LB_GETTEXT, (count - 1), sbText);<br>            string str = sbText.ToString();<br><br>            MessageBox.Show(&quot;Text:&quot; + str.Trim());<br>    </font> <font class="w_Dgrey_small">This does not work and when I look at the value of Text it is <br>    &quot;[0&#14;&#1;9]&quot; and not as I expected.<p>im passing the listbox handle to VC dll, which in turn will post back message to listbox.the problem is &quot;listbox.count&quot; shows me zero.so im using WIN API to retrieve the text from listbox. using LB_GETCOUNT im able to get the counts correctly but using LB_GETTEXT it gives me some junk value.</p></font> Fri, 23 Jan 2009 12:00:54 Z2009-01-23T12:00:54Zhttp://social.microsoft.com/Forums/en-US/interopconversations/thread/6e03aa2c-5fde-4d40-a212-ddcaeb1e1938#35c9d434-7343-4b34-9006-0158714d5930http://social.microsoft.com/Forums/en-US/interopconversations/thread/6e03aa2c-5fde-4d40-a212-ddcaeb1e1938#35c9d434-7343-4b34-9006-0158714d5930Bill Wesse MSFThttp://social.microsoft.com/Profile/en-US/?user=Bill%20Wesse%20MSFTGeneral Discussion I am trying to retrieve a selected value from a listbox and store it in a string variable using Win API<p>Hi Chandrashekar, this forum is for software developers who are using the Open Protocol Specification documentation to assist them in developing systems, services, and applications. The Open Protocol Specifications can be found at: <a href="http://msdn2.microsoft.com/en-us/library/cc203350.aspx">http://msdn2.microsoft.com/en-us/library/cc203350.aspx</a>. Since your post does not appear to be related to the Open Protocol Specification documentation set we would appreciate it if you could try the 'Windows SDK' forum at <a href="http://social.msdn.microsoft.com/forums/en-US/windowssdk/threads/">http://social.msdn.microsoft.com/forums/en-US/windowssdk/threads/</a>, or the 'Visual C# Language' forum at <a href="http://social.msdn.microsoft.com/forums/en-US/csharplanguage/threads/">http://social.msdn.microsoft.com/forums/en-US/csharplanguage/threads/</a> instead to find the information you are looking for. Thanks!</p> <p>Regards,<br>Bill Wesse - MSFT<br></p> <hr class=sig> Escalation EngineerFri, 23 Jan 2009 16:41:10 Z2009-01-27T23:32:38Z