Automatically clicking OK button in c# app - c#

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:
Removing the if statement to directly run whats inside it
Put DialogResult.OK = true before the if statement
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?

Related

How to detect if a window is closed C#, Windows Form App?

So, I'm making a payroll management system as a hobby project to help my resume and general knowledge of c#. So, I'm making a UI and I can open a new window just fine with this code:
private void button1_Click(object sender, EventArgs e)
{
CreateAdminAcct createAcct = new CreateAdminAcct();
createAcct.StartPosition = FormStartPosition.CenterScreen;
createAcct.Show();
this.Hide();
}
however, I don't know the event to check when the little red "x" button is clicked, because when that button is clicked, I want to go back to the main screen because I hide the main screen when that button is clicked, and when i click the red "x" on the screen that just opened, it closes, but the application continues to run in the background.
If there is some better way to manage multiple menus, I'm open to suggestions, however, this is what I've found easiest.
Thanks in advance
I second Robert Harvey's suggestion; this gives the user the reassurance tha tht emain window is still open/ nothing got lost, but it's unreachably "behind" the CreateAdminAcct form while the CreateAdminAcct form is open
private void button1_Click(object sender, EventArgs e)
{
CreateAdminAcct createAcct = new CreateAdminAcct();
createAcct.StartPosition = FormStartPosition.CenterScreen;
createAcct.ShowDialog();
//do any code here that needs to access createAcct before it's lost
MessageBox.Show(createAcct.NewAdmin.Name);
}
If you really do want to hide your main form, pass the main form itself to createAcct, and make it createAcct's job to re-open the main form when it is closing
private void button1_Click(object sender, EventArgs e)
{
CreateAdminAcct createAcct = new CreateAdminAcct(this); //note passing this form to constructor
createAcct.StartPosition = FormStartPosition.CenterScreen;
createAcct.Show();
}
class CreateAcctForm : Form{
private Form _showWhenClosing;
CreateAcctForm(Form revertTo){
InitializeComponent();
_showWhenClosing = revertTo;
}
}
void Form_Closing(object sender, ...){ //event
_showWhenClosing.Show();
}
Side note: please rename your controls after you drop them ona form. code that's stuffed with label57, textbox25 is effectively obfuscated and really wearisome to follow

C# Wait for SendKeys to finish sending before executing next line of code?

I think the title explains what I am asking.
I need the SendKeys to finish sending the keys before the DoMouseClick method or my entire program is useless.
// Simulate the keyboard typing the value of 'i'
SendKeys.SendWait(i.ToString());
// Simulate mouse click so the Accept button in-game is clicked
DoMouseClick();
I tried using Thread.Sleep but I was hoping you guys had some better suggestions on how to fix my problem.
Use Input Simulator. It's much better than SendKeys, and handles the edge cases internally.
Install-Package InputSimulator
var simulator = new InputSimulator();
//simulate what you need
simulator.Keyboard.KeyPress(VirtualKeyCode.VK_I);
simulator.Keyboard.TextEntry(i.ToString());
simulator.Mouse.LeftButtonDoubleClick();
I am not sure if this helps, but if not critique this post so I can try to help in your situation.
private void Form1_DoubleClick(object sender, MouseEventArgs e)
{
// Send the enter key; since the tab stop of Button1 is 0, this
// will trigger the click event.
SendKeys.SendWait("{ENTER}");
CallAfterSend();
}
private void CallAfterSend()
{
MessageBox.Show("Had to hit enter first");
}
private void button1_Click_1(object sender, EventArgs e)
{
MessageBox.Show("Click here!");
}

Show dialog on first start of application

Is there an easy way to show an dialog when the program is started for the first time (and only the first time), for some kind of instruction or specifying settings?
You could save it as a bool in your settings and you should check at load event of first form.
Your settings file should have a setting that I called "FirstRun" do this with following steps:
Right click your Project
Click "Properties"
Click "Settings" tabpage(probably on the left)
Add setting like I did as seen in image above
Note: The Scope can be changed to "Application", if that is your application's need, since you didn't mention in your question.
Your Settings file should look like image below:
public void Form1_Load(object sender, EventArgs e)
{
if((bool)Properties.Settings.Default["FirstRun"] == true)
{
//First application run
//Update setting
Properties.Settings.Default["FirstRun"] = false;
//Save setting
Properties.Settings.Default.Save();
//Create new instance of Dialog you want to show
FirstDialogForm fdf = new FirstDialogForm();
//Show the dialog
fdf.ShowDialog();
}
else
{
//Not first time of running application.
}
}
Note: wrote this from my phone, so I couldn't compile to test
Edit: Checked code and added image from desktop.
You can have bool value in your settings file which is a "user setting" which means you can change it to true save it for this specific user.
When your application starts just check that value. If it's false show your dialog and change it to true and it will stay true.
public void Form_Load(object sender, EventArgs e)
{
if(Settings.Default.ShowDialog)
{
Settings.Default.ShowDialog = false;
Settings.Default.Save();
// show first disalog
}
// rest of code if needed
}
Here's an MSDN link on user settings:
http://msdn.microsoft.com/en-us/library/bb397750(v=vs.110).aspx
Ok, so I assume you're creating WinForms application. First of all, locate the Load event in your main Form event lists (or simply double click your Form in Designer panel). The following method stub will pop up:
public void Form1_Load(object sender, EventArgs e)
{
}
And modify it like this:
public void Form1_Load(object sender, EventArgs e)
{
MessageBox.Show("Your message here");
}

Form_Activated is messing up closing the program

I'm currently trying to bring two forms to the front when I activate them from the Task bar or via Alt+Tab. The problem is that I can't close the main form if the second one exists:
This is the code I use
private void Haupt_Activated(object sender, EventArgs e)
{
cardLibrary.Focus(); //Focus the second form
this.Focus(); //refocus the first one
}
What am I doing from?
EDIT:
Haupt is the main form.
CardLib is variable in Haupt
this is how CardLib is called:
private void cMenuOpenLibrary_Click(object sender, EventArgs e)
{
if (!Config.libOpen)
{
if (cardLibrary == null) cardLibrary = new CardLib(this);
cardLibrary.Show();
cardLibrary.Left = this.Left - cardLibrary.Width - 5;
cardLibrary.Top = this.Top;
}
}
EDIT 2: Sry I was being dumb. Adding closing the CardLib via Haupt_FormClosed fixed it.
If you are calling second form from
Second.ShowDialog();
then this will create this problem as a dialog retains focus until it is closed
Instead of
Second.ShowDialog();
use
Second.Show();
How can this.focus in second form focus the first form???
this.Focus(); //refocus the first one

Winform appears twice

I am working on a personal project in winforms just to gain some experience in it since I've never had the chance to work with it before. So, I'm quite the n00b when it comes to Winforms. This is the error I'm encountering:
In form BudgetTracker, I have a button called 'AddCat'. Below is the form's constructor and the button's click eventHandler:
public form_BudgetTracker()
{
InitializeComponent();
setEvents();
}
public void setEvents()
{
this.btn_AddCat.Click += new System.EventHandler(this.btn_AddCat_Click);
}
private void btn_AddCat_Click(object sender, EventArgs e)
{
form_NewCat NewCatForm = new form_NewCat();
var NewCatFormResult = NewCatForm.ShowDialog();
NewCatForm.Show();
}
In the NewCat form that comes up, I have a Cancel button. Code:
public form_NewCat()
{
InitializeComponent();
SetEvents();
}
private void SetEvents()
{
this.btn_Add.Click += new System.EventHandler(this.btn_Add_Click);
this.btn_Cancel.Click += new System.EventHandler(this.btn_Cancel_Click);
}
private void btn_Cancel_Click(object sender, EventArgs e)
{
this.Close();
}
The problem I'm facing is, when I click Add, the new form comes up. At this point, if I click Cancel, the form disappears but instantly a new instance of the form appears. I then click cancel again, and the form disappears.
What part of my code is making the form appear twice. I checked the contructors etc, but couldn't figure it out. Any help or pointers would be appreciated.
PS - As I mentioned, I'm new to winforms programming, so any cues or pointers would be appreciated as well.
private void btn_AddCat_Click(object sender, EventArgs e)
{
form_NewCat NewCatForm = new form_NewCat();
var NewCatFormResult = NewCatForm.ShowDialog(); // <-- opens the first time
NewCatForm.Show(); // <-- opens the second time
}
Judging from your code, you're simply showing the form twice!!!
form_NewCat NewCatForm = new form_NewCat();
var NewCatFormResult = NewCatForm.ShowDialog();
NewCatForm.Show();
The second line shows the form and blocks the method until DialogResult is set, then the third line shows the form without blocking the method.
Simply remove the third line!
Try stepping through the code using the F8 key instead of running it, or hitting F5. It will show you line by line what it's about to execute.
delete NewCatForm.Show();

Categories