Hack Forums
[C#] Listbox next Item - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] Listbox next Item (/showthread.php?tid=5044760)



Listbox next Item - Geekie - Nov 3, 2015

Hey guys, I have started coding this part yet for Obotle for my chessy pick up line bot for omegle. But what would be the best method to go to next item in a listbox. I don't want to use foreach or for I think thinking it starts off a first item once that item is sent it go to next item in the listbox. Anyone has a good method that would work. This isn't multi thread. a single thread.
I was thinking something like this..

Code
public void ChessyLine()
        {
            int currentLine = -1;
            if (currentLine == -1)
            {
                currentLine = listBox1.SelectedIndex;
            }
            currentLine++;
            if (currentLine < listBox1.Items.Count)
            {
                //code message listBox1.Items[currentLine];
            }
            else
            {
                //end of messages
            }
        }



* - He B TeMy - Nov 3, 2015

You can use Listbox.Items[index] for this.
Or if you're talking about only selecting, you can do Listbox.SelectedItem +=1, don't forget to check for index overflow.


* - +Haze - Nov 3, 2015

Code
private string GetNextCheesyLine()
{
     int nextIndex = (lstLines.SelectedIndex == lstLines.Items.Length) ?  0 : lstLines.SelectedIndex++;
     return lstLines.Items[nextIndex].ToString();
}

I wrote that off head. Should work.


* - Geekie - Nov 3, 2015

Thanks guys, Reason why I ask about my code because I have to send the message to the stranger.

So the code would look like this.

Code
public void ChessyLine()
        {
            int currentLine = -1;
            if (currentLine == -1)
            {
                currentLine = listBox1.SelectedIndex;
            }
            currentLine++;
            if (currentLine < listBox1.Items.Count)
            {
                textSend.Invoke(new MethodInvoker(() => textSend.Text = listBox1.Items[currentLine].ToString()));
                Chat("Bot: ", Color.Blue, FontStyle.Bold, false);
                Chat(textSend.Text, Color.FromArgb(124, 133, 142), FontStyle.Regular, true);
                _omegle.SendMessage(textSend.Text);
                textSend.Invoke(new MethodInvoker(() => textSend.Text = string.Empty));
            }
            else
            {
                textSend.Invoke(new MethodInvoker(() => textSend.Text = "We're at the end of chessy lines, Good bye for now"));
                Chat("Bot: ", Color.Blue, FontStyle.Bold, false);
                Chat(textSend.Text, Color.FromArgb(124, 133, 142), FontStyle.Regular, true);
                _omegle.SendMessage(textSend.Text);
                textSend.Invoke(new MethodInvoker(() => textSend.Text = string.Empty));
            }
        }

Edit, The method I posted above works great thanks guys for the help.