简体   繁体   中英

Using Events for TextBox, Label, Button Control inside Design Surface

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM