Question Use picturebox to access another form

Nichan-san

Member
Joined
Jan 6, 2020
Messages
6
Programming Experience
Beginner
Hey, I'm back with a quick question-

assuming these exist, how can I use a picturebox click event to access another form?
 
A form is just an object. How are you currently accessing other objects from your click event handler?
 
In exactly the same way as you would use the Click event of a Button to do the same thing. The code to display a form is the same no matter when and where you want to display it. Which event handler you put it in is irrelevant to the code you use to perform a task.
 
Why not? Like most objects with a click event, they function the same way. If you were doing it with a button, how would you do it?


I used to do VB Windows Forms. Tried using


C#:
private void PbMonitor_Click(object sender, EventArgs e)
{

     this.Hide();
     Form2.Show();
}


but .show doesn't exist. What's similar that I can use in C#?

Again, just a beginner so I'm not good with tech terms.
Appreciate the support :)
 
Try creating an object reference of the object you want to show.
C#:
            Form2 obj_RefFrm2 = new Form2();
            obj_RefFrm2.Show();
 
So the issue here had nothing to do with a PictureBox or a Click event but was purely because you were trying to use a default instance of a form in C# when that is a VB-specific feature. What you posted in post #6 is what you should have posted in post #1. If you have code that doesn't work, ALWAYS show us that code.

For the record, the vast majority of experienced developers will tell you not to use default instances in VB either. They exist primarily to allow VB6 developers to use forms in VB.NET in essentially the same way as they did in VB6 and they also allow beginners to access forms easily across a project without having to understand the principles of OOP. It's actually better that you learn the principles of OOP sooner rather than later and treat forms as objects like any other. I've never heard any C# developer say that they wish that C# supported default instances so there's clearly no actual need for them.

For more specific information on default instances, you may like to read this.
 
So the issue here had nothing to do with a PictureBox or a Click event but was purely because you were trying to use a default instance of a form in C# when that is a VB-specific feature. What you posted in post #6 is what you should have posted in post #1. If you have code that doesn't work, ALWAYS show us that code.

For the record, the vast majority of experienced developers will tell you not to use default instances in VB either. They exist primarily to allow VB6 developers to use forms in VB.NET in essentially the same way as they did in VB6 and they also allow beginners to access forms easily across a project without having to understand the principles of OOP. It's actually better that you learn the principles of OOP sooner rather than later and treat forms as objects like any other. I've never heard any C# developer say that they wish that C# supported default instances so there's clearly no actual need for them.

For more specific information on default instances, you may like to read this.

Okay, thanks for the advice. I'll include the code from my forms if something like this happens again.
 
Back
Top Bottom