I am creating a wpf form which is going to be used for adding/editing data from datagrid. However when I check for ShowDialog() == true I am getting the above exception.
The code is taken from a book (Windows Presentation Foundation 4.5 Cookbook).
UserWindow usrw = new UserWindow();
usrw.ShowDialog();
if (usrw.ShowDialog() == true)
{
//do some stuff here;
}
And on the WPF window:
private void btn_Save_Click(object sender, RoutedEventArgs e)
{
DialogResult = true;
Close();
}
How I can handle this?
===============================
The solution to the problem was simply to remove usrw.ShowDialog(); and it start working as expected
UserWindow usrw = new UserWindow();
//usrw.ShowDialog();
if (usrw.ShowDialog() == true)
{
//do some stuff here;
}
You are trying to open your window 2 times with every call to ShowDialog()
try
UserWindow usrw = new UserWindow();
bool result =(bool)usrw.ShowDialog();
if (result)
{
//do some stuff here;
}
or
UserWindow usrw = new UserWindow();
usrw.ShowDialog();
if ((bool)usrw.DialogResult)
{
//do some stuff here;
}
keep in mind that DialogResult is Nullable. If there is a chance that you are closing the window without setting the DialogResult, check for null.
Related
I have the following code in C#:
while (wechsel)
{
ProfDpDrv.MDPReadSlaveData(SlaveAddress, resetdiag, out dpData);
newStatus = dpData.m_InputData[i];
if (newStatus == 1)
{
int debug = e.RowIndex;
Dgv_Data_List.Rows[e.RowIndex].Cells["Adresse"].Style.BackColor = Color.Green; //
Dgv_Data_List.Refresh();
wechsel = false;
}
}
Here I want to EXIT the loop with the ESC if wechsel still at true. I have no console by the way.
Due to the fact, that we don't know where this code is running (WinForms, WPF, ASP, etc) except it is not a console it is hard to give you a concrete help.
A general advice to solve this issue, would be to create a CancellationTokenSource and give the source.Token to the long running method. This method can within the loop regulary check, if a cancellation was requested by checking token.IsCancellationRequested and then simply do whatever needed to leave a consistent state and exit the method.
And back to the beginning of my answer, it depends on the used interface you have to hook onto something to register a keypress and then calling source.Cancel().
I assumed that you are using WPF or WinForms.
Just subscribe to KetDown method on host window to capture all keys being pressed and then check if it's ESC and handle that appropriately, see code below:
public partial class MainWindow : Window
{
private bool wechsel = true;
public MainWindow()
{
InitializeComponent();
this.KeyDown += MainWindow_KeyDown;
}
private void MainWindow_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key != Key.Escape) return;
wechsel = false;
txtInfo.Text = "interrupted by ESC";
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
int i = 0;
while (wechsel)
{
txtInfo.Text = i++.ToString();
await Task.Delay(500);
}
}
}
I'm really confused so I'm hoping someone can help me out here. I'm working on a programming assignment for uni but there's one part that's really been bugging me and I can't move on until it is fixed. I have created two classes. The problems in each are shown here:
class Login : Form1
{
Form1 f = new Form1();
public void LoginCorrect()
{
Form1.attempts = 3;
MessageBox.Show("Correct Credentials Entered!");
f.loginScreenVar = false;
f.mainScreenVar = true;
f.ChangeScreen();
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void ChangeScreen()
{
//Login Screen
txtUsername.Visible = loginScreenVar;
txtPassword.Visible = loginScreenVar;
btnLogin.Visible = loginScreenVar;
lblLoginCaption.Visible = loginScreenVar;
lblUsername.Visible = loginScreenVar;
lblPassword.Visible = loginScreenVar;
//Main Screen
lblWelcomeUser.Visible = mainScreenVar;
btnViewDetails.Visible = mainScreenVar;
btnViewAccounts.Visible = mainScreenVar;
btnLogout.Visible = mainScreenVar;
MessageBox.Show(loginScreenVar.ToString());
}
}
I have some controls on screen in my design which consist of text boxes, labels, and buttons, and these are meant to show and hide at diffferent times. I have created some booleans which can be set to true and false which will also set the visibility of these controls to true and false.
My problem is when accessing ChangeScreen() from my Login class, for some reason the controls don't hide when they're meant to. I've literally got a message box in the ChangeScreen() method which outputs the result of 'loginScreenVar' and this is false. Please can someone tell me why my 'Login Screen' controls are NOT hiding even though 'loginScreenVar' = false.
Another thing to note is when calling this code from a button in the Form1 class, it does work. However, due to the brief of my assignment I need to use multiple classes.
I really hope this isn't a bug and someone can help me here because I literally can't move on until this is fixed, thanks!
The issue is that, as noted in the comments, you create a new instance of Form1.
A whole new object, with own states.
Why can't I see this new instance? - well, if you did f.show() THEN you'd see it.
As it stands, you're still looking at the old instance.
So, what you need is a publicly accessible instance of Form1 which your two classes access, without creating a new instance.
OR
you could also work with two different windows. For example:
Form1_loaded(object sender, EventArgs e)
{
LoginWindow lw = new LoginWindow();
var result = lw.ShowDialog();
if(result == DialogResult.Cancel)
{
Application.Quit();
}
}
Let's assume you have a button for login. When clicked, it checks whether password and user name are correct. If not, the incorrect count gets increased by one. If the incorrect count is >= 3, then you just close the LoginWindow. (Default DialogResult is DialogResult.Cancel). The code might look like this:
LoginBtn_Click(object sender, EventArgs e)
{
if(UserNameInput.Text == userName && PasswordInput.Text == password)
{
failedAttempts = 0;
this.DialogResult = DialogResult.OK;
this.Close();
}
else
{
failedAttempts++;
if(failedAttempts >= 3)
{
MessageBox.Show("Wrong password. Shutting down the application...");
this.Close();
}
else
{
MessageBox.Show("Wrong password. " + (3-failedAttempts) + " tries left.");
}
}
}
This way, if login isn't successful, app quits. Otherwise the main screen appears.
Note: This is a basic solution. In a more complex app, you'd want more sophisticated output (not hard-coded strings) and comparisions using VariableName.Equals();
Let's keep it simple (and in the style you've started) for now:
public partial class Form1 : Form //Change the default "form1" "Button1" etc names as soon as possible
{
private bool loginScreenVar = true; //when naming booleans, use "truth test" sounding names like isLoginScreenMode
private bool mainScreenVar = true;
public Form1() //this is a constructor, a method that is always called when a new instance of this object is created
{
InitializeComponent();
//use the constructor to set things up
loginScreenVar = true;
mainScreenVar = false;
ChangeScreen();//make sure loginscreen is showing
}
public void ChangeScreen()
{
//Login Screen
txtUsername.Visible = loginScreenVar;
txtPassword.Visible = loginScreenVar;
btnLogin.Visible = loginScreenVar;
lblLoginCaption.Visible = loginScreenVar;
lblUsername.Visible = loginScreenVar;
lblPassword.Visible = loginScreenVar;
//Main Screen
lblWelcomeUser.Visible = mainScreenVar;
btnViewDetails.Visible = mainScreenVar;
btnViewAccounts.Visible = mainScreenVar;
btnLogout.Visible = mainScreenVar;
MessageBox.Show(loginScreenVar.ToString());
}
//call this method when the login is correct
public void LoginCorrect()
{
loginScreenVar = false;
mainScreenVar = true;
ChangeScreen();
}
//double click your login button in the forms designer to add this click event handler
public void LoginButton_Clicked(object sender, ClickEventArgs e){
if(txtUsername.Text == "user" && txtPassword.Text == "pass"){
LoginCorrect();
} else {
MessageBox.Show("Login incorrect");
}
}
}
Forget the class Login:Form stuff unless you're really trying to explore object instantiation and making your own classes for things. Your Form1 will be on show when your app starts, do all the logic inside it
A better way to change screens in winforms is by creating two separate panels each one contains the desired controls to be shown and hide so that you can switch between them
Code example:
Form1_loaded(object sender, EventArgs e)
{
LogInPanel.Visible=true;
}
private void ConnectBtn_Click(object sender, EventArgs e)
{
// Do your checking here
// IF conditions met
MainPanel.Visible=true;
}
private void DisconnectBtn_Click(object sender, EventArgs e)
{
// Do your checking here
// IF conditions met
LogInPanel.Visible=true;
}
If you want to keep you methadologie make sure your program.cs runs Login class instead of Form1 class
Can anyone help me understand why my call to dialogservice executes after the CanNavigateAway function has returned its value? (My goal is to warn the user they are about to navigate away from a view without saving their changes. If they click OK, the navigation is allowed. I'm using MVVM Light.
When I step through the code, it does reach the dialog service, but then proceeds to the end of CanNavigateAway before creating the dialog. The CanNavigateAway method is called by OnNavigatingFrom.
public bool CanNavigateAway()
{
if (!changesSaved && Model.IsModified && !continueNavigation)
{
dialogService.ShowMessage("Are you sure you want to continue?",
"Confirmation",
buttonConfirmText: "Continue", buttonCancelText: "Discard",
afterHideCallback: (confirmed) =>
{
if (confirmed)
{
// User has pressed the "confirm" button.
// ...
continueNavigation = true;
}
else
{
// User has pressed the "cancel" button
// (or has discared the dialog box).
// ...
continueNavigation = false;
}
});
return continueNavigation;
}
}
Here is the OnNavigatingFrom method from the MVVM Light Bindable Page class:
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
var navigableViewModel = this.DataContext as INavigable;
if (navigableViewModel != null)
{
if (!navigableViewModel.CanNavigateAway())
{
e.Cancel = true;
}
}
}
I tried this a different way to get the dialog service out of the mix, but showConfirmationDialogAsync still does not seem to execute in time:
public bool CanNavigateAway()
{
continueNavigation = false;
if (!changesSaved && Model.IsModified && !continueNavigation)
{
showConfirmationDialogAsync();
return continueNavigation;
}
private async void showConfirmationDialogAsync()
{
continueNavigation = false;
ContentDialog noSaveConfirmation = new ContentDialog
{
Title = "Warning",
Content = "You have unsaved changes. Are you sure you want to leave this page without saving?",
PrimaryButtonText = "Leave without saving",
SecondaryButtonText = "Stay and finish"
};
ContentDialogResult result = await noSaveConfirmation.ShowAsync();
if (result == ContentDialogResult.Primary)
{
continueNavigation = true;
}
else if (result == ContentDialogResult.Secondary)
{
continueNavigation = false;
}
}
None of the solutions will work if you require a response from the user. The problem is that when the code is inside the navigation event handler, it is running on the UI thread and the user prompt runs asynchronously, so that the UI is free to present the dialog to the user. This however means that the event handler finishes before the user has a chance to respond.
However, you can use a workaround solution. Add a flag bool field like forceNavigation. Then inside the OnNavigatingFrom display the dialog to the user and set Cancel to true right away and display the user the confirmation dialog. If the user says yes, then set forceNavigaiton to true and trigger the navigation manually again. Now it will skip the confirmation part and navigate right away.
protected async override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
//if navigation is forced, skip all logic
if ( !forceNavigation )
{
var navigableViewModel = this.DataContext as INavigable;
if (navigableViewModel != null)
{
e.Cancel = true;
//display the dialog to the user, if he says yes, set
//forceNavigation = true; and repeat the navigation (e.g. GoBack, ... )
}
}
}
Not to execute any further code until the async method is completed its execution. Please let me know how to achieve it.
Following is sample code :
// Parent Form code
private void btnOpenForm1_Click(object sender, EventArgs e)
{
Form1 form1 = new Form1();
var result = form1.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
{
// do something
}
}
// Child form code
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult result = MessageBox.Show(string.Format("Do you want to save changes?", "Confirmation", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (result == System.Windows.Forms.DialogResult.Cancel)
{
e.Cancel = true;
}
else
{
if (result == DialogResult.Yes)
{
e.Cancel = true;
this.DialogResult = System.Windows.Forms.DialogResult.OK;
// HERE I NEED TO WAIT COMPULSARILY TILL THE OPERATION IS COMPLETED, NO NEXT STATEMENT SHOULD BE EXECUTED (NEITHER IN PARENT FORM)
var isSaveCompleted = await HandleSave().ConfigureAwait(false);
if(isSaveCompleted == true)
{
// dispose some objects
}
}
else // if No is clicked
{
this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
// dispose some objects
}
}
}
public async Task<bool> HandleSave()
{
await doWork();
//
// some code here
//
}
public doWork()
{
//
// some code for I/O operation
//
}
In the above code, I don't want to execute the any of the next statements (not even in the Parent Form) until the HandleSave() method is completed its execution.
There's an XY problem here. Trying to force an asynchronous method to run synchronously in order to block closing the form is the wrong solution.
Instead of fighting window lifetimes, you need to figure out how to work with them. One solution for this scenario is to hide the form rather than closing it; and then actually close it when the asynchronous save completes. You can enable your parent form to detect when the child form is actually closed by using a TaskCompletionSource<DialogResult>.
I would like to know if there is a better way than the following to check if the window got closed, or if a Closing cancled the closing procedure?
Here we go with my way:
var window = Application.Current.Windows.FirstOrDefault(x => x is FooWindow);
if (window != null)
{
var gotClosed = false;
window.Closed += (sender, args) => gotClosed = true;
window.Close();
if (gotClosed == false)
{
//Close got cancled, by closing...
}
}
From checking the .NET source, I'm not too sure that IsDisposed is safe. There don't seem to be a lot of safe options though. The one I have been using so far without issues is checking the Visibility property for Visible after closing.
A cleaner approach might be creating your own class and overriding OnClosing() or OnClosed() though:
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
// Fires Closing event
base.OnClosing(e);
if (!e.Cancel)
{
// Window was allowed to close.
// Set IsClosed = true or something like that
}
}
There you can store the result in a property for example.
I'm not sure it's better than your solution, but after calling window.Close() the property IsDisposed gets true. So, you can check it:
if(window.IsDisposed)
{
....
}