Results 1 to 3 of 3

Thread: [RESOLVED] C#/VS2005 - How can I pass a listbox and it's selected index?

  1. #1

    Thread Starter
    Hyperactive Member gjon's Avatar
    Join Date
    Nov 2004
    Location
    Inescapable Void
    Posts
    442

    Resolved [RESOLVED] C#/VS2005 - How can I pass a listbox and it's selected index?

    I want to pass the selected index to a method. I have several listbox's and I want to figure out how I can pass that particular listbox's selected index to a method and then return some result. Not sure if I want it to just be a boolean or a number. I am thinking a boolean value would suffice.

    checkDialsPosition(listbox1.SelectedIndex);

    private void checkDialsPosition(chckthis listbox)
    {
    if (chckthis.SelectedIndex == 1)
    {
    if (dialPosition > 34 & dialPosition < 72)
    {
    pictureBox1.BackColor = Color.Green;
    return true;
    }
    else { pictureBox1.BackColor = Color.Red;
    return false;
    }
    }
    }

  2. #2
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: C#/VS2005 - How can I pass a listbox and it's selected index?

    I'm not sure I entirely understand your question. You can just pass your method the SelectedIndex and do what you like. However, you are trying to pass your method an Integer when it is expecting a ListBox.
    c# Code:
    1. private void button1_Click(object sender, EventArgs e)
    2. {
    3.     MessageBox.Show(CheckDialsPosition(this.listBox1.SelectedIndex).ToString());
    4. }
    5.  
    6. private bool CheckDialsPosition(int selIndex)
    7. {
    8.     if (selIndex == 1)
    9.     {
    10.         return true;
    11.     }
    12.     else
    13.     {
    14.         return false;
    15.     }
    16. }

  3. #3

    Thread Starter
    Hyperactive Member gjon's Avatar
    Join Date
    Nov 2004
    Location
    Inescapable Void
    Posts
    442

    Re: C#/VS2005 - How can I pass a listbox and it's selected index?

    Thank you.

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