Click here to Skip to main content
15,892,839 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Within my desktop windows forms project I have two sets of private subs, they both work but I would like to choose/select during run time which set to use. How can I do that?

The first set looks like: (The first two subs restrict entry to numbers or backspace only. The third sub inserts colons and converts to timespan.)
VB
Private Sub DGV3_EditingControlShowing(sender...
  other code including add handler myTime
End Sub

Private Sub myTime(sender...
  other code
End Sub

Private Sub DGV3_CellEndEdit...
  other code
End Sub

The Second set (only one Sub) looks like:
VB
Private Sub DGV3_CellClick(sender As Object...
        DGV3(6, DGV3.CurrentRow.Index).Value = TimeOfDay...
End Sub

The first set of subs enters time in 24 hour format after the fact. The last sub shown above enters now (TimeOfDay) time.
Posted
Updated 11-Mar-15 22:28pm
v2

There are a couple of ways to do that.

Simple way: add a "SetSelection" option (could be bool, could be an enum) and test it in your code to decide which method to call.
Complex way: Use Delegates[^] to "preload" the method you want to call and then call the delegate.

The latter way is a lot more efficient, but is more complex to set up, more complex to maintain, and needs care to avoid runtime errors.
 
Share this answer
 
Comments
Member 10628309 12-Mar-15 14:49pm    
I have not been able to find any examples of SetSelection for the purpose I described. Could you provide an example or lead me to an article that demonstrates the concept?
OriginalGriff 12-Mar-15 15:00pm    
You are kidding, right?

public bool SetSelection { get; set; }
...
if (SetSelection)
{
...
Member 10628309 17-Mar-15 22:19pm    
Perhaps not the best solution...but it works. I added a CheckBox, that if checked would allow one of the selections to process and would not allow the other selection to process by adding If statements that would Exit Sub. Coded the reverse if the CheckBox is unchecked.
Perhaps not the best solution...but it works. I added a CheckBox, that if checked would allow one of the selections to process and would not allow the other selection to process by adding If statements that would Exit Sub. Coded the reverse if the CheckBox is unchecked.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900