My function accepts user input and then do somework when user clicks ok.
private void cannyToolStripMenuItem_Click(object sender, EventArgs e)
{
canny();
}
private void canny()
{
// get user input
// if user clicks ok
if (ok button is clicked)
{
messagebox.show(" you clicked ok")
//
//do dome work
//
}
}
But I can't see any messagebox. What I am missing.
private void ok_Click(object sender, EventArgs e)
{
// should I add here some thing
}
what i am missing.
regards,
I think what you are trying to achieve is to get the result from a dialog box. If that is the case you want to do the following:
private void ShowDialogAndDoSomethingBasedOnTheResult()
{
DialogResult result = MessageBox.Show(
"Dialog text",
"Caption to go in title bar",
MessageBoxButtons.OK);
if (result == DialogResult.OK)
{
//Do work
}
}
See http://msdn.microsoft.com/en-gb/library/0x49kd7z.aspx for more examples.
Well, yes, you do:
private void ok_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Ok;
}
Which closes the dialog, it will only stay running as long as its DialogResult property is None. It isn't strictly necessary, you can also use the designer. Change the button's DialogResult property, now you don't need to write the code. That is however not often appropriate, you usually want to check if the user provided all the information you require. Ymmv.
Related
I'm trying to set the "txtMiles" textbox to focus after:
The form opens
When the "clear" button is clicked
I have tried using txtMiles.Focus(); but it doesn't seem to work for me.
CODE BEING USED ON THIS FORM
private void btnConvert_Click(object sender, EventArgs e)
{
//assigns variable in memory.
double txtMile = 0;
double Results;
try
{
// here is where the math happens.
txtMile = double.Parse(txtMiles.Text);
Results = txtMile * CONVERSION;
lblResults.Text = Results.ToString("n2");
txtMiles.Focus();
}
// if the user enters an incorrect value this test will alert them of such.
catch
{
//MessageBox.Show (ex.ToString());
MessageBox.Show("You entered an incorrect value");
txtMiles.Focus();
}
}
private void btnClear_Click(object sender, EventArgs e)
{
//This empties all the fields on the form.
txtMiles.Text = "";
txtMiles.Focus();
lblResults.Text = "";
}
private void btnExit_Click(object sender, EventArgs e)
{
// closes program
this.Close();
}
}
}
Thanks in advance for the help.
You should make sure your TabIndex is set and then instead of Focus(), try use Select(). See this MSDN link.
txtMiles.Select();
Also make sure there isn't a TabStop = true attribute set in the view file.
It's old, but someone could need this.
Control.Focus() is bugged. If it's not working, try workaround:
this.SelectNextControl(_controlname, true, true, true, true);
Change function parameters so they will work with your control, and remember about TabStop = true property of your control.
You already have your txtMiles focused after the clear-button click. As for the Startup, set txtMiles.Focus() in your load-method.
private void MilesToKilometers_Load(object sender, EventArgs e)
{
txtMiles.Focus();
}
using this solution worked perfectly...
txtMiles.Select();
I am using Windows Forms in c# and have problems with how and when the the different forms (i have 2) should close and not. It is very annoying since i feel that i should be able to fix it. But here we go.
I have two forms, one MainForm that calls another form called ContactForm.
The MainForm:
private void btnAdd_Click(object sender, EventArgs e)
{
ContactForm frmContact = new ContactForm();
int index = lstCustomers.SelectedIndex;
//If a customer is selected, export data for the selected customer to ContactForm
if (index != -1)
{
frmContact.ContactData = customerMngr.GetCustomer(index).ContactData;
}
if (frmContact.ShowDialog() == DialogResult.OK) //Show the ContactForm object
{
//The user has chosen the OK button - add the new customer object
customerMngr.AddCustomer(frmContact.ContactData); //??
UpdateCustomerList();
}
if (frmContact.ShowDialog() == DialogResult.Cancel)
{
return;
}
}
And the form that is called:
OK button.
private void btnOK_Click(object sender, EventArgs e)
{
if (ValidateInput())
{
this.DialogResult = DialogResult.OK;
this.Close();
}
}
Cancel button:
private void btnCancel_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Do you want to cancel and discard all data?", "Cancel input", MessageBoxButtons.YesNo,
MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
}
When the OK button in the ContactForm is used i want it to close, which works. When i press the cancel button, and no (in the box that appears), i want the form to stay open with the input still intact. Right now it doesn´t work.
Any ideas?
/Martin
Your code is alright. I think the problem lies in your Cancel Button itself. By this I mean that you probably attached (by designer or somewhere in code) DialogResul.Cancel to your button btnCancel.DialogResul property. To fix this simply set it to DialogResult.None.
If I'm right this is what is closing your second form.
See MSDN for more information.
guys!
I've got 2 forms in application - working form (frmMain) and form of settings (frmSettings).
There are two buttons on frmSettings - Save and Cancel. In frmMain I use the following approach to show the frmSettings:
private void btnSettings_Click(object sender, EventArgs e)
{
frmSettings = new SettingsForm();
frmSettings.ShowDialog();
// ...
}
The problem is I don't know, how to detect, which button was pressed on the frmMain - Save or Cancel. The further logic of the program depends on this fact. I need something like this:
private void btnSettings_Click(object sender, EventArgs e)
{
frmSettings = new SettingsForm();
frmSettings.ShowDialog();
if(/* frmSettings.SaveButton.WasClicked == true */)
{
InitializeServices();
}
// ...
}
Please, give me an advice, how to implement such kind of interaction between forms. Better without using global variables for saving buttons state.
Thanks beforehand.
ShowDialog returns a DialogResult object that allow you to know that. You have to:
On Save Button's click event, set this.DialogResult to DialogResult.OK
On Cancel Button's click event, set this.DialogResult to DialogResult.Cancel
private void btnSettings_Click(object sender, EventArgs e)
{
frmSettings = new SettingsForm();
if(frmSettings.ShowDialog() == DialogResult.OK)
{
InitializeServices();
}
//.......
}
Edited to manage the DialogResult as #tsiorn's answer: setting form's DialgoResult insted of setting that property on each button.
You chould use DialogResult to handle this. On your form settings window, you can set the result as so:
protected void btnSave_Click(object sender, EventArgs e) {
DialogResult = System.Windows.Forms.DialogResult.OK
this.close;
}
protected void btnCancel_Click(object sender, EventArgs e) {
DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.close;
}
Then ...
private void btnSettings_Click(object sender, EventArgs e)
{
frmSettings = new SettingsForm();
frmSettings.ShowDialog();
if(frmSettings.DialogResult == DialogResult.OK)
{
// save
InitializeServices();
}
// ...
}
Start with an enumeration of the possible values:
public enum ExitMethod
{
Other, //this should be first, as a default value
Save,
Cancel,
Error
}
Then make a property on SettingsForm of that type:
public ExitMethod ExitMethod { get; private set; }
In SettingsForm's save/exit methods set that property to the appropriate enum value, and in the main form you can read that property value.
in the frmSettings window you handle the Click events on the buttons. Then set the dialog result:
void frmSettings_Save_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
}
void frmSettings_Cancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}
in the main form you do something like this to capture and evaluate the result:
DialogResult answer = frmSettings.ShowDialog();
if (answer == DialogResult.OK)
{
...
}
Additional information and usage can be found here:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.dialogresult.aspx
private void frmSearch_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'bookdatabaseDataSet.Dist_Year' table. You can move, or remove it, as needed.
this.dist_YearTableAdapter.Fill(this.bookdatabaseDataSet.Dist_Year);
// TODO: This line of code loads data into the 'bookdatabaseDataSet.Dist_Auth' table. You can move, or remove it, as needed.
this.dist_AuthTableAdapter.Fill(this.bookdatabaseDataSet.Dist_Auth);
// TODO: This line of code loads data into the 'bookdatabaseDataSet.Book' table. You can move, or remove it, as needed.
this.bookTableAdapter.Fill(this.bookdatabaseDataSet.Book);
}
private void button1_Click(object sender, EventArgs e)
{
Form f4 = new Confirm();
f4.Show();
Hide();
}
private void button2_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Are you sure you want to Exit?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
Application.Exit();
}
}
my question is:
I want from the form to give me error message if I didn't check any of the check boxes. what is the correct code for it? and where should I right it? and thanks a lot for your concern.
form of windows application
For every checkbox do a checkbox.Checked test (boolean AND) and display a message box.
If you wanted to prevent the closing of the application then you have to handle the closing event and set CANCEL to true in this case.
void HandleFormClosing (object sender, CancelEventArgs args)
{
if (checkbox1.Checked && checkbox2.Checked)
return;
MessageBox.Show ("Need to check all boxes");
args.Cancel = true;
}
I guess you want to ensure at least one checkbox is checked when button1 is clicked, right? If so, place it at the beginning of button1_Click event.
private void button1_Click(object sender, EventArgs e)
{
if (!checkbox1.Checked && !checkbox2.Checked && !checkbox100.Checked)
{
MessageBox.Show("Please select a checkbox.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
checkbox1.Focus();
}
else
{
Form f4 = new Confirm();
f4.Show();
Hide();
}
}
This will not give you a direct answer (work-done-just-for-you kind of answer), but should point you in the right direction:
Finding the selected Radiobutton's value in ASP.NET
You can do this using custom validation.
But you could also get rid of the checkboxes and assume that if the user types in the textbox then they want to do a search on the field.
Start with search button disabled and only enable it when at least one field has text in it.
i would like to create a custom control for my crud buttons. The user controls will have the following code.
private void btnDelete_Click(object sender, EventArgs e)
{ if (MessageBox.Show("Are you sure to Delete?", "Please Confirm", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
DeleteClick(sender, e); // this will read the code from GUI delete button
if (DeleteResult.Success)
{
MessageBox.Show("This record is deleted successfully.");
}
else
{
MessageBox.Show("This record is deleted failed.");
}
}
}
My GUI
private void adminUserController_DeleteClick(object sender, EventArgs e)
{
Repository.Delete(user);
}
Question:
1) What is the best way to pass the DeleteResult boolean result from GUI to User control? How do i create an event for the delete button which can be hooked by the UI?
2) Do u think is good if i put my error logger code at the user control button?
replace the call in your adminUserController_DeleteClick with a call to a new method (foo)
private DeleteResult foo(TypeOfUser user)
{
return Repository.Delete(user);
}
and call foo instead of adminUserController_DeleteClick
foo will return the desired result