Asked by:
WinForm - Is it possilbe to place (CellMouseEnter, CellMouseLeave) in a Global Class?

Question
-
The coding works fine in a WinForm, however, is there a way to place this code in a centralize location such as a Global Class, so there is no need to place these coding repeatedly, all over to other forms.
What I am looking for is something like a class
Global.FormatDataGridView.HighlightRow(true); // In case I need this feature, just turn it onprivate void datagridview1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex > 0 && e.ColumnIndex > 0)
this.datagridview1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.LightBlue;
else
return;
}private void datagridview1_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex > 0 && e.ColumnIndex > 0)
this.datagridview1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.White;
else
return;
}- Moved by CoolDadTx Monday, November 18, 2019 2:47 PM Winforms related
All replies
-
I may regret even responding to you, but you can just simply make each method event name a generic name not dedicated to a particular form control name. And then you can go to the control's property for the event and give the generic control event method, which makes the code sharable for all controls needing to use the generic event.
If you want to find out what control is using the event, then you can cast 'sender' to a datagridview control object and address the propertiers of the object, like the Name property.
private void datagridview_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
{
var dgv = (datagridview)sender;
if (dgv.Name == " datagridview1") do something;
}
- Edited by DA924x Saturday, November 16, 2019 5:39 AM
-
I'd suggest creating a class that inherits from DataGridView. Add the events as well as any other features you need (such as the Highlight) inside your inherited class.
Then, in all the places in your program where you are using a DataGridVew, use your inherited class instead of the DataGridView.
- Proposed as answer by Ihandler MSDN Tuesday, November 19, 2019 3:31 AM
-
If I understand what you are asking then the easiest way to do it is to make a User Control with a DataGridView in it.
Inheriting from DataGridView is the elegant solution. It is what C++ (actually MFC) programmers would do. Unfortunately .Net makes things not as easy. I suggest doing it if you can but if it is too complicated for you then use the User Control solution.
Sam Hobbs
SimpleSamples.Info- Proposed as answer by Ihandler MSDN Tuesday, November 19, 2019 3:31 AM
-
Yes, your solution also works.
Personally, I would prefer to create a class that inherits from DataGridView.
Sam, Many thanks !
Another option is to just instance the DVG object and dependency inject the object into the class that needs to use the object, becuase after all, the control is a class. Even if you inherit from the DVG control, a class, the inheriting class must be instanced.
-
I'd suggest creating a class that inherits from DataGridView. Add the events as well as any other features you need (such as the Highlight) inside your inherited class.
Then, in all the places in your program where you are using a DataGridVew, use your inherited class instead of the DataGridView.
Yes, I totally understand what you mean, you explained it very well !
I think your solution would do great on my project, however, the existing DataGridView control is widely in use in different parts of modules, it looks like there is a lot to do if I create a class that inherits from DataGridView.
If possible, would it be possible to post some sample coding so I can dig into it.
Anyway, thanks a lot !
-
If I understand what you are asking then the easiest way to do it is to make a User Control with a DataGridView in it.
Inheriting from DataGridView is the elegant solution. It is what C++ (actually MFC) programmers would do. Unfortunately .Net makes things not as easy. I suggest doing it if you can but if it is too complicated for you then use the User Control solution.
Sam Hobbs
SimpleSamples.InfoYes, your solution also works.
Personally, I would prefer to create a class that inherits from DataGridView.
Sam, Many thanks !
- Proposed as answer by Ihandler MSDN Tuesday, November 19, 2019 3:31 AM
- Unproposed as answer by Ihandler MSDN Tuesday, November 19, 2019 3:31 AM
-
Hi Ihandler,
It seems that your problem has been solved. If so, please click "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.
Best Regards,
TimonMSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.