locked
Button enable/disable implement in Model-View-Presenter? RRS feed

  • Question

  • Dear All,

    If the control enable / disable is related to the logical part, how to handle(implement) the control enable/disable in Model-View-Presenter?

    For example, if there is a tree, to delete root is not allowed, that make us to disable the [DeleteNode] button when root is selected.

    if the child level is greater than three, it is not allowed to add any child node, that make us to disable the [Add Child Node] button when selectedNode level is equal to 3

    Thanks and Best regards,

    E-John

    namespace ButtonMVP
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            public class NodeName
            {
                public static int index = 0;
            }
    
            private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
            {
                if (treeView1.SelectedNode != null)
                {
                    if (treeView1.SelectedNode == treeView1.Nodes[0])
                    {
                        buttonDeleteNode.Enabled = false;
                    }
                    else
                    {
                        buttonDeleteNode.Enabled = true;
                    }
    
                    if (treeView1.SelectedNode.Level >= 3)
                    {
                        buttonAddChildNode.Enabled = false;
                    }
                    else
                    {
                        buttonAddChildNode.Enabled = true;
                    }
                }
            }
    
            private void buttonAddChildNode_Click(object sender, EventArgs e)
            {
                if (treeView1.SelectedNode != null)
                {
                    NodeName.index++;
                    treeView1.SelectedNode.Nodes.Add("Node" + NodeName.index);
    
                    treeView1.ExpandAll();
                }
            }
    
            private void buttonDeleteNode_Click(object sender, EventArgs e)
            {
                if (treeView1.SelectedNode != null)
                {
                    treeView1.SelectedNode.Remove();
                }
            }
        }
    }


    • Edited by E-John Monday, April 29, 2019 3:09 PM
    • Moved by CoolDadTx Monday, April 29, 2019 6:25 PM Winforms related
    Monday, April 29, 2019 3:06 PM

All replies

  • Hi E-John,

    I am not quite sure what your problem is. It seems that you have achieved the relevant requirements yourself.

    Do you want to ask questions about Model-View-Presenter? I am sorry that this forum only discusses issues related to the Winform feature.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 30, 2019 2:19 AM
  • Dear Kyle,

    Thanks for your reply.

    The article introduces the basic MVP,

    https://www.codeproject.com/Articles/32769/Easy-Example-for-Model-View-Presenter-in-ASP-NET-C

    basically, we make a wrapper for the control,

    for example, if we want to get a string from UI text box,

    we will have a view interface like below, and a implement NodeName in FormMain(UI), and the manipulation is implement in Presenter(here is not provided.)

    Here are my understanding about the MVP, but I don't know how to handle the button enable/disable in MVP pattern?

    Thanks and Best regards,

    E-John

    We will have 

        public interface IMainView : IView
        {
            string NodeName { get; set; }
        }

        public partial class FormMain : Form, IMainView 
        {

            public string NodeName
            {
                get { return textBoxNodeName.Text; }
                set { textBoxNodeName.Text = value; }
            }

            ...

        }



    • Edited by E-John Tuesday, April 30, 2019 6:55 AM
    Tuesday, April 30, 2019 2:38 AM
  • Hi E-John,

    I am sorry that architecture is not within the scope of the forum. I read the article you provided. And I think it will be better to  place the code associated with enable/disable in the "Presenter".

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 30, 2019 8:03 AM