How to make a controls compliant for winform and webform?
In my application I 开发者_StackOverflow中文版have methods which returns a control (checkbox, radiobutton, textbox) but I want to use the same class for my webform and winform. How can I achieve this? I was thinking about an interface, but I don't know how to implement this.
In my code I have the following methods:
public TextBox GenerateTextfield(AnswerPossibility answerPossibility)
{
TextBox textBox = new TextBox();
textBox.Tag = answerPossibility.Tag;
return textBox;
}
public Collection<ButtonBase> GenerateMultipleChoice(Collection<AnswerPossibility> answers)
{
Collection<ButtonBase> checks = new Collection<ButtonBase>();
foreach (AnswerPossibility a in answers)
{
CheckBox chk = new CheckBox();
chk.Text = a.Text;
chk.Name = "chk" + a.Id.ToString();
chk.Tag = a.Tag;
checks.Add(chk);
}
return checks;
}
How can I make this so, that I can use this methods in a win form as well in a web form?
The Winforms and Webforms UI structure is totally different from each other. The Web is a disconnected architecture, where you post back the results from the screen. With Winforms you have total control over every keystroke, within your application. You can achieve the same on the Web, but only through JavaScript, in a disconnected environment.
Although the control class names look the same, they are from a different part in the namespace.
If you really need you app to run the same on the web and local, you should go for Silverlight. This can run in & outside the browser, with the same code. Although you could limit your possible userbase.
精彩评论