Solved

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

Posted on 2008-10-28
6
2,092 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to packaging 1 87
Need to impress with my knowledge of .NET 5 89
Having a different time zone on your task bar 3 64
C++ mouse_event mouse look 7 82
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…
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

813 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

10 Experts available now in Live!

Get 1:1 Help Now