none
How to pass value from one user control to another user control using c# RRS feed

  • Question

  • Hi,

    I am developing a form application with c #. I use 2 user controls in the form application I developed, these user controls have one picturebox.

    I want to send the data from the picturebox in usercontrol 1 to the picturebox in usercontrol 2.

    but i haven't done it yet.

    // This is main form code
    
    namespace FrmDrag
    {
        public partial class Form1 : Form
        {
            static Form1 _obj;
            private bool mouseDown;
            private Point lastPosition;
            previewTab ptab = new previewTab();
            resultsTab rtab = new resultsTab();
    
            public Panel PnlContainer
            {
                get { return panelContainer; }
                set { panelContainer = value; }
            }
            public static Form1 Instance
            {
                get
                {
                    if (_obj == null)
                    {
                        _obj = new Form1();
                    }
                    return _obj;
                }
            }
    
    
            public Form1()
            {
                
                InitializeComponent();
                _obj = this;
                ptab.Dock = DockStyle.Fill;
                rtab.Dock = DockStyle.Fill;
                panelContainer.Controls.Add(ptab);
                panelContainer.Controls.Add(rtab);  
              this.panelContainer.Controls["previewTab"].BringToFront();           
                
    
            }     
    
            
            private void Form1_Load(object sender, EventArgs e)
            {
                menuSidePanel.Height = previewBtn.Height;
                menuSidePanel.Left = previewBtn.Left;
    
            }
    
            private void previewBtn_MouseMove(object sender, MouseEventArgs e)
            {
                        
                menuSidePanel.Height = previewBtn.Height;
                menuSidePanel.Left = previewBtn.Left;
    
            }
    
            private void resultsBtn_MouseMove(object sender, MouseEventArgs e)
            {
                menuSidePanel.Height = resultsBtn.Height;
                menuSidePanel.Left = resultsBtn.Left;
            }
    
           
    
            private void exitBtn_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }
    
            private void previewBtn_Click(object sender, EventArgs e)
            {
               
                this.panelContainer.Controls["previewTab"].BringToFront();
               
               
            }
    
            private void resultsBtn_Click(object sender, EventArgs e)
            {
                resultsTab rsltTab = new resultsTab();
                rsltTab.rsltTabPicture.Image = rtab.rsltTabPicture.Image;
                this.panelContainer.Controls["resultsTab"].BringToFront();
            }
    
            private void headerPanel_MouseDown(object sender, MouseEventArgs e)
            {
                mouseDown = true;
                lastPosition = e.Location;
            }
    
            private void headerPanel_MouseUp(object sender, MouseEventArgs e)
            {
                mouseDown = false;
               this.Opacity = 1;
            }
    
            private void headerPanel_MouseMove(object sender, MouseEventArgs e)
            {
                if (mouseDown)
                {
                    this.Location = new Point((this.Location.X - lastPosition.X) + e.X,(this.Location.Y - lastPosition.Y) + e.Y);
                    this.Opacity = 0.4;
                    this.Update();
                }
               
            }
        }
    }
    
    // this is usercontrol1 code
    
    
    
    namespace FrmDrag
    {
        public partial class previewTab : UserControl
        {
           
            public PictureBox prvTabPicture   // property
            {
                get { return previewTabPictureBox; }   // get method
                set { previewTabPictureBox = value; }  // set method
            }
            resultsTab rsltTb = new resultsTab();
            public previewTab()
            {
                InitializeComponent();
            }
    
            private void countBtn_Click(object sender, EventArgs e)
            {
                 if (!Form1.Instance.PnlContainer.Controls.ContainsKey("resultsTab"))
                 {
    
                     rsltTb.Dock = DockStyle.Fill;
                     Form1.Instance.PnlContainer.Controls.Add(rsltTb);
                 }
               
                 rsltTb.rsltTabPicture.Image = previewTabPictureBox.Image;
                 Form1.Instance.PnlContainer.Controls["resultsTab"].BringToFront();
               
    
            }
    
           
    
            private void snapBtn_MouseMove(object sender, MouseEventArgs e)
            {
                menuSidePanel1.Height = snapBtn.Height;
                menuSidePanel1.Top = snapBtn.Top;
            }
    
            private void countBtn_MouseMove(object sender, MouseEventArgs e)
            {
                menuSidePanel1.Height = countBtn.Height;
                menuSidePanel1.Top = countBtn.Top;
            }
    
            private void previewTabStartBtn_MouseMove(object sender, MouseEventArgs e)
            {
                menuSidePanel1.Height = previewTabStartBtn.Height;
                menuSidePanel1.Top = previewTabStartBtn.Top;
            }
    
            private void brightnessSlider_ValueChanged(object sender, EventArgs e)
            {
                label2.Text = "Brightness="+brightnessSlider.Value;
            }
            
        }
    }
    
    // and this is usercontrol2 code
    
    namespace FrmDrag
    {
        public partial class resultsTab : UserControl
        {
            
            
            public PictureBox rsltTabPicture   // property
            {
                get { return resultTabPcitureBox; }   // get method
                set { resultTabPcitureBox = value; }  // set method
            }
            public string labelText   // property
            {
                get { return label1.Text; }   // get method
                set { label1.Text = value; }  // set method
            }
            public resultsTab()
            {
                InitializeComponent();
               
            }
    
           
        }
    }
    
    
    
    

    Thank you for your advice,

    Friday, March 27, 2020 3:07 PM

Answers

  • If you want to pass an object between two classes, then you would use an Interface. A form and a usercontrol are classes where an Interface can be used to pass an object between classes. 

    Here is the concept where primitive types (objects) are being passed from a form class to a presenter class using an Interface between the two.

    namespace PubCompanyWinCore.IView
    {
        public interface IAuthorView
        {
            int AuthorId { get; set; }
            string FirstName { get; set; }
            string LastName { get; set; }
            string Message { get; set; }
    
            event EventHandler<EventArgs> Send;
            event EventHandler<EventArgs> Delete;
            
            List<DtoAuthor> GetAuthors();
        }
    }
    The 'this' is the passing of the IAutorview objects over to the presenter class.
    namespace PubCompanyWinCore
    {
        public partial class AuthorView : Form, IAuthorView
        {
            private IAuthorDM _authorDm;
            private AuthorViewPresenter mPresenter;
            private List<DtoAuthor> dtoauthors = new List<DtoAuthor>();
            private int currentauthoridx;
    
            public AuthorView(IAuthorDM authorDm)
            {
                _authorDm = authorDm;
                InitializeComponent();
            }
    
            protected override void OnLoad(EventArgs e)
            {
                base.OnLoad(e);
                mPresenter = new AuthorViewPresenter(this, _authorDm);
                AuthorView_Load();
            }
    //code removed to keep it simple
    
            //IAuthorView implementation
     
            public int AuthorId { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string Message { get; set; }
    
            public event EventHandler<EventArgs> Send;
            public event EventHandler<EventArgs> Delete;
    
            public List<DtoAuthor> GetAuthors()
            {
                return mPresenter.GetAuthors();
            }
    

    namespace PubCompanyWinCore.Presenters
    {
        
        public class AuthorViewPresenter
        {
            private IAuthorView mView;
            private IAuthorDM _authorDm;
    
            public AuthorViewPresenter(IAuthorView authorView, IAuthorDM authorDm)
            {
                mView = authorView;
                _authorDm = authorDm;
                this.Initialize();
            }
    
            private void Initialize()
            {
                this.mView.Send += new EventHandler<EventArgs>(mView_Send);
                this.mView.Delete += new EventHandler<EventArgs>(mView_Delete);
            }
    
            private void mView_Delete(object sender, EventArgs e)
            {
                mView.Message = _authorDm.Delete(mView.AuthorId);
            }
    
            private void mView_Send(object sender, EventArgs e)
            {
                var dto = new DtoAuthor{AuthorId = mView.AuthorId, FirstName = mView.FirstName, LastName = mView.LastName};

    At the above point, the objects are passed from the form class to the presenter class, because of the IAuthorview interface. You can do the same thing  with any object in passing objects between classes.

     

    • Marked as answer by MSA_Aydn Wednesday, May 13, 2020 11:07 PM
    Friday, March 27, 2020 5:27 PM
  • I think many experts would say that it is better for the two user controls to not communicate directly. They could and should communicate with their owner; in other words, the form. There are many ways that can be done depending on requirements. So the following in previewTab should be avoided.

    Form1.Instance.PnlContainer.Controls.ContainsKey("resultsTab")
    Also, handling the mouse move event in the manner you are attempting to do might not work well. If there is a problem then it will be because there are so many of such messages. The system might have difficulty processing each one. You probably need to optimize what the mouse move events do each time.



    Sam Hobbs
    SimpleSamples.Info

    • Marked as answer by MSA_Aydn Wednesday, May 13, 2020 11:08 PM
    Friday, March 27, 2020 7:32 PM
  • I solved it,

    Thank you for your reply, 

    I trigger the clicked event of the button in the resultsTab in the main form and pass the parameter by making the picturebox in the resultsTab public.

    • Marked as answer by MSA_Aydn Wednesday, May 13, 2020 11:07 PM
    Wednesday, May 13, 2020 10:23 PM
  • I solved it,

    Thank you for your reply, 

    I trigger the clicked event of the button in the resultsTab in the main form and pass the parameter by making the picturebox in the resultsTab public.

    • Marked as answer by MSA_Aydn Wednesday, May 13, 2020 11:07 PM
    Wednesday, May 13, 2020 10:23 PM

All replies

  • If you want to pass an object between two classes, then you would use an Interface. A form and a usercontrol are classes where an Interface can be used to pass an object between classes. 

    Here is the concept where primitive types (objects) are being passed from a form class to a presenter class using an Interface between the two.

    namespace PubCompanyWinCore.IView
    {
        public interface IAuthorView
        {
            int AuthorId { get; set; }
            string FirstName { get; set; }
            string LastName { get; set; }
            string Message { get; set; }
    
            event EventHandler<EventArgs> Send;
            event EventHandler<EventArgs> Delete;
            
            List<DtoAuthor> GetAuthors();
        }
    }
    The 'this' is the passing of the IAutorview objects over to the presenter class.
    namespace PubCompanyWinCore
    {
        public partial class AuthorView : Form, IAuthorView
        {
            private IAuthorDM _authorDm;
            private AuthorViewPresenter mPresenter;
            private List<DtoAuthor> dtoauthors = new List<DtoAuthor>();
            private int currentauthoridx;
    
            public AuthorView(IAuthorDM authorDm)
            {
                _authorDm = authorDm;
                InitializeComponent();
            }
    
            protected override void OnLoad(EventArgs e)
            {
                base.OnLoad(e);
                mPresenter = new AuthorViewPresenter(this, _authorDm);
                AuthorView_Load();
            }
    //code removed to keep it simple
    
            //IAuthorView implementation
     
            public int AuthorId { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string Message { get; set; }
    
            public event EventHandler<EventArgs> Send;
            public event EventHandler<EventArgs> Delete;
    
            public List<DtoAuthor> GetAuthors()
            {
                return mPresenter.GetAuthors();
            }
    

    namespace PubCompanyWinCore.Presenters
    {
        
        public class AuthorViewPresenter
        {
            private IAuthorView mView;
            private IAuthorDM _authorDm;
    
            public AuthorViewPresenter(IAuthorView authorView, IAuthorDM authorDm)
            {
                mView = authorView;
                _authorDm = authorDm;
                this.Initialize();
            }
    
            private void Initialize()
            {
                this.mView.Send += new EventHandler<EventArgs>(mView_Send);
                this.mView.Delete += new EventHandler<EventArgs>(mView_Delete);
            }
    
            private void mView_Delete(object sender, EventArgs e)
            {
                mView.Message = _authorDm.Delete(mView.AuthorId);
            }
    
            private void mView_Send(object sender, EventArgs e)
            {
                var dto = new DtoAuthor{AuthorId = mView.AuthorId, FirstName = mView.FirstName, LastName = mView.LastName};

    At the above point, the objects are passed from the form class to the presenter class, because of the IAuthorview interface. You can do the same thing  with any object in passing objects between classes.

     

    • Marked as answer by MSA_Aydn Wednesday, May 13, 2020 11:07 PM
    Friday, March 27, 2020 5:27 PM
  • I think many experts would say that it is better for the two user controls to not communicate directly. They could and should communicate with their owner; in other words, the form. There are many ways that can be done depending on requirements. So the following in previewTab should be avoided.

    Form1.Instance.PnlContainer.Controls.ContainsKey("resultsTab")
    Also, handling the mouse move event in the manner you are attempting to do might not work well. If there is a problem then it will be because there are so many of such messages. The system might have difficulty processing each one. You probably need to optimize what the mouse move events do each time.



    Sam Hobbs
    SimpleSamples.Info

    • Marked as answer by MSA_Aydn Wednesday, May 13, 2020 11:08 PM
    Friday, March 27, 2020 7:32 PM
  • I solved it,

    Thank you for your reply, 

    I trigger the clicked event of the button in the resultsTab in the main form and pass the parameter by making the picturebox in the resultsTab public.

    • Marked as answer by MSA_Aydn Wednesday, May 13, 2020 11:07 PM
    Wednesday, May 13, 2020 10:23 PM
  • I solved it,

    Thank you for your reply, 

    I trigger the clicked event of the button in the resultsTab in the main form and pass the parameter by making the picturebox in the resultsTab public.

    • Marked as answer by MSA_Aydn Wednesday, May 13, 2020 11:07 PM
    Wednesday, May 13, 2020 10:23 PM