Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using a custom control 1

Status
Not open for further replies.
Jun 17, 2004
73
US
How do i pass a parameter to a custom control?

I have a custom viewer control when the viewer loads I want it to display whatever image is passed to it. Right now I have it working where when the control is loaded a default image is displayed.


This is a code block from my control
Code:
 GetFile();
 SetImage();
 InitializeComponent();

...


private void GetFile()
   {
     FileName = "Y:\\KalimdorLoading.jpg";
   }

public void GetFile(string ExternalFileName)
   {
      FileName = ExternalFileName;
   }

And this is from the form that has the name of the image I want to pass to my control. It is hardcoded because I am still learning how to do it but eventually it will be dynamic.

Ive tried both these ways
Code:
public Form1()
    {
      InitializeComponent();
      ViewerControl ImageFileName = new            ViewerControl();
            ImageFileName.GetFile("Y:\\SCAN_21040_03-18-2005_SCS56371993.tif");
            
        }
Code:
public Form1()
    {
      ViewerControl ImageFileName = new ViewerControl();                    ImageFileName.GetFile("Y:\\SCAN_21040_03-18-2005_SCS56371993.tif");
 InitializeComponent();
        }


Right now it works. So my guess is I am not understanding how to use my public GetFile to set the image or the placement of it.

/cry
/help

[viking2]
LVL 60 ROGUE
 
Ok,

When you are doing GetFile() you are trying to Open the image?

I would use a different name to start so you don't get confused. Let's change it to OpenImage().

public class ViewerControl : System.Windows.Forms.UserControl
{
private PictureBox pic;
private string imagePath;

public ViewerControl()
{
InitializeComponent(); //Places your picture box for you via the designer
}

public void OpenImage(string path)
{
imagePath = path;
pic.Image = Image.FromFile(path);
}

public string GetImagePath()
{
return imagePath;
}
}

Then your calling code would do this:

private void button1_Click(object sender, EventArgs e)
{

//Create the viewer control
ViewerControl viewer = new ViewerControl();

//Open the image in your viewer
viewer.OpenImage(@"Y:\SCAN_21040_03-18-2005_SCS56371993.tif");

//Ask the viewer to tell us what image path is currently being used - just for fun
Console.WriteLine("Viewer Image Path Is " + viewer.GetImagePath());

}


Also: Always do InitializeComponent() first in a class, then put in your code. I noticed your Viewer stuff is above the InitializeComponent();
 
Thanks for the help and the pointers.

I am still having one problem. None of the controls(picturebox, labels, etc) in my control(the viewer) are letting me use them.

The path gets passed in and I see the result on the return(nice touch that helped me alot btw).

But for some reason the picturebox will not take the value. I tried just passing the imagepath into a plain label to see if it would display the value and it would not. So I tried making the controls public but it still did not work.

Thanks in advance.


/cry
/help

[viking2]
LVL 60 ROGUE
 
Can you post your code for your custom control? It's strange that you can't access all your controls.
 
I was able to get it to work, just the way i was passing the value.

/cry
/help

[viking2]
LVL 60 ROGUE
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top