4

I have an application that I want to have 2 optional interfaces for: Touchscreen and Non-Touchscreen.

I obviously can make 2 separate forms but there is a lot of underlying code that would have to be duplicated anytime there is a change to it. All the controls are the same, they just have different sizes and positions. I was thinking of putting in 2 InitializeComponent methods but then I would have no way of designing both interfaces with visual studio.

Hoping someone else has any idea.

LarsTech
  • 80,625
  • 14
  • 153
  • 225
Brian Tacker
  • 1,091
  • 2
  • 18
  • 37

3 Answers3

5

I think that would be one interface with two implementations and then you inject the one you want into the form.

A quick example:

public interface IScreen
{
  void DoStuff();
}

public class TouchScreen : IScreen
{
  public void DoStuff()
  { }
}

public class NonTouchScreen : IScreen
{
  public void DoStuff()
  { }
}

public partial class ScreenForm : Form
{
  IScreen _ScreenType;

  public ScreenForm(IScreen screenType)
  {
    InitializeComponent();
    _ScreenType = screenType;
  }
}

And you would load it thus:

  TouchScreen touchThis = new TouchScreen();
  ScreenForm form1 = new ScreenForm(touchThis);
  form1.Show();

  //or

  NonTouchScreen notTouchThis = new NonTouchScreen();
  ScreenForm form2 = new ScreenForm(notTouchThis);
  form2.Show();
LarsTech
  • 80,625
  • 14
  • 153
  • 225
2

You might be interested in looking at this (and related) questions: MVVM for winforms more specifically stuff related to WPF Application Framework (WAF). One of the samples has a WinForms and a WPF UI sharing the same application logic. In your case it would just be two different WinForms UIs sharing the same application logic.

Also, have you considered using a templating engine (something like T4) to just generate both forms?

Community
  • 1
  • 1
Roman
  • 19,581
  • 6
  • 68
  • 84
1

If this is a winform, then you can add an event handler for Load event:

this.Load += new System.EventHandler(this.YourForm_Load);

...and there you can check whether this is touchscreen or not, then arrange the positions, shapes and sizes in separate helper methods for the two cases.

private void YourForm_Load(object sender, System.EventArgs e)
{
  if (IsTouchScreen)
  {
    ArrangeControlsForTouchScreen();
  }
  else
  {
    ArrangeControlsForPlainScreen();
  }
}

If this is in a web page, then you can do pretty much the same in the overridden Page.Load method.

scrat.squirrel
  • 3,607
  • 26
  • 31
  • I thought about this option but is there a way to design both interfaces with visual studio? – Brian Tacker Aug 04 '11 at 17:17
  • I find that I have better precision when I do this programmatically. It's a personal preference, nevertheless... If you want to do this with the mouse in VS, have a base class/form which does everything, then derive from it to put buttons and other UI elements on it. Then derive again and make the second form for the other version. Then, at runtime, instantiate the form class which suits the situation. (I guess there are many ways to do what you want) – scrat.squirrel Aug 04 '11 at 19:32