CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    May 2011
    Posts
    3

    How to get value...

    Q1)How to get the current value from a listbox? (for e.g name of listbox is listbox1)
    I am trying text1=tlistbox1.text .........but it gives error

  2. #2
    Join Date
    Dec 2008
    Posts
    144

    Re: How to get value...

    Welcome to the forum. You need to give us specific details about your problems- if you reference an error we need to know the exact contents of the error message. Otherwise we probably won't be able to help.
    Code:
    if (Issue.Resolved)
    {
         ThreadTools.Click();
         MarkThreadResolved();
    }

  3. #3
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: How to get value...

    Hi and welcome,

    You can always use MSDN to look up documentation on elements of the C# library. In particular, this link should be useful:

    http://msdn.microsoft.com/en-us/libr...tbox.text.aspx

    As you typed it, it looks like perhaps you are not paying close attention to case and spelling (both of which matter). Try:

    Code:
    string text1 = listbox1.Text
    If problems persist, please provide more information on the error your are encountering.
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  4. #4
    Join Date
    May 2011
    Posts
    3

    Re: How to get value...

    I am trying...

    string sh1=sh2=sh3= null;
    sh1="may month" ;
    sh2 = listBox1.Text;
    sh3=sh1 +"Year is is:"+sh2;

    but I am not getting the value from sh2(listbox value). Also, by default value from the listbox is string type or value type?

  5. #5
    Join Date
    May 2011
    Posts
    5

    Re: How to get value...

    I think somewhere on that page can help you.

    http://www.homeandlearn.co.uk/csharp/csharp_s3p7.html

  6. #6
    Join Date
    Jun 2010
    Posts
    56

    Re: How to get value...

    I believe he/she wants the selected item from the list box.

    In that case you need to do the following...

    Code:
    textBox1.Text = listBox1.SelectedItem.ToString();
    string s = listBox1.SelectedItem.ToString();
    This will return the string value of the selected item from the list box.

    Or you could cast from an object to a string...

    Code:
    textBox1.Text = (string)listBox1.SelectedItem;
    Last edited by user612345; May 18th, 2011 at 12:37 PM.

  7. #7
    Join Date
    May 2011
    Posts
    3

    Re: How to get value...

    Hi,

    Thanks, this works , but the restriction is that the value from the listbox should be highlited,
    otherwise (if simply scrolled and selected, not highlited in this case) it gives exception error that,...

    " Null Refernce Exeption was unhandled
    Object refernce was not set to an instanceof an object."




    Quote Originally Posted by user612345 View Post
    I believe he/she wants the selected item from the list box.

    In that case you need to do the following...

    Code:
    textBox1.Text = listBox1.SelectedItem.ToString();
    string s = listBox1.SelectedItem.ToString();
    This will return the string value of the selected item from the list box.

    Or you could cast from an object to a string...

    Code:
    textBox1.Text = (string)listBox1.SelectedItem;

  8. #8
    Join Date
    Jun 2010
    Posts
    56

    Re: How to get value...

    Are you trying to send multiple items over to a string? Like if you select more then one item in the listbox because that can lead you to a null reference.

    I think you need to provide more information what exactly you are trying achieve. I know you want the value from a listbox but you might be hitting conditions that a simple ToString or Cast wont solve.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured