Shmuel Naukarkar
Shmuel Naukarkar

Reputation: 263

How can I easily replace one control for another with the same name in Windows Forms?

The first problem is that in form1 top I have:

ListViewNF lvnf;

The ListViewNF is a class in form1:

class ListViewNF : System.Windows.Forms.ListView
        {
            public ListViewNF()
            {
                //Activate double buffering
                this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);

                //Enable the OnNotifyMessage event so we get a chance to filter out 
                // Windows messages before they get to the form's WndProc
                this.SetStyle(ControlStyles.EnableNotifyMessage, true);
            }

            protected override void OnNotifyMessage(System.Windows.Forms.Message m)
            {
                if (m.Msg != 0x14)
                {
                    base.OnNotifyMessage(m);
                }
            }
        }

Then in the form1 constructor I'm making instance for the lvnf variable set the size, location and add this control lvnf to the form1 controls. Then in my program I'm using the lvnf in some places.

But now I want to make a new UserControl call it lvnf so when I drag the UserControl from the toolbox to the form1 designer it will replace the lvnf I created.

The first problem is once I delete the line:

ListViewNF lvnf;

I'm getting many errors and can't see the form1 designer I'm getting an error since lvnf not exist.

If I don't delete it and drag the new UserControl it will give the new control another name and I want it to be lvnf. I don't want to change anything in my run time code related to the lvnf only the control in the designer.

This is the code of the UserControl:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Pop3_Emails
{
    public partial class ListViewNF : UserControl
    {
        public ListViewNF()
        {
            InitializeComponent();           
        }
    }
}

Should I make all the events of the lvnf to move them to the UserControl code? Or leave them all on form1?

The main problem is how to use the UserControl as lvnf. Also I need to use the code in the class ListViewNF.

What a mess. In general what I want to do is to make the lvnf the ListViewNF a control in the form1 designer instead a control in the run time code.

Upvotes: 2

Views: 1195

Answers (1)

BartoszKP
BartoszKP

Reputation: 35891

  1. Drag the new UserControl onto the form, under a different name (x for example)
  2. Rename the lvnf to x, asking Visual Studio to update all references. Ignore the warning that the name already exists, and the project won't compile.
  3. Remove the ListViewNF x declaration (now the project should compile again).
  4. Rename x back to lvnf, also updating all references.

Upvotes: 2

Related Questions