简体   繁体   中英

Automatically clicking OK button in c# app

Morning all,

I have ac# 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

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.

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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM