1

Morning all,

I have a c# app where if you press a start button a dialog box will open and the OK button will be automatically pressed. The problem is I don't know how to do this.

The code is below:

private void Start_Click(object sender, EventArgs e)
{
    if (captureDevice.ShowDialog(this) == DialogResult.OK)
    {
         var videoSource = captureDevice.VideoDevice;

         FinalVideo = captureDevice.VideoDevice;
         FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);
         FinalVideo.Start();
    }
}

I have tried:

  1. Removing the if statement to directly run whats inside it
  2. Put DialogResult.OK = true before the if statement
  3. CaptureDevice.DialogResult.OK = true before the if statement;

Image shows the dialogbox when start is pressed

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
Kenny Barber
  • 245
  • 2
  • 11
  • if the only purpose of this dialog is to have the OK button automatically clicked, what's the point? Also, is this Windows Phone? Universal App? – Krzysztof Skowronek Aug 25 '18 at 08:37
  • If it always opens to the same spot in the screen, literally just move the mouse there and click it. –  Aug 25 '18 at 08:44
  • Even with the addition of the image, it is still not clear why you want *Ok* to be automatically/programmatically pressed when the dialog is shown. This prevents the user from selecting anything. – Baltasarq Aug 25 '18 at 08:44
  • Hi Krzyztof, when the start button is pressed it opens a dialog box that lets you choose a webcam, video resolution and video input. My webcam and the correct video resolution shows by default so I want to just by-pass this and start the webcam. I have added an image to my post. – Kenny Barber Aug 25 '18 at 08:45
  • Still not clear. If you want to bypass the dialog, don't show it at all! Remove the **if** block! – Baltasarq Aug 25 '18 at 08:46
  • My main goal is to be able to program the webcam to start at a specific time. The start button is just to see if I can automatically start the web cam without having to press OK. – Kenny Barber Aug 25 '18 at 08:47
  • Click the button in code – Peter Smith Aug 25 '18 at 08:48
  • I have tried removing the if statement but I get a null reference exception at the line: FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame); – Kenny Barber Aug 25 '18 at 08:49
  • If you want to skip this dialog you need to move the variable for the VideoCaptureDevice to a global class level and initialize if, BEFORE the start click. Then when you enter this event handler check if the VideCaptureDevice is not null and run directly the code that starts the video, otherwise ask info with the Dialog – Steve Aug 25 '18 at 08:52

3 Answers3

1

This dialog let you select the source capturing device. If you want to bypass this dialog you should specify source device in your code. if you use AForge.Net this link help you. if not search for appropriate solution in documentation of component or library you use.

Mojtaba Tajik
  • 1,725
  • 16
  • 34
0

Add a new button to your form. Call it "Settings". In the event handler for this button, you roughly put the first half of what you have now for the Start button. Create a Settings object in your MainForm in which you will store the camera chosen.

private void Settings_Click(object sender, EventArgs e)
{
    if (captureDevice.ShowDialog(this) == DialogResult.OK)
    {
        settings.VideoSource = captureDevice.VideoDevice;
    }
}

private void Start_Click(object sender, EventArgs e)
{
    FinalVideo = settings.VideoSource;
    FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);
    FinalVideo.Start();
}

Hope this helps.

Baltasarq
  • 12,014
  • 3
  • 38
  • 57
0

I have sort of found a solution to the question and it was to use:

SendKeys.Send("{ENTER}");

I used it before the if statement and it works with the Start_Click method but when i use it in a method called Start_Vid(), I get the error:

'SendKeys cannot run inside this application because the application is not handling Windows messages. Either change the application to handle messages, or use the SendKeys.SendWait method'

I have no idea why it should not work and what the error message means so should I be creating another question to have this answered or can it be solved in here do you think?

Kenny Barber
  • 245
  • 2
  • 11