Resources for IT Professionals > 論壇首頁 > Interoperability Conversations (Archived) > General Discussion I am trying to retrieve a selected value from a listbox and store it in a string variable using Win API
發問發問
 

一般討論General Discussion I am trying to retrieve a selected value from a listbox and store it in a string variable using Win API

  • Friday, 23 January, 2009 12:00ChandrashekarYalagi 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    I am trying to retrieve a selected value from a listbox and store it in
    a string  variable.
    I am using the following code:
                int count = 0;
                List<string> retVal = new List<string>();
                string item;
                IntPtr zero = new IntPtr(0);          

                //Get the count of items in the list
                IntPtr ptr = SendMessage(listBox1.Handle, LB_GETCOUNT, zero, zero);
               
                count = ptr.ToInt32();

                StringBuilder sbText = new StringBuilder(256 + 1);
                SendMessage(listBox1.Handle, LB_GETTEXT, (count - 1), sbText);
                string str = sbText.ToString();

                MessageBox.Show("Text:" + str.Trim());
       
     This does not work and when I look at the value of Text it is
        "[09]" and not as I expected.

    im passing the listbox handle to VC dll, which in turn will post back message to listbox.the problem is "listbox.count" 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.

所有回覆