2

I have a windows custom control with a picturebox and a button on it's default panel. This windows custom control will be added to a windows form. There is another windows form called form2. when the user double clicks on the custom control, it should load form2. in designer when i double clicked the custom control on the form, it creates a load() event for that custom control. But i need double click event, how this can be done?

enter image description here

here is the graphical view of what happens enter image description here

Here is code in the control

 [DefaultEvent("DoubleClick")] public partial class cntrlImageLoader : UserControl { public cntrlImageLoader() { InitializeComponent(); } private void btnBrowse_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.InitialDirectory = Environment.SpecialFolder.MyPictures.ToString(); if (ofd.ShowDialog() == DialogResult.OK) { pbImage.Image = Image.FromFile(ofd.FileName); } } private void pbImage_Click(object sender, EventArgs e) { this.cntrlImageLoader_DoubleClick(pbImage, e); } private void cntrlImageLoader_Load(object sender, EventArgs e) { } private void cntrlImageLoader_DoubleClick(object sender, EventArgs e) { } } 

Here is the calling code on form1

private void cntrImLdrFront_DoubleClick(object sender, EventArgs e) { //this.cntrImLdrFront.pbImage.DoubleClick += new EventHandler(pbImage_DoubleClick); } FrmImageViewer f; // this is form2 private void pbImage_DoubleClick(object sender, EventArgs e) { f= new FrmImageViewer(); f.MdiParent = this.MdiParent; f.Show(); } 
2
  • 1
    Can you show some code. What you want exactly and what it is showing now? Commented Aug 8, 2014 at 3:52
  • Create a separate class for LoadPictureEventArgs Commented Aug 8, 2014 at 9:13

1 Answer 1

1

Assign the attribute [DefaultEvent("DoubleClick")] at the class declaration.

[DefaultEvent("DoubleClick")] public partial class MyControl : UserControl { } 

This will creates event which you have set by default on double click of control at design time where you have placed your user control.

EDITED:

[DefaultEvent("LoadPicture")] public partial class cntrlImageLoader : UserControl { public delegate void LoadPictureEventHandler(object sender, LoadPictureEventArgs e); public event LoadPictureEventHandler LoadPicture; private void pbImage_DoubleClick(object sender, EventArgs e) { if (LoadPicture != null) { LoadPictureEventArgs ev = new LoadPictureEventArgs(); LoadPicture(this, ev); if (ev.Picture != null) { pbImage.Image = ev.Picture; } } } } 

Create another class and give that class name to LoadPictureEventArgs

public class LoadPictureEventArgs : EventArgs { public Image Picture {get; set;} public LoadPictureEventArgs(Image _picture) { Picture = _picture } public LoadPictureEventArgs() : base() { } } 

HOW TO USE IT?

//FORM1 private void cntrImLdrFron_LoadPicture(object sender, LoadPictureEventArgs e) { Image img = null; //LOAD YOUR IMAGE HERE e.Picture = img; } 
Sign up to request clarification or add additional context in comments.

2 Comments

sorry this does not solve it, i have added the structure of the panel for better understanding
@PhillGreggan I have updated my answer please take a look. I think you need something like this. I have created an Event that will be called when you double click on the picture box and you can handle it in Form1. In that form you can browse the image and assign then picture to that user control.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.