C# ListView Hook Pre SizeChanged Event
I have a ListView in tile mode. I have created a custom method to fire when the SizeChanged event is fired. Is there anyway to override this so the method fires before the SizeChanged event is fired?
I tried looking for a SizeChanging event but there isnt one. How can I开发者_JAVA技巧 do this?
In order to add some logic before the SizeChanged
event is fired, you'll need to subclass the existing ListView
control. Create a new class in your project and paste this code into it:
public class CustomListView : ListView
{
protected override void OnSizeChanged(System.EventArgs e)
{
//Fire my custom method before the ListView's size is changed
MyCustomMethod();
base.OnSizeChanged(e);
}
private void MyCustomMethod()
{
//Insert your custom logic here
//...
}
}
Then build your project, and use this CustomListView
control (or whatever you decide to name it) instead of the standard ListView
.
Alternatively, if you want to decouple the custom logic from the control itself, you can have your custom listview raise an event instead. Then, you can handle this new event (we'll call it SizeChanging
for consistency) wherever you need in your code. For example, modifying the above example:
public class CustomListView : ListView
{
public event EventHandler SizeChanging;
protected override void OnSizeChanged(System.EventArgs e)
{
//Raise the SizeChanging event before the ListView's size is changed
if (SizeChanging != null) {
SizeChanging(this, e);
}
base.OnSizeChanged(e);
}
}
精彩评论