E
E-John
Guest
Dear All,
How to implement comboBox in Model-View-Presenter?
If Model-View-Presenter is applied to this case, how to implement the comboBox in Model-View-Presenter?
The item in Device Model may support or may not support Password setting, how to disable the password field on Window when the password setting is not supported for the device models, for example, device.Kind = "X3" and device.Kind = "X5" do not support password setting, when combox text is changed to these two items, the "checkBoxPassword" and maskTextBosPassword should be invisible to user.
Thanks and Best regards,
E-John
namespace TestWinFormMVPComboBox.Models
{
public interface IModel
{
void AddDevice(DeviceModel device);
}
}
namespace TestWinFormMVPComboBox.Models
{
public class Model : IModel
{
static Model()
{
var Devices = new List<DeviceModel>();
var device = new DeviceModel();
device.Kind = "X3";
device.DeviceName = "Basic";
device.PasswordIsSupport = false;
device.Password = null;
Devices.Add(device);
device = new DeviceModel();
device.Kind = "X5";
device.DeviceName = "Classic";
device.PasswordIsSupport = false;
device.Password = null;
Devices.Add(device);
device = new DeviceModel();
device.Kind = "X7";
device.DeviceName = "Luxury";
device.PasswordIsSupport = true;
device.Password = null;
Devices.Add(device);
}
public void AddDevice(DeviceModel device)
{
}
}
}
namespace TestWinFormMVPComboBox.Models
{
public class DeviceModel
{
public string Kind { get; set; }
public string DeviceName { get; set; }
public bool PasswordIsSupport { get; set; }
public string Password { get; set; }
}
}
namespace TestWinFormMVPComboBox.Views
{
public interface IView
{
}
}
namespace TestWinFormMVPComboBox.Views
{
public interface IDeviceView : IView
{
string Kind { get; set; }
string DeviceName { get; set; }
bool PasswordIsSupport { get; set; }
string Password { get; set; }
// comboBox show available model?
}
}
namespace TestWinFormMVPComboBox.Presenters
{
public class Presenter<T> where T : IView
{
protected static IModel Model { get; private set; }
static Presenter()
{
Model = new Model();
}
protected T View { get; private set; }
public Presenter(T view)
{
View = view;
}
}
}
namespace TestWinFormMVPComboBox.Presenters
{
public class DevicePresenter : Presenter<IDeviceView>
{
public DevicePresenter(IDeviceView view)
: base(view)
{
}
}
}
namespace TestWinFormMVPComboBox
{
public partial class Form1 : Form
{
private DevicePresenter _devicePresenter;
public Form1()
{
InitializeComponent();
}
private void buttonAddDevice_Click(object sender, EventArgs e)
{
using (var formAddDevice = new FormAddDevice())
{
if (formAddDevice.ShowDialog() == DialogResult.OK)
{
MessageBox.Show("Add device is complete ");
}
}
}
}
}
namespace TestWinFormMVPComboBox
{
public partial class FormAddDevice : Form, IDeviceView
{
private DevicePresenter _devicePresenter;
private bool _cancleClose;
public FormAddDevice()
{
InitializeComponent();
this.Closing += FormAddDevice_Closing;
this.AcceptButton = this.buttonOK;
this.CancelButton = this.buttonCancel;
this.buttonOK.DialogResult = DialogResult.OK;
this.buttonCancel.DialogResult = DialogResult.Cancel;
_devicePresenter = new DevicePresenter(this);
}
// view's Kind implementation is here
public string Kind
{
get { return comboBoxKind.SelectedText; }
set { comboBoxKind.SelectedValue = value; }
}
public string DeviceName
{
get { return textBoxName.Text; }
set { textBoxName.Text = value; }
}
public bool PasswordIsSupport
{
get { return checkBoxSetPassword.Enabled; }
set { checkBoxSetPassword.Enabled = value; }
}
public string Password
{
get { return maskedTextBoxPassword.Text; }
set { maskedTextBoxPassword.Text = value; }
}
private void FormAddDevice_Closing(object sender, CancelEventArgs e)
{
e.Cancel = _cancleClose;
_cancleClose = false;
}
// view's method???
private void comboBoxKind_TextChanged(object sender, EventArgs e)
{
// pass to presenter which handle model and view
}
}
}
Continue reading...
How to implement comboBox in Model-View-Presenter?
If Model-View-Presenter is applied to this case, how to implement the comboBox in Model-View-Presenter?
The item in Device Model may support or may not support Password setting, how to disable the password field on Window when the password setting is not supported for the device models, for example, device.Kind = "X3" and device.Kind = "X5" do not support password setting, when combox text is changed to these two items, the "checkBoxPassword" and maskTextBosPassword should be invisible to user.
Thanks and Best regards,
E-John
namespace TestWinFormMVPComboBox.Models
{
public interface IModel
{
void AddDevice(DeviceModel device);
}
}
namespace TestWinFormMVPComboBox.Models
{
public class Model : IModel
{
static Model()
{
var Devices = new List<DeviceModel>();
var device = new DeviceModel();
device.Kind = "X3";
device.DeviceName = "Basic";
device.PasswordIsSupport = false;
device.Password = null;
Devices.Add(device);
device = new DeviceModel();
device.Kind = "X5";
device.DeviceName = "Classic";
device.PasswordIsSupport = false;
device.Password = null;
Devices.Add(device);
device = new DeviceModel();
device.Kind = "X7";
device.DeviceName = "Luxury";
device.PasswordIsSupport = true;
device.Password = null;
Devices.Add(device);
}
public void AddDevice(DeviceModel device)
{
}
}
}
namespace TestWinFormMVPComboBox.Models
{
public class DeviceModel
{
public string Kind { get; set; }
public string DeviceName { get; set; }
public bool PasswordIsSupport { get; set; }
public string Password { get; set; }
}
}
namespace TestWinFormMVPComboBox.Views
{
public interface IView
{
}
}
namespace TestWinFormMVPComboBox.Views
{
public interface IDeviceView : IView
{
string Kind { get; set; }
string DeviceName { get; set; }
bool PasswordIsSupport { get; set; }
string Password { get; set; }
// comboBox show available model?
}
}
namespace TestWinFormMVPComboBox.Presenters
{
public class Presenter<T> where T : IView
{
protected static IModel Model { get; private set; }
static Presenter()
{
Model = new Model();
}
protected T View { get; private set; }
public Presenter(T view)
{
View = view;
}
}
}
namespace TestWinFormMVPComboBox.Presenters
{
public class DevicePresenter : Presenter<IDeviceView>
{
public DevicePresenter(IDeviceView view)
: base(view)
{
}
}
}
namespace TestWinFormMVPComboBox
{
public partial class Form1 : Form
{
private DevicePresenter _devicePresenter;
public Form1()
{
InitializeComponent();
}
private void buttonAddDevice_Click(object sender, EventArgs e)
{
using (var formAddDevice = new FormAddDevice())
{
if (formAddDevice.ShowDialog() == DialogResult.OK)
{
MessageBox.Show("Add device is complete ");
}
}
}
}
}
namespace TestWinFormMVPComboBox
{
public partial class FormAddDevice : Form, IDeviceView
{
private DevicePresenter _devicePresenter;
private bool _cancleClose;
public FormAddDevice()
{
InitializeComponent();
this.Closing += FormAddDevice_Closing;
this.AcceptButton = this.buttonOK;
this.CancelButton = this.buttonCancel;
this.buttonOK.DialogResult = DialogResult.OK;
this.buttonCancel.DialogResult = DialogResult.Cancel;
_devicePresenter = new DevicePresenter(this);
}
// view's Kind implementation is here
public string Kind
{
get { return comboBoxKind.SelectedText; }
set { comboBoxKind.SelectedValue = value; }
}
public string DeviceName
{
get { return textBoxName.Text; }
set { textBoxName.Text = value; }
}
public bool PasswordIsSupport
{
get { return checkBoxSetPassword.Enabled; }
set { checkBoxSetPassword.Enabled = value; }
}
public string Password
{
get { return maskedTextBoxPassword.Text; }
set { maskedTextBoxPassword.Text = value; }
}
private void FormAddDevice_Closing(object sender, CancelEventArgs e)
{
e.Cancel = _cancleClose;
_cancleClose = false;
}
// view's method???
private void comboBoxKind_TextChanged(object sender, EventArgs e)
{
// pass to presenter which handle model and view
}
}
}
Continue reading...