Modal Dialog Box - c#

I have created two forms in my Windows Application.
One Form acts as a Form and the other form acts as a MODAL DIALOG BOX.
The Form Dialog Box contains a button and One textBox.
When this button is clicked the MODAL DIALOGBOX should be displayed.
This dialog box also contains One Textbox and Two Buttons(Ok and Cancel).
Now when this dialog box is displayed the TextBox of the dialog box should contain the value entered in the textbox of Form1.
I have used the following coding to accomplish this task.
Form1 Coding:
public string UserName;
private void btnFn_Click(object sender, EventArgs e)
{
UserName = txtUserName.Text;
frmFnC objFnC = new frmFnC();
objFnC.ShowDialog();
objFnC.txtUserName.Text = UserName;
}
Code in MODAL DIALOGBOX OK button:
Please note that the Cancel button is enabled only when the OK button is clicked.
Coding:
private void btnOk_Click(object sender, EventArgs e)
{
btnCancel.Enabled=true;
}
private void btnCancel_Click(object sender,EventArgs e)
{
this.Close();
}
The problem I am facing is the value entered by the User in the USERNAME textbox is not displayed in the TEXTBOX in the MODAL DIALOG BOX. Instead it is displaying the textbox as empty.
What should I do to get the values entered by the user in the textbox to this modal dialog box?
Can anybody help me out in performing the desired task?
Thanks in advance!!!

The problem you've got is that you're showing the dialog before you set the username.
//this shows your dialog
objFnC.ShowDialog();
//this won't happen until the dialog is closed
objFnC.txtUserName.Text = UserName;
Because the dialog is modal it won't go to the next line until the dialog is closed. You want to swap those lines round and it'll be fine.
//do this first
objFnC.txtUserName.Text = UserName;
//then show your dialog
objFnC.ShowDialog();
I'd like to point out that exposing the textbox publically isn't a really good idea though. You don't want the consumer to have implementational knowledge of your dialog.
It would be better if you added a parameter to the form constructor and then set the textbox text from within that. Then you could do the following:
//get the username
string userName = txtUserName.Text;
//create a new form passing in the username
frmFnC objFnC = new frmFnC(userName);
//display the form
objFnC.ShowDialog();
That way, the consumer isn't relying on frmFnC having a textbox named txtUserName which means you're free to change the inner workings of how you display the username. For example, you could change it to a label and you wouldn't break the consumer's code! All the consumer needs to know is that they should pass a username into the constructor.

Change:
objFnC.ShowDialog();
objFnC.txtUserName.Text = UserName
To:
objFnC.txtUserName.Text = UserName
objFnC.ShowDialog();

Set the text field in the dialogue before calling ShowDialog:
private void btnFn_Click(object sender, EventArgs e)
{
UserName = txtUserName.Text;
frmFnC objFnC = new frmFnC();
objFnC.txtUserName.Text = UserName;
objFnC.ShowDialog();
}

You need to swap the setting of the text and the ShowDialog:
public string UserName;
private void btnFn_Click(object sender, EventArgs e)
{
UserName = txtUserName.Text;
frmFnC objFnC = new frmFnC();
objFnC.txtUserName.Text = UserName; // SET THE DATA BEFORE SHOWING THE DIALOG
objFnC.ShowDialog();
}
or force a redraw of the dialog afterwards.

In order to be able to set (and get) the contents of the text box in the modal form, add this code to that form:
public string UserName
{
get { return txtUserName.Text; }
set { txtUserName.Text = value; }
}
Then, in the other form, you can set the user name:
frmFnC objFnC = new frmFnC();
objFnC.UserName = txtUserName.Text;
objFnC.ShowDialog();
I also need to ask you about the relation between the OK and Cancel buttons in the modal dialog form; it seems a bit wierd the user would need to first click OK, in order to get the Cancel button enabled, and then click Cancel to actually close the form.
I would suggest to not have any event handlers for the click events of these buttons, but instead set the appropriate values of their DialogResult property, and then set the modal form's AcceptButton and CancelButton properties. That way you can check how the dialog was closed:
frmFnC objFnC = new frmFnC();
objFnC.UserName = txtUserName.Text;
if (objFnC.ShowDialog() == DialogResult.OK)
{
// the user clicked the OK button
}
else
{
// the user clicked the Cancel button
}

Related

How to set DialogResult AFTER button is pressed?

I am creating a password window using Windows Form Designer, except the context is a little different. There are several input fields, and one of them is password protected. There is a "Change" button that spawns a new password window on top of the home window. The user enters a password attempt and presses "OK". I need a way to have the OK button check the password and then send a DialogResult.OK back to the home window, or display an "incorrect password" if the attempt is incorrect. This means I can't set the DialogResult to DialogResult.OK initially, so I'm not sure how to do this.
Currently I set the DialogResult to DialogResult.OK in the click event function, but obviously this sets it for the next click, not the current one, so the user has to press the OK button twice.
private void buttonOK_Click(object sender, EventArgs e)
{
string passwordAttempt = textBoxPassword.Text;
if( passwordAttempt.CompareTo("pass") == 0 )
{
this.buttonOK.DialogResult = System.Windows.Forms.DialogResult.OK;
}
}
One possible solution is to trigger a second event through the code (not sure how to do this). Or alternatively, is there a better way to do password windows I'm not thinking of in this situation?
Set the AcceptButton of the dialog to buttonOK (the OK button). You can do this either in code or designer.
Set the DialogResult of the dialog form, not the button.
Code:
this.AcceptButton = buttonOK;
...
private void buttonOK_Click(object sender, EventArgs e)
{
string passwordAttempt = textBoxPassword.Text;
if (passwordAttempt.CompareTo("pass") == 0)
{
this.DialogResult = System.Windows.Forms.DialogResult.OK;
Close();
}
}

how to open a window from a window exist

I have a main window, inside there is a button which in a click-
open a dialog window to write a name save a name in a textbox and need to open another window.
I want it will open the new window at the main window-
but it is opened the window in the dialog window, allthoght I set the Owner to be the main window..
what should I do?
this is the code in a button of the dialog window:
Screen myScreen = new Screen (name, ViewModel, mainWindow);
myScreen.Owner = mainWindow;
myScreen.Show();
this.Close();
This is how you should do it:
In your first dialog, you must have a public variable,something like:
public string TextBoxContent;
And in the OK button event, you'll do something like:
this.TextBoxContent = TextBox.Text;
this.DialogResult = true;
this.Close();
In your main window you must do this:
string returnedString;
DialogWindow w = new DialogWindow ();
w.ShowDialog();
if (w.DialogResult.HasValue && w.DialogResult.Value)
returnedString=w.TextBoxContent;
And after that, you'll show your second dialog with the returned string. Hope you see the logic.
Ok you have three windows one is MainWindow which have a button to open another window name Dialog which have a textBox for input name and a button for pass that textBox input to another window name Screen. SO I have take a textBox in Screen window named textBoxName which will show the passed text, here is my solution
the main window button show dialog this way
private void buttonMainShowDialog_Click(object sender, RoutedEventArgs e)
{
Dialog dl = new Dialog();
dl.ShowDialog();
}
Constructor for Screen window as below which take text as a parameter by which we pass the Dialog window textBox text
public Screen(string text)
{
InitializeComponent();
this.textBoxName.Text = text;
}
and you need to call this screen window from dialog window as below
private void buttonDialogShowScreen_Click(object sender, RoutedEventArgs e)
{
Screen myScreen = new Screen(this.textBox.Text);
myScreen.Show();
this.Close();
}
its works fine, hope this help you

How to prevent modal dialog form to close after pressing OK?

I have a (login) form that use it as modal like this (parent form code):
using (var login = new Login())
{
login.ShowDialog();
}
I do some checks on opened modal dialog and I want that it not close on pressing OK button if user name and password was wrong.
My login:
private void goSignIn_Click(object sender, EventArgs e)
{
var loggedInCustomer =LoginController.signIn(usernameBox.Text, passwordBox.Text);
if (loggedInCustomer == null)
{
MessageBox.Show("Wrong username or password! :( ", "Wrong!");
}
else
Close();
}
Check the button (goSignIn) DialogResult property. If it's set, it automatically close the form.
If the DialogResult for this property is set to anything other than None, and if the parent form was displayed through the ShowDialog method, clicking the button closes the parent form without your having to hook up any events. The form's DialogResult property is then set to the DialogResult of the button when the button is clicked.
MSDN Button.DialogResult Property
I guess you've set the DialogResult of goSignIn button to some value(probably DialogResult.OK), remove that line, everything should work fine as expected.
Don't call close then
private void goSignIn_Click(object sender, EventArgs e)
{
var loggedInCustomer =LoginController.signIn(usernameBox.Text, passwordBox.Text);
if (loggedInCustomer == null)
{
MessageBox.Show("Wrong username or password! :( ", "Wrong!");
}
else
{
Close();
}
}

WPF showdialog Textbox with var content

I try to get the following done:
A WPF application where i have multiple buttons where you can set a notification message.
Depending on the button, you can set different messages.
What i did, was on the message button i have put this code:
private void button1_Click(object sender, RoutedEventArgs e)
{
CounterMessage msgOne = new CounterMessage();
msgOne.ShowDialog();
}
This will open op a new WPF window here only is a textbox and an exit button.
On exit in this message window, it will save the message to a parameter.
But here is the trick.
I want to use this message window for multiple notifications, and it will display in the textbox any text content if there is already any on a string in the application.
So for example:
In the main app i have button A and B to set the notification on.
I click on button A, the showdialog pops up and in the textbox already have "you clicked button A"
If it was button B that has been clicked, it should display "you clicked button B"
So i should sent some extra info with the ShowDialog, so i can use the messagewindow for each one.
Could someone help me out a bit herE?
I must say i find it a bit hard do decently discribe what i want, so i hope i made myself clear enough.
EDIT
So hat i want is showing the content of a string parameter (to be exact: Properties.Settings.Default.XXX) into the textbox that is in the Countermessage window
I am not entirely sure what you are asking, but it sounds like you want something like this. I am assuming that CounterMessage is a Window, and that there is some binding mechanism or property that displays what the message is.
public class CounterMessage : Window
{
public CounterMessage(string message)
{
this.Message = message;
}
public string Message
{
get;
set;
}
}
Your button event would then be something along the lines of:
private void button1_Click(object sender, RoutedEventArgs e)
{
Button btn = sender as Button;
CounterMessage msgOne = new CounterMessage(btn.Text);
msgOne.ShowDialog();
}
The point being that you don't send something to the ShowDialog method, but rather to the class that is the dialog itself. I also assume that the dialog does more than just displaying the message - otherwise, you would just use MessageBox.Show(....)
Button btn = (Button)sender;
Debug.WriteLine(btn.Name);

Windows Forms C#

I am learning windows forms and can create a one form with textboxes and stuff, but I was wondering how can I change the form upon let's say clicking a button?, so for instance my initial form has a textbox and a button, if the button is clicked I want to show a form with a dropdown and a button. SO the question should be:
1) How do I change the form upon clicking a button but without creating a new instance of the form.
2) If I wanted, how can I add a form when the button is clicked showing the same drop down and button as a pop up form?
In reality I would like to know both cases, changing the form via using the same form and via popping a new form on top.
Should the questions not be clear, I am willing to further explain
Thank you
I'm assuming you already know how to add controls in the form designer and how to implement event handlers.
Question 1
private void button1_Click(object sender, EventArgs e)
{
if (comboBox1.Visible)
{
comboBox1.Visible = false;
textBox1.Visible = true;
}
else
{
comboBox1.Visible = true;
textBox1.Visible = false;
}
}
The button click handler simply toggles the visibility of the two controls.
Question 2
private void button2_Click(object sender, EventArgs e)
{
Form1 form = new Form1();
form.ShowDialog();
}
This time the button handler instantiates a new form an then shows it as a modal dialog. Call Show() if you don't want to show modally.

Categories

Resources