Creating multiple interfaces for a form

I have an application in which I want to have 2 additional interfaces for: a touch screen and a non-touch screen.

I obviously can make two separate forms, but there is a lot of basic code that will need to be duplicated at any time when there are changes to it. All controls are the same, they have different sizes and positions. I was thinking about implementing two InitializeComponent methods, but then I would not have the opportunity to develop both interfaces with a visual studio.

Hope someone else has any ideas.

+4
source share
3 answers

I think it will be one interface with two implementations, and then you will enter the one you want in the form.

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 load it like this:

  TouchScreen touchThis = new TouchScreen(); ScreenForm form1 = new ScreenForm(touchThis); form1.Show(); //or NonTouchScreen notTouchThis = new NonTouchScreen(); ScreenForm form2 = new ScreenForm(notTouchThis); form2.Show(); 
+5
source

You may be interested in looking at these (and related) questions: MVVM for winforms are more specifically related to the WPF Application Framework (WAF) . One sample has WinForms and a WPF user interface using the same application logic. In your case, it will be just two different WinForms interfaces that use the same application logic.

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

+2
source

If it is winform, you can add an event handler for the Load event:

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

... and there you can check whether it is a touch screen or not, and then arrange the positions, shapes and sizes in separate auxiliary methods for two cases.

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

If this is on a web page, you can do almost the same thing in the redefined Page.Load method.

+1
source

All Articles