0

I am learning basics of c#. Ia m using WPF. I want to make list-box to get disappear after selecting item from it. i used visibility=collapsed but it is not working here my code is:

<ListBox Foreground="White" Grid.Row="1" SelectionMode="Single" SelectionChanged="PrintText" Background="DarkGray" Visibility="Collapsed"  Height="Auto" HorizontalAlignment="Left" Margin="156,36,0,0" Name="listBox1" VerticalAlignment="Top" Width="191" UseLayoutRounding="True" />

private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
    if(autolist.Count>0)
    {
        listBox1.ItemsSource = autolist;
        listBox1.Visibility = Visibility.Visible;
        // a = pk;
    }
    else
    {
        listBox1.Visibility = Visibility.Collapsed;
        listBox1.ItemsSource = null;
    }
}

private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    //selectedItemsId = (int)listBox1.SelectedValue;
    if (listBox1.ItemsSource != null)
    {
        listBox1.Visibility = Visibility.Collapsed;
        textBox1.TextChanged += new TextChangedEventHandler(textBox1_TextChanged);
    }

    if (listBox1.SelectedIndex != -1)
    {
        textBox1.Text = listBox1.SelectedItem.ToString();
        textBox1.TextChanged += new TextChangedEventHandler(textBox1_TextChanged);
    }
}
Timothy Groote
  • 8,614
  • 26
  • 52
Dua Ali
  • 3,163
  • 3
  • 19
  • 13
  • is this asp.net? or WPF? This also has nothing to do with C#5.0 – Timothy Groote Jul 01 '13 at 16:04
  • 1
    There are also a number of terrible things in your code. why do you attach extra listeners to the textbox whenever the selection on the listbox changes? – Timothy Groote Jul 01 '13 at 16:06
  • @TimothyGroote Seeing the listbox definition and that he is using Visibility, it is safe to say this is WPF. – Tombala Jul 01 '13 at 16:09
  • @Tombala I figured, i was just being a tag-nazi ;) emd got it wrong by the looks of it. no postbacks in WPF. – Timothy Groote Jul 01 '13 at 16:11
  • 1
    Agreed with @TimothyGroote. You don't want to attach the event handler every time the selection changes in your listbox. You want to do that once when the textbox is created/added to your from, not in an event that could fire multiple times. – Tombala Jul 01 '13 at 16:12
  • Friends! i am using WPF. I want ListBox to disappear when after selecting any item value from this listbox. @TimothyGroote Tombala – Dua Ali Jul 01 '13 at 16:14
  • 1
    @DuaAli We figured this out already ;) – Timothy Groote Jul 01 '13 at 16:15
  • @DuaAli Post current code. In debug to you see that line called. Why do you post textBox1_TextChanged if that is not the question? Why do you attach that event handler multiple times? – paparazzo Jul 01 '13 at 16:34
  • @Blam turns out the textchanged handler had everything to do with why the listbox didn't seem to disappear ;) – Timothy Groote Jul 01 '13 at 16:43
  • 1
    @TimothyGroote Yes, OP was way ahead of us. – paparazzo Jul 01 '13 at 19:07

2 Answers2

1

Nothing is happening because you defined an event handler that has a different name from the one you call in your XAML.

Your listbox tries to fire PrintText, but i can see in your code, you want it to fire listBox1_SelectionChanged instead.

Change your XAML like this :

<ListBox Foreground="White" Grid.Row="1" SelectionMode="Single" SelectionChanged="listBox1_SelectionChanged" Background="DarkGray" Visibility="Collapsed"  Height="Auto" HorizontalAlignment="Left" Margin="156,36,0,0" Name="listBox1" VerticalAlignment="Top" Width="191" UseLayoutRounding="True" />

Also, to prevent the textbox change event from setting the listbox back to visible, try something like this in the listbox event handler

private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //selectedItemsId = (int)listBox1.SelectedValue;
            if (listBox1.ItemsSource != null)
            {
                listBox1.Visibility = Visibility.Collapsed;
            }

            if (listBox1.SelectedIndex != -1)
            {
                //remove the listener on the textbox
                textBox1.TextChanged -= TextBoxBase_OnTextChanged;
                textBox1.Text = listBox1.SelectedItem.ToString();
                //put the listener back on the text box
                textBox1.TextChanged += TextBoxBase_OnTextChanged;
            }
        }
Timothy Groote
  • 8,614
  • 26
  • 52
  • I was surprised, so i just tried your code, then started to laugh. IT works, you do hide the listbox, but then, you set the value of the textbox, causing it to fire its textchanged handler, which in turn makes the listbox visible again. – Timothy Groote Jul 01 '13 at 16:37
  • If you want to learn how these kind of things happen, and what's really going on in your code when you run it, learn about breakpoints. How to set them, and how to use them. See http://msdn.microsoft.com/en-us/library/ktf38f66(v=vs.71).aspx – Timothy Groote Jul 01 '13 at 16:41
  • Timothy Groote thank you for sharing this:) i want search panel in data-grid can you share some links related to its creation? – Dua Ali Jul 01 '13 at 18:19
  • @Dua Ali there are a *lot* of ways to achieve that, none of which seem related to this question. but you might want to check out http://stackoverflow.com/questions/4166403/wpf-datagrid-filter – Timothy Groote Jul 02 '13 at 07:49
-1

You can write:

listBox1.Visible = false;

Instead of listBox1.Visibility.

r.mirzojonov
  • 1,209
  • 10
  • 18