Solved

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

Posted on 2008-10-28
6
2,080 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

744 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

13 Experts available now in Live!

Get 1:1 Help Now