Click here to Skip to main content
16,018,294 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want the item selected in the listbox, to be sent to the label as text. However I do not want a direct translation of the text

i.e. if the text has the word plant I want the label to say flowers. I want this to continually change when next is pressed as next selects the next item in the list.

private void NxtBtn_Click(object sender, EventArgs e)
{
    {
        if (listBox1.SelectedIndex == 0 || listBox1.SelectedIndex < listBox1.Items.Count - 1)
        {
            listBox1.SelectedIndex = listBox1.SelectedIndex + 1;

            if (listBox1.SelectedItems.Contains("plant"))
            {
                PgeTitleLbl.Text = "flowers";
            }

            else if (listBox1.SelectedItems.Contains("sugar"))
            {
                PgeTitleLbl.Text = "Sweets";
            }

            else if (listBox1.SelectedItems.Contains("eyes"))
            {
                PgeTitleLbl.Text = "face";
            }

       }
    }


What I have tried:

string[] Process = { "Flowers", "face"};
static int i = 0;


//   PgeTitleLbl.Text = Process[i];
// i = (i + 1);
Posted
Updated 10-Apr-18 8:21am
Comments
#realJSOP 10-Apr-18 9:27am    
WHAT IS YOUR PLATFORM?!!! Winforms or WPF?

First off, you should not be using the button click event to set controls like that. Instead, you should be changing the listbox selection when the button is clicked, and have a separate handler for the selection changed event for the listbox which changes the label.
 
Share this answer
 
First, you don't need all the if statements to do the replacements.

You do, however, need the first if statement, but it's wrong. Read the documentation on the ListBox.SelectedIndex. If nothing is selected in the ListBox, the SelectedIndex will be -1. There is no need to check to see if it's 0 or less than the count of the collection. Your condition, as coded, will always be true because the SelectedIndex, be it -1 or an actual index value because it will always be less than the count of the collection.
C#
if (listbox1.SelectedIndex >= 0)
{
    // Something is selected...
}


As for the text replacements, you just need to use string.Replace() without any if statements. I'll let you read the documentation on string.Replace to figure out how that works.
 
Share this answer
 
Hello,
You can easily achieve this by setting Text & Value pair in the ListBox. You do not need to use if condition here. When you select an item the corresponding value will show in the Label. You can use the OnSelectedIndexChanged event so that when you change your selection the value will change in the label.

Here is the code snippet for you if you are using asp.net web form.
<div>
          Selected Item: <asp:Label ID="PgeTitleLbl" runat="server"></asp:Label>
          <br /><br />

          <asp:ListBox ID="ListBox1" runat="server" OnSelectedIndexChanged="ShowValueInLabel"  AutoPostBack="true">
            <asp:ListItem Text="Plant" Value="Flowers"></asp:ListItem>
            <asp:ListItem Text="sugar" Value="sweet"></asp:ListItem>
            <asp:ListItem Text="eyes" Value="face"></asp:ListItem>
          </asp:ListBox>
        </div>


In code behind file for the method ShowValueInLabel:
protected void ShowValueInLabel(object sender, EventArgs e)
    {
        PgeTitleLbl.Text = ListBox1.SelectedValue;
    }


This is the easy process. If you want to display items in Listbox from database, you can use so. If still you want to use button click you can do so.
Change the ListBox AutoPostBack=false.

Add Button to the cshtml.
<asp:Button ID="NxtButton" runat="server" Text="Show Item" OnClick="ShowListItem" />


Add the method ShowListItem in Code behind file:

protected void ShowListItem(object sender, EventArgs e)
    {
        PgeTitleLbl.Text = ListBox1.SelectedValue;
    }



Hope this helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900