Solved

How do I select a listbox item by its tag in WPF and then disable it.

Posted on 2008-10-28
6
2,088 Views
Last Modified: 2013-11-12
Hello Everyone,

I am trying to select a listbox item by its tag value and then disabling it. I have no clue on how to this wiht a WPF control. Can someone show me an example on how to achieve this?
Thanks,

Stephane
0
Comment
Question by:MrTouya
  • 3
  • 3
6 Comments
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 22830389
I found some sample code which may help.

private:
   void listBox1_SelectedIndexChanged( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      // Get the currently selected item in the ListBox.
      String^ curItem = listBox1->SelectedItem->ToString();

      // Find the string in ListBox2.
      int index = listBox2->FindString( curItem );

      // If the item was not found in ListBox 2 display a message box,
      //  otherwise select it in ListBox2.
      if ( index == -1 )
            MessageBox::Show( "Item is not available in ListBox2" );
      else
            listBox2->SetSelected( index, true );
   }
0
 

Author Comment

by:MrTouya
ID: 22833866
This is what I have to do now. Is there a better way of doing the same?
 

private void MenuButtonState(string MenuButtonName, string State)
{
ListBoxItem CurrentSelectedItem = (ListBoxItem)this.MenuButtons.SelectedItem;
ListBoxItem SelectionToChangeStateOf = (ListBoxItem)this.MenuButtons.FindName(MenuButtonName);
ItemCollection MenuItems = this.MenuButtons.Items;
int IndexToDisable = MenuItems.IndexOf((ListBoxItem)SelectionToChangeStateOf);
int CurrentSelectedIndex = MenuItems.IndexOf((ListBoxItem)CurrentSelectedItem);
MenuButtons.SelectedItem = IndexToDisable;
if (State == "Disabled")
SelectionToChangeStateOf.IsEnabled = false;
else SelectionToChangeStateOf.IsEnabled = true;
MenuButtons.SelectedItem = CurrentSelectedItem;
}
 
Thanks,
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 22843946
Does it work?

>>>> SelectionToChangeStateOf = (ListBoxItem)this.MenuButtons.FindName(MenuButtonName);

Does the FindName return a (reference to a) ListBoxItem or does it return an index? In the latter case you hardly could cast an integer to a ListBoxItem.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:MrTouya
ID: 22846853
It Works. I have tried other ways, but this is the best way that I have come up with that works.
 
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 250 total points
ID: 22851066
>>>> It Works.

That is good. I personally have only little experience with managed C++ and WPF but I would try simpler code like


private void MenuButtonState(string MenuButtonName, string State)
{
    ListBoxItem SelectionToChangeStateOf = (ListBoxItem)this.MenuButtons.FindName(MenuButtonName);
    if (State == "Disabled")
         SelectionToChangeStateOf.IsEnabled = false;
    else
         SelectionToChangeStateOf.IsEnabled = true;
}

If your code above was working that one should work as well, right?
0
 

Author Comment

by:MrTouya
ID: 22855251
Yep. That is much more elegant.

Thanks,
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now