0

Can anyone help me answering to my question that how can we implement the click events for dynamically generated component inside Designer.

I had referred The DesignSurface (Extended) Class is Back, Together with a DesignSurfaceManager (Extended) Class and a Form Designer Demo![^] website

I have use below snippet inside

class MenuCommandServiceExt:IMenuCommandService

class but didn't got any success.

if ((string)menuItem.Tag == "Label")
                {
                  var lbl1 = (Label)surface.CreateControl(typeof(Label),
                   new Size(300, 20), new Point(10, 80));

                  this.lbl1.Text = "Hello World by DesignSurfaceExt";
                  this.lbl1.Click += new EventHandler(btnclick_Click);
                  this.lbl1.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.lbl_MouseDoubleClick);   
                }

I also tried to achieve it through creating a user control for Label with all the events defined inside the label and creating it through above code snippet but it also didn't worked. Below is the logic I used for user control.

if ((string)menuItem.Tag == "Label")
                {
                  var lbl1 = (MyLabel)surface.CreateControl(typeof(MyLabel),
                   new Size(300, 20), new Point(10, 80)); 
                }

User Control

public class MyLabel : Label
    { 
     public MyLabel()
        {

            this.DoubleClick += new EventHandler(MyLabel_Click);
        }

 void MyLabel_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Clicked");
        }
.........
.....
...
}

Also, I want to save/view my custom form which I built inside the Powerful DesignSurface Designer. How can I achieve that?

AMIT SINHA
  • 11
  • 2

1 Answers1

-1

You just cast an UserControl to type MyLabel:

var lbl1 = (MyLabel)surface.CreateControl(typeof(MyLabel), new Size(300, 20), new Point(10, 80));

So, it doesn't create an object by constructor. You should create an object by constructor then add to your "surface" object.

MyLabel label1 = new MyLabel();
label1.DoubleClick += new EventHandler(MyLabel_Click);
surface.Controls.Add(label1);
}

void MyLabel_Click(object sender, EventArgs e)
{
MessageBox.Show("Clicked");
}

Hope this helps.

anhtv13
  • 1,636
  • 4
  • 30
  • 51