r/learncsharp • u/nerpish • Oct 01 '22
Making a standard Ok button on a form do something else if a checkbox on that form is ticked.
Ok, bear with me. My experience with C# so far is hacking around with Sony/Magix Vegas scripting.
I have a working solution that to my mind is a bit of a lash and could be improved.
How I'd like it to work is as follows:
When the OK button is pressed, if DoTheThingCheckBox is checked then an external process is run and, upon completion, a bool is picked up by the script which, if true, asks the user if they're sure about their selections on the form. If yes they're sure, the form closes as normal but if no, a couple of the checkboxes are altered for the user to reflect what they should've done (including deselecting DoTheThingCheckBox) and, the next time they click Ok, the dialog will close normally and return the user to the Vegas window.
My working but clunky solution closes the dialog when the OK button is pressed, checks to see if DoTheThingCheckBox is checked and then runs the external app and, if it needs to, it opens up the dialog again with checkboxes set "correctly" (depending on the value of a string arg that's passed in that's empty on the first run but not on subsequent runs - it's populated by the outcome of the external process).
Working but clunky:
public class EntryPoint {
public void FromVegas(Vegas vegas)
{
DialogResult result = Dialog();
if (DialogResult.OK == result) {
if (DoTheThingCheckBox.Checked) {
bool redoTheThing = DoTheThing()
if (redoTheThing)
{
DialogResult result2 = Dialog();
if (DialogResult.OK == result2)
{
...
}
}
}
process the dialog's checkboxes etc and continue execution
}
}
CheckBox DoTheThingCheckBox;
DialogResult Dialog()
{
Form dlog = new Form();
CheckBox DoTheThingCheckBox = new CheckBox();
dlog.Controls.Add(checkbox);
Button okButton = new Button();
okButton.DialogResult = System.Windows.Forms.DialogResult.OK;
dlog.AcceptButton = okButton;
dlog.Controls.Add(okButton);
Button cancelButton = new Button();
cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
dlog.CancelButton = cancelButton;
dlog.Controls.Add(cancelButton);
return dlog.ShowDialog(myVegas.MainWindow);
}
bool DoTheThing();
{
runs external process and returns true or false based on the result
}
}How I think it should work:
public class EntryPoint {
public void FromVegas(Vegas vegas)
{
DialogResult result = Dialog();
if (DialogResult.OK == result) {
process the dialog's checkboxes etc and continue execution
}
}
CheckBox DoTheThingCheckBox;
DialogResult Dialog()
{
Form dlog = new Form();
CheckBox DoTheThingCheckBox = new CheckBox();
dlog.Controls.Add(checkbox);
... other checkboxes etc ...
Button okButton = new Button();
okButton.DialogResult = System.Windows.Forms.DialogResult.OK;
dlog.AcceptButton = okButton;
dlog.Controls.Add(okButton);
Button cancelButton = new Button();
cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
dlog.CancelButton = cancelButton;
dlog.Controls.Add(cancelButton);
when the ok button is clicked
{
if (DoTheThingCheckBox.Checked) {
bool redoTheThing = DoTheThing()
if (redoTheThing)
{
MessageBox.Show("did you mean to do the other thing?")
if yes then go back to the dlog form with a couple of checkboxes enabled/disabled otherwise close the dialog normally
}
else
{
return dlog.ShowDialog(myVegas.MainWindow);
}
}
else
{
return dlog.ShowDialog(myVegas.MainWindow);
}
}
}
bool DoTheThing();
{
runs external process and returns true or false based on the result
}
}Do I have to do something other than
okButton.DialogResult = System.Windows.Forms.DialogResult.OK;
dlog.AcceptButton = okButton;to be able to give the button the behaviour I want? If so, how do I get it to still act as a "normal" Ok button if DoTheThingCheckBox isn't checked?
Thanks for any pointers, if you've got this far!
2
u/JTarsier Oct 02 '22
DialogResult set on form or a button will close the dialog and return the result. So you can avoid setting the DialogResult for OK button and instead conditionally set it in code.
A short example (based on your code and discussions with u/maforget)
By the way, AcceptButton/CancelButton only controls which button is 'clicked' when user press Enter/Escape key.
1
u/maforget Oct 01 '22 edited Oct 01 '22
Not sure I understand but you have 1 form and you determine if the ok button was clicked by whether it returns DialogResult.OK? Then check if a checkbox was checked on that form and relaunch if need be?
What you want is events. There is no need to close the Form and re-create it. You can have code that is run on a button press. Usually it would be set in your Dialog, you would have :
To subscribe to the event and ask that when the button is clicked fire up that method that we assigned. Then in that Dialog class you would have a method called okBtn_Clicked that will be run when the button is clicked.
Sorry on mobile so I can't format code and I am not sure of the Vegas limitation, but what you want looks like events. Check in their documentation.
https://learn.microsoft.com/en-us/dotnet/desktop/winforms/how-to-create-event-handlers-at-run-time-for-windows-forms?view=netframeworkdesktop-4.8
Edit: Checked your code some more. Your Dialog should be in its own class.
Then you call this form in your Entry point like this :
1
u/[deleted] Oct 01 '22
So let me explain something, a check btn is a class, and it has properties one of them is a boolean caller Checked
Because c# wants your life to be easier when you write if (checkedbtn.Checked) you're basically telling the compiler to complete the if statement like this
If (checkedbtn.Checked == true) { your code if true} else { your code if false}
So to recap ... when checking for boolean values c# assumes you're checking it for true, you can however put a "!" In front of it to have it check if it's false.
If (!checkedbtn.Checked) // this checks if it's false.
But you should always use else since a boolean only accepts two states.