Click here to Skip to main content
15,867,488 members
Home / Discussions / C#
   

C#

 
AnswerRe: Error Converting String to Int Pin
OriginalGriff18-Jul-15 2:31
mveOriginalGriff18-Jul-15 2:31 
AnswerRe: Error Converting String to Int Pin
miki-bgd18-Jul-15 5:12
miki-bgd18-Jul-15 5:12 
QuestionHide a Panel on MDI Container Pin
Jassim Rahma17-Jul-15 11:13
Jassim Rahma17-Jul-15 11:13 
AnswerRe: Hide a Panel on MDI Container Pin
Eddy Vluggen17-Jul-15 11:59
professionalEddy Vluggen17-Jul-15 11:59 
QuestionComplexity Pin
Gilbert Consellado16-Jul-15 22:37
professionalGilbert Consellado16-Jul-15 22:37 
AnswerRe: Complexity Pin
Godhaniketan17-Jul-15 0:07
professionalGodhaniketan17-Jul-15 0:07 
GeneralRe: Complexity Pin
Gilbert Consellado17-Jul-15 2:15
professionalGilbert Consellado17-Jul-15 2:15 
AnswerRe: Complexity PinPopular
Pete O'Hanlon17-Jul-15 1:07
subeditorPete O'Hanlon17-Jul-15 1:07 
There's a heck of a lot of code going on here, and it certainly appears as though you're violating SRP here. Rather than doing things like this, I would look to implement separate classes that do what you need to do - each class would then decide whether it was the one that handled the case. There's even a name for this, as a pattern; it's called the Chain Of Responsibility. So, you would end up with something like this:
C#
public abstract class HandlerBase
{
  private IHandler successor;
  public void SetSuccessor(IHandler successor)
  {
    this.successor = handler;
  }
  public virtual void HandleRequest(KeyEventArgs e)
  {
    if (CanHandleRequest(e))
    {
      return;
    }
    if (successor != null)
    {
      successor.HandleRequest(e);
    }
  }

  public abstract bool CanHandleRequest(KeyEventArgs e);
  public abstract void HandleRequest(FlyoutDialog fd);
}

public class FlyoutAbort : HandlerBase
{
  public virtual bool CanHandleRequest(KeyEventArgs e)
  {
    return (e.KeyCode == Keys.A || (e.Alt && e.KeyCode == Keys.A)) && fd.CanExecuteCommand(Helpers.FlyoutCommandAbort);
  }

  public abstract void HandleRequest(FlyoutDialog fd)
  {
    fd.ExecuteCommand(Helpers.FlyoutCommandAbort); // In reality, I would look to move the command from Helpers, into this class.
  }
}

public class FlyoutCancel : HandlerBase
{
  public virtual bool CanHandleRequest(KeyEventArgs e)
  {
    return e.KeyCode == Keys.Escape && fd.CanExecuteCommand(Helpers.FlyoutCommandCancel;
  }

  public abstract void HandleRequest(FlyoutDialog fd)
  {
    fd.ExecuteCommand(Helpers.FlyoutCommandCancel);
  }
}
Then, in your main class, you simply do this:
C#
private FlyoutAbort flyoutAbort;
private FlyoutCancel flyoutCancel;

private void InitializeChain()
{
  flyoutAbort = new FlyoutAbort();
  flyoutCancel = new FlyoutCancel();
  flyoutAbort.SetSuccessor(flyoutCancel);
}
Finally, all you need to do to handle the request (if it can be handled that is), is call:
C#
flyoutAbort.HandleRequest(e);

GeneralRe: Complexity Pin
Gilbert Consellado17-Jul-15 2:17
professionalGilbert Consellado17-Jul-15 2:17 
GeneralRe: Complexity Pin
Gilbert Consellado17-Jul-15 5:18
professionalGilbert Consellado17-Jul-15 5:18 
AnswerRe: Complexity PinPopular
Alan N17-Jul-15 4:37
Alan N17-Jul-15 4:37 
GeneralRe: Complexity Pin
Gilbert Consellado17-Jul-15 5:22
professionalGilbert Consellado17-Jul-15 5:22 
QuestionPass Anonymous Type To Worker Pin
Kevin Marois16-Jul-15 12:58
professionalKevin Marois16-Jul-15 12:58 
AnswerRe: Pass Anonymous Type To Worker Pin
Dave Kreskowiak16-Jul-15 14:49
mveDave Kreskowiak16-Jul-15 14:49 
GeneralRe: Pass Anonymous Type To Worker Pin
Kevin Marois16-Jul-15 14:52
professionalKevin Marois16-Jul-15 14:52 
AnswerRe: Pass Anonymous Type To Worker Pin
F-ES Sitecore16-Jul-15 21:45
professionalF-ES Sitecore16-Jul-15 21:45 
Questionform 1 and form 2 Pin
jamesmc153516-Jul-15 4:08
jamesmc153516-Jul-15 4:08 
AnswerRe: form 1 and form 2 Pin
Wes Aday16-Jul-15 4:26
professionalWes Aday16-Jul-15 4:26 
Questioni get a error invalid expresion Pin
jamesmc153516-Jul-15 2:57
jamesmc153516-Jul-15 2:57 
AnswerRe: i get a error invalid expresion Pin
Richard MacCutchan16-Jul-15 3:02
mveRichard MacCutchan16-Jul-15 3:02 
GeneralRe: i get a error invalid expresion Pin
jamesmc153516-Jul-15 3:09
jamesmc153516-Jul-15 3:09 
GeneralRe: i get a error invalid expresion Pin
Richard MacCutchan16-Jul-15 4:54
mveRichard MacCutchan16-Jul-15 4:54 
GeneralRe: i get a error invalid expresion Pin
jamesmc153516-Jul-15 4:56
jamesmc153516-Jul-15 4:56 
GeneralRe: i get a error invalid expresion Pin
Richard MacCutchan16-Jul-15 4:58
mveRichard MacCutchan16-Jul-15 4:58 
GeneralRe: i get a error invalid expresion Pin
jamesmc153516-Jul-15 5:00
jamesmc153516-Jul-15 5:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.