I have a problem with the option to select the button once when it is open, what am I doing wrong?
private Boolean buttonWasClicked = false;
private void Button_Click(object sender, RoutedEventArgs e)
{
buttonWasClicked = true;
if ( buttonWasClicked == true)
{
new SettingsWindow().Show();
var test = new SettingsWindow();
test.Owner = System.Windows.Application.Current.MainWindow;
test.WindowStartupLocation = WindowStartupLocation.CenterOwner;
test.Top = this.Top + 20;
}
else {
buttonWasClicked = false;
}
}`
I would avoid to keep your own flag variable and manage its setting by hand. For example, if the user closes the window how do you change back the variable to allow again opening a SettingsWindow?. There is a more robust system based approach at this problem. Looking at system provided informations will help you to avoid opening a second time the settings window until there is already one instance opened.
To check if there is already an instance of a SettingsWindow open you could use informations provided by the system with code like this
private void Button_Click(object sender, RoutedEventArgs e)
{
// Check if, in the Application.Current.Windows collection
// there is at least one window of type SettingsWindow
SettingsWindow w = Application.Current.Windows
.OfType<SettingsWindow>()
.FirstOrDefault();
if(w == null)
{
// No window of the type required, open a new one....
w = new SettingsWindow();
w.Owner = System.Windows.Application.Current.MainWindow;
w.WindowStartupLocation = WindowStartupLocation.CenterOwner;
w.Top = this.Top + 20;
}
// Show it NON MODALLY....
w.Show();
}
The call to Show returns immediately (Non modally) and thus your program continue as usual with the MainWindow still active.
Instead, if you want to use a modal approach, (meaning that until the SettingsWindow is open nothing in your MainWindow is active) you could simply create the SettingsWindow, set its Owner and eventually its Position and finally call ShowDialog (Do not forget to set the Owner property). In this way your code is blocked in the ShowDialog and doesn't return until the user closes the SettingsWindow instance just opened. (And you could remove all the checking above)
Pretty sure this will always be true
Go with the excellent answer from Steve
buttonWasClicked = true;
if (buttonWasClicked == true)
{
// this will execute every time
}
else
{
// this will never execute
}
Related
I have a problem with topmost level, found a solution that "works" but it don't look so nice. Is there another "cleaner" way to solve this?
Here's my code: comments of event in code.
OrderTemplateView template;
private void toolStripButton4_Click(object sender, EventArgs e)
{
if (template != null)
{
template.Close(); //must close to trigger close event.
template.Dispose();
}
mainUi.TopMost = true; // must set my mainUi topMost here othervise it drops in the background of other windows open at the computer.
template = new OrderTemplateView(this);
template.TopMost = true;// must set my dialog topmost othervise it drops behind my mainUi
template.StartPosition = FormStartPosition.CenterParent;
mainUi.TopMost = false;//must release my topmost so other windows on the computer can be called to front.
template.TopMost = false;
template.ShowDialog();
}
Updated code that does the same job:
private void toolStripButton4_Click(object sender, EventArgs e)
{
if (template != null)
{
template.Close();
template.Dispose();
}
template = new OrderTemplateView(mainUi);
template.StartPosition = FormStartPosition.CenterParent;
template.ShowDialog(mainUi);
}
`
Rather than setting TopMost, try the following:
Remove all your references to TopMost
Call mainUi.BringToFront()
Call template.ShowDialog(mainUi), noting that the parent control is passed to the dialog.
For two days, I have tried various methods for making it so that on loadButton click, it opens a secondary window and disables the loadButton; Then, when that secondary window has been closed, the loadButton will be re-enabled. Although, obviously all of my attempts have been unsucessful.
I have been reading about using the isClosing event, although, I haven't figured out how to properly implement it. So I decided to go with this route.
private void loadButton_Click(object sender, RoutedEventArgs e)
{
var richWindow = RichTextWindow.GetWindow(new RichTextWindow());
if (richWindow.IsActive != true)
{
loadButton.IsEnabled = false;
richWindow.Show();
}
else
{
loadButton.IsEnabled = true;
}
}
Issue here is, the first half is executed. Once I click the loadButton, it does disable. However, on closing the new Window, the loadButton is still disabled.
Could anyone point me in the right direction on where I need to go with this?
I think what you want is something like this:
private void loadButton_Click(object sender, RoutedEventArgs e)
{
var richWindow = RichTextWindow.GetWindow(new RichTextWindow());
richWindow.Closed += (s, e) => loadButton.IsEnabled = true;
loadButton.IsEnabled = false;
richWindow.Show();
}
Basically, disable the button before opening the window. Then, listen for the window to close and enable the button again.
richWindow.Loaded +=
{
loadedButton.IsEnabled = false;
};
richWindow.Closing +=
{
loadedButton.IsEnabled = true;
}
I'm having a bit weird problem with WinForm which seems to refuse to close for some weird reason. I've got very simple gui which sometimes doesn't react for me pressing X or when i use events on buttons it even reaches Close() and does nothing..
private void buttonZapisz_Click(object sender, EventArgs e) {
string plik = textBoxDokumentDoZaladowania.Text;
if (File.Exists(plik)) {
string extension = Path.GetExtension(plik);
string nazwaPliku = Path.GetFileName(plik);
SqlMethods.databaseFilePut(plik, comboBoxTypDokumentu.Text, textBoxKomentarz.Text, sKlienciID, sPortfelID, extension, nazwaPliku);
Close();
}
}
There are no events assigned to FormClosed or FormClosing. So how can I find out what's wrong. Sometimes X will work after the GUI is loaded but after i press Button to save some stuff to database it reaches Close() in that button event and it still is visible and does nothing. Can't use X, nor ALT+F4. I can go around GUI and choose other values for ComboBox without problem.
I call GUI like this:
private void contextMenuDokumentyDodaj_Click(object sender, EventArgs e) {
var lv = (ListView) contextMenuDokumenty.SourceControl;
string varPortfelID = Locale.ustalDaneListViewKolumny(listViewNumeryUmow, 0);
string varKlienciID = Locale.ustalDaneListViewKolumny(listViewKlienci, 0);
if (lv == listViewDokumentyPerKlient) {
if (varKlienciID != "") {
var dokumenty = new DocumentsGui(varKlienciID);
dokumenty.Show();
dokumenty.FormClosed += varDocumentsGuiKlienci_FormClosed;
}
} else if (lv == listViewDokumentyPerPortfel) {
if (varPortfelID != "" && varKlienciID != "") {
var dokumenty = new DocumentsGui(varKlienciID, varPortfelID);
dokumenty.Show();
dokumenty.FormClosed += varDocumentsGuiPortfele_FormClosed;
}
}
}
While I can't close GUI i can work on the main gui without problem too. I can open up same GUI and after opening new GUI i can quickly close it. GUI is very simple with few ComboBoxes,TextBoxes and one EditButton from Devexpress.
Edit: varDocumentsGuiPortfele_FormClosed code allows me to refresh GUI (reload ListView's depending on where the user is on now).
private void varDocumentsGuiPortfele_FormClosed(object sender, FormClosedEventArgs e) {
TabControl varTabControl = tabControlKlientPortfele;
if (varTabControl.TabPages.IndexOf(tabPageDokumentyPerKlient) == varTabControl.SelectedIndex) {
loadTabControlKlientPortfeleBezZmianyUmowy();
}
}
Paste this code into your form classes:
protected override void OnFormClosing(FormClosingEventArgs e) {
e.Cancel = false;
base.OnFormClosing(e);
}
When that works, you want to find out why you have Validating event handlers that don't want the form to be closed.
Next thing you want to verify is Debug + Exceptions, tick the Thrown box for CLR Exceptions. This makes sure you don't swallow an exception that prevents a form from closing. Or worse, the operating system swallowing the exception, a nasty Windows 7 problem.
If you are getting an Exception in your close method, then the Base closing method is never called.
Put a try{}catch{} around everything
I've created a public bool LogedIn; in my login.cs:
if(login successful condition)
LogedIn = true;
else
LogedIn = false
But when I access this var from another form with Login Log = new Login();
by using if(Log.LogedIn) the LogedIn variable is always false, even after successful login by the user.
Why this is not working/updating its value outside its parent form?
Updating the code:
Login.cs
public bool isLogedIn;
private void button1_Click(object sender, EventArgs e)
{
if (i>-1 && (textBox2.Text == DS.Tables[0].Rows[--i][0].ToString()))
{
this.DialogResult = DialogResult.OK;
isLogedIn = true;
}
else
{
MessageBox.Show("Invalid password supplied for username \"" + comboBox1.Text + "\"", "Login Error.....", MessageBoxButtons.OK);
isLogedIn = false;
return;
}
}
Checking for the updated value in Home.cs
private void Home_Load(object sender, EventArgs e)
{
if (Log.isLogedIn) // Always False at this position.
{
label18.ForeColor = System.Drawing.Color.Green;
submitButton.Enabled = true;
}
else
{
label18.ForeColor = System.Drawing.Color.Red;
submitButton.Enabled = false;
}
}
I've checked again... I'm not having double instance of this variable in Login.cs form.
Here's how I'm calling Login.cs form via Home.cs (main form). Hope this helps...
private void loginToolStripMenuItem_Click(object sender, EventArgs e)
{
Log.FormClosed += new FormClosedEventHandler(Log_FormClosed);
Log.ShowDialog(this);
Log.BringToFront();
}
void Log_FormClosed(object sender, FormClosedEventArgs e)
{
if (Log.isLogedIn)
{
// Something here
}
else
{
// Something here
if (Log.DialogResult == DialogResult.Cancel)
Log.Hide();
}
}
I assume you have a form called Login in your application. Ignore the rest if assumption is wrong.
You are not referring to the correct instance of the Login form. In windows application, there is a collection called Application.OpenForms. This contains all the open form instances in your application. To access the correct Login form, try this:
Application.OpenForms.OfType<Form>().Where(x => x is Login).FirstOrDefault()
Make sure you have Login form always open to perform this task. You can make use of Hide instead of Close or CloseDialog for the Login form.
If you are closing the Login form, you can create static class which is accessible from each of the forms keep the properties there.
It seems that you have more than one instance of the Login class, each one with its isLogedIn var.
It is not clear where you are instantiating Login with your Login Log = new Login(); line. Have you tried to put a breakpoint there and see how many times it gets hit?
Another thing you could do is put a breakpoint on the line where isLogedIn is set, and another one where you read it. When the setting breakpoint is hit add a watch to the instance of Login (in this case add a watch to this) and choose Make Object ID from the right click menu. Your instance will be marjked by a "#1" Then do the same for the variable Log when the reading breakpoint is hit. If the mark is different (i.e. "#2") you can be sure that you are reading something different from the variable you set before.
RESOLVED: Turned out to be a visual studio problem. Closed visual studio, cleaned and rebuilt, and the value started showing. Thanks all for the help, sounds like I need to switch to VS2010.
This may not be the best, safest, or preferred way to pass values between forms, but this is the way I am attempting for the moment. So, please do help me to get this way working. After you provide an answer, you're more than welcome to add in some better ways of doing this.
The problem is, when the modal dialog box closes and I go back to the owner, the textbox value from the modal is an empty string rather than the actual value. I've read in several places this should not be the case, as the data should persist even after the modal box disposes. Here's my code.
public partial class PreferencesForm : Form
{
public PreferencesForm()
{
InitializeComponent();
}
private void okButton_Click(object sender, EventArgs e)
{
if (masterRadioButton.Checked == true)
{
if (password1TextBox.Text != password2TextBox.Text)
{
errorLabel.Text = "Passwords do not match, please re-enter both passwords and try again.";
this.Refresh();
}
else if (password1TextBox.Text == "" && password2TextBox.Text == "")
{
errorLabel.Text = "You must enter a password.";
}
else
{
okResultButton_Click(null, null);
}
}
else if (singleRadioButton.Checked == true)
{
okResultButton_Click(null, null);
}
}
private void cancelButton_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Dispose();
}
private void okResultButton_Click(object sender, EventArgs e)
{
// invisible button
this.DialogResult = DialogResult.OK;
this.Dispose();
}
And here is the code that calls the above form as a modal dialog box.
private void setPreferencesToolStripMenuItem_Click(object sender, EventArgs e)
{
PreferencesForm pf = new PreferencesForm();
DialogResult result = pf.ShowDialog();
if (result == DialogResult.OK)
{
if (pf.password1TextBox.Text != "")
{
masterPassword = pf.password1TextBox.Text;
}
else
{
masterPassword = null;
}
}
}
Thanks for any assistance. I'm getting pretty frustrated over here. >:(
Note: The ReadOnly property of the password1TextBox variable is correctly shown as true or false, depending on what I select in the modal form, but the text property will still not correctly display.
I'm guessing that Dispose will also dispose the controls it contains. After the controls have been disposed, the text is likely no longer valid either. Try Close rather than Dispose and then Dispose in the caller.
You should listen to the people answering your question. Dispose is supposed to clear out memory allocated, it doesn't matter if you can still get the ReadOnly property.
Don't call Dispose in the form, call dispose from the calling code, as in the example code from the ShowDialog method documentation (http://msdn.microsoft.com/en-us/library/c7ykbedk.aspx#Y851). Note that Dispose is called just before the testDialog variable goes out of scope.
public void ShowMyDialogBox()
{
Form2 testDialog = new Form2();
// Show testDialog as a modal dialog and determine if DialogResult = OK.
if (testDialog.ShowDialog(this) == DialogResult.OK)
{
// Read the contents of testDialog's TextBox.
this.txtResult.Text = testDialog.TextBox1.Text;
}
else
{
this.txtResult.Text = "Cancelled";
}
testDialog.Dispose();
}
I propose just save the string of the control of your Dialog into string property, and retrieve value of that class property and not control's property value after Dialog is closed, and stop worrying about Dispose or not Dispose, or whatever else.
Hope this helps