What parameter has controls.remove() method in winforms? I want to remove some element from a form. What should I pass into the remove method - its id or something else?
The controls property is a Control.ControlCollection. To remove a control from this collection, you need to pass in an instance of the control you want to remove.
The MSDN docs has an example:
private void removeButton_Click(object sender, System.EventArgs e)
{
if (panel1.Controls.Contains(removeButton))
{
panel1.Controls.Remove(removeButton);
}
}
To answer the question in your comment:
You cannot remove it by its id or name, but you can find the instance of your control by passing its name into the Find method on the Controls property. This returns as array of all controls that have the specified name.
Related
This is a similar question to this one, but I'm hoping that a better answer has come along in the six years since it was asked.
I have a custom dictionary that I want to use in all textboxes in the application. I don't seem to be able to set the SpellCheck.CustomDictionaries property in a style, like I can with the SpellCheck.IsEnabled property, and I don't want to have to add the setter to every textbox individually.
The answer posted in that question requires hooking in to the Loaded event of every window where you want to use the custom dictionary. We have a large application that is growing all the time, and do not want to have to add handlers for every window, as well as rely on developers remembering to add the code when they add a new window.
Is there any better way to specify a custom dictionary for the whole application? Thanks.
Probably not what you wanted, but I used this EventManager.RegisterClassHandler to handle all of certain type's certain event.
For ex, here I am registering to all TextBoxes' LoadedEvent in MainWindow's constructor:
EventManager.RegisterClassHandler(typeof(TextBox), FrameworkElement.LoadedEvent, new RoutedEventHandler(SetCustomDictionary), true);
and define the RoutedEventHandler:
private void SetCustomDictionary(object sender, RoutedEventArgs e)
{
var textBox = e.OriginalSource as TextBox;
if (textBox != null)
{
textBox.SpellCheck.IsEnabled = true;
Uri uri = new Uri("pack://application:,,,/MyCustom.lex");
if (!textBox.SpellCheck.CustomDictionaries.Contains(uri))
textBox.SpellCheck.CustomDictionaries.Add(uri);
}
}
i serach a way to find controls by a string property:
private void AnimationCallback(string objectName, string value)
{
}
is it possible to find a control in this Animation callback method about the property "objectName"?? (e.g. ellipse1)
the control should become another color in condition to the value property. The Type of the control is also unknown.
Thank you
If your method is defined in a type derived from FrameworkElement, the FindName method should do what you want.
Use must use instance of any control as FameworkElement for using FindName method.
Example from MSDN:
private System.Windows.Controls.Grid LayoutRoot;
public Page()
{
System.Windows.Application.LoadComponent(this, new System.Uri(
"/SilverlightApplication1;component/Page.xaml",
System.UriKind.Relative));
this.LayoutRoot = ((System.Windows.Controls.Grid)
(this.FindName("LayoutRoot")));
}
I have a user control which is having a listview inside it. The SelectionChanged event of this list view is handled inside the user control. The code for the same is as follows:
private void lstvMyView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{...}
I want to call this handler again from some other place inside the user control. So to call this handler i need the "SelectionChangedEventArgs" there. When I am trying to create the instance of "SelectionChangedEventArgs", i am unable to understand what should I pass as parameters to the constructor of "SelectionChangedEventArgs".
The place from where I am suppose to call this handler does not add or remove any items in the listview. It just navigates in the items in the listview thereby changing the selectedindex of the listview.
I am trying to do something like this. The below code is obviously incorrect.
lstvMyView_SelectionChanged(_lstvMyView, new SelectionChangedEventArgs());
I want to call this handler again from some other place inside the user control
Don't. An event handler is not supposed to be called explicitly from your code. Whatever you're doing in this handler, you can put it in another method that only takes the parameters it needs, and call that method from your code.
The SelectionChangedEventArgs can be instantiated for unit testing as follows:
new SelectionChangedEventArgs(
System.Windows.Controls.Primitives.Selector.SelectionChangedEvent,
new List<CustomViewModel> { },
new List<CustomViewModel> { customViewModel }
)
Beware that both the removedItems and addedItems cannot be null so an empty collection should be passed.
I'd like to add tabs to my window when an item in the GridView is double-clicked. But the tab that will be added depends on the clicked item. Which way should I do this on WPF? I thought about RoutedEvents, but I don't know how to pass a parameter with it. Any suggestions?
This codeproject article covers using a new RoutedEvent with a different argument type than just RoutedEventArgs (rather far down, search for "Second using custom RoutedEventArgs"), though I wish WPF included a version of RoutedEventHandler like the CLR's EventHandler<T> where T : EventArgs so you didn't have to declare a new delegate every time.
The summary:
Create a new type that subtypes RoutedEventArgs and has properties for your data.
Make sure your RoutedEventArgs subtype implements the non-default constructors of RoutedEventArgs as those are needed for it to work.
Declare a new delegate that matches this subtype.
Use as you would any other custom event in your code (also covered in that link).
Use commands rather than routed events:
Set a command for the clicked item and pass the clicked item's reference as the command's parameter using RelativeSource binding in the XAML.
Is there an event handler that will be called when an item is added in a listbox in WPF?
Thanks!
The problem is that the INotifyCollectionChanged interface which contains the event handler is explicitly implemented, which means you have to first cast the ItemCollection before the event handler can be used:
public MyWindow()
{
InitializeComponent();
((INotifyCollectionChanged)mListBox.Items).CollectionChanged +=
mListBox_CollectionChanged;
}
private void mListBox_CollectionChanged(object sender,
NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
// scroll the new item into view
mListBox.ScrollIntoView(e.NewItems[0]);
}
}
Ref.
Josh's advice about the observable collection should also be considered.
Take a different approach. Create an ObservableCollection (which does have such an event) and set the ItemsSource of the ListBox to this collection. In other words, in WPF you should think about the problem differently. The control isn't necessarily what is being modified ... the collection behind it is.
UPDATE
Based on your comment to Mitch's answer which indicates your binding source is actually an XML document, I suggest looking into hooking up to the XObject.Changed event of the XML document/element/etc. This will give you change information about the XML structure itself - not the ItemCollection which is an implementation detail you shouldn't need to consider. For example, ItemCollection (or any INotifyCollectionChanged) doesn't guarantee an individual event for every change. As you noted, sometimes you'll just get a generic reset notification.