# Setting DialogResult.OK to true when button is pressed in webcam C# Forms App

#### hunterage2000

Joined May 2, 2010
487
Hi all,

I have an app where if you press a button, a dialog box pops up and gives you a choose of available webcams, screen resolution and inputs (see attached image). When I press start, my webcam shows automatically with the correct screen resolution but I need to press OK.

I want to be able to program the webcam to start at anytime which means I need a way to automatically press the OK button. The below code shows the function.

Code:
private void Start_Click(object sender, EventArgs e)
{
if (captureDevice.ShowDialog(this) == DialogResult.OK)
{
VideoCaptureDevice videoSource = captureDevice.VideoDevice;
FinalVideo = captureDevice.VideoDevice;
FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);
FinalVideo.Start();
}
}
I have tried to remove the if statement so it bypasses the dialog box but I get a null exception error at the line
FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);
I thought it would make sense to set DialogResult.OK to true but that gives an error.

#### Attachments

• 57.1 KB Views: 4

#### MrSoftware

Joined Oct 29, 2013
2,059
When your start button is clicked, it triggers an event. The function Start_Click() is the event handler that executes when the button click event is fired. So what you need is a way to programmatically trigger the event. You can do it several different ways, here's one discussion on it:

https://stackoverflow.com/questions/12184614/trigger-controls-event-programmatically

If you don't want the selection dialog to show at all and you want to skip right to capturing, then you need to figure out what value is null on your line 7, then figure out if that value is assigned inside the dialog that pops up when captureDevice.ShowDialog(this) is called inside your if(). I suspect your captureDevice is set after the user makes a selection in your captureDevice.ShowDialog() code, so you probably need to make this assignment yourself instead of asking the user to make the selection.

Last edited:

#### joeyd999

Joined Jun 6, 2011
4,626
Just a guess:

captureDevice is a VideoCaptureDeviceForm object. Calling ShowDialog (perhaps) initializes (or properly sets) one or more of the required properties to properly start the webcam.

I assume you have a debugger? Look at the property values before and after executing the ShowDialog() method. See what is being changed.

You should be able to manually (programatically) set the same properties without calling ShowDialog().

Again, just a guess.