User control button - c#

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

Related

How to do something after clicking on MessageBox button, but before this MessageBox closes?

I have a list of "projects" with some informations in textBoxs for each project. The user can select a project then modify the informations and click on save button after that.
If I changes selected project without save the modifications, a Yes/No MessageBox appear:
DialogResult dialogResult = MessageBox.Show(
"Do you want to save changes ?",
"Title",
MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
//Click Yes
}
else
{
//Click No
}
I would to refresh all the project list (with my own Refresh() methode) after clicking on Yes/No button, but staying on the MessageBox until the refresh is done.
Is it possible?
The built in MessageBox class does not allow such complicated behaviour.
One way to do this is to create your own message box. Create a subclass of Form, add some labels and buttons. Expose some events like YesClicked and NoClicked.
In your main form, create an instance of your custom message box, subscribe to the events, and call ShowDialog on it.
After the refresh is done, you can call Close or Dispose on your custom message box to close it.
Try creating a custom message box. I commented the code, let me know if you need clarification
public static class MessageBoxResult
{
public static int dialogResult; // <== i use this value to determine what button was pressed
}
// your custom message box form code
public partial class CustomMsgBox : Form
{
public CustomMsgBox()
{
InitializeComponent();
}
public void show(string pos0, string pos1, string pos2, string message) //<=== initializing the message box with the values from your main code
{
button1.Text = pos0;
button2.Text = pos1;
button3.Text = pos2;
label1.Text = message;
}
// message box events to set the static field incase a button on the custom form was changed
private void button1_Click(object sender, EventArgs e)
{
MessageBoxResult.dialogResult = 0;
this.Close();
}
private void button2_Click(object sender, EventArgs e)
{
MessageBoxResult.dialogResult = 1;
this.Close();
}
private void button3_Click(object sender, EventArgs e)
{
MessageBoxResult.dialogResult = 2;
this.Close();
}
}
//usage
{
MessageBoxResult.dialogResult = -1; // <== setting the static field to -1 to mean nothing was pressed
CustomMsgBox cMsgBox = new CustomMsgBox();
cMsgBox.show("your message");
cMsgBox.ShowDialog();
}
You can change what happends, based on what button is clicked, with the DialogResult
DialogResult dialogResult = MessageBox.Show(#"Some body", #"Title", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
// do stuff
}
No, a message box cannot do this. It's not meant to do this. It's meant to just display a message... in a box :)
What you can always do is create your own window that looks like a message box and behaves like a message box, but actually does things when you click the buttons.

How to handle form closing events

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.

Interaction between forms in WinForms application, c#

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

question about windows forms checkbox

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.

user input and acceptance inside a function

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.

Categories