I have a user control (AddNewTransaction) inside a Popup (NewTransaction in DebtPage.xaml) that I open with this code:
[DebtPage.xaml.cs]
public void Button_Click_1(object sender, RoutedEventArgs e)
{
if (!NewTransaction.IsOpen) { NewTransaction.IsOpen = true; }
NewTransaction.HorizontalOffset = (Window.Current.Bounds.Width) / 2 - (New_Transaction_Grid.Width / 2);
NewTransaction.VerticalOffset = (Window.Current.Bounds.Height) / 2 - (New_Transaction_Grid.Height / 2);
NewTransaction.Visibility = Visibility.Visible;
RetanguloBranco_Background.Visibility = Visibility.Visible;
}
The Popup opens and there's a custom control (AddNewTransaction) with a "Save" button. The button suppose to save data AND close the Popup. But it just save, the Popup keeps opened.
[AddNewTransaction.xaml.cs]
private static DebtPage _debtPage;
public static DebtPage debtPage
{
get
{
if (_debtPage == null)
{
_debtPage = new DebtPage();
}
return _debtPage;
}
}
private void BotaoSave_Click(object sender, RoutedEventArgs e)
{
SalvarTransaction(); //It's working!
debtPage.ClosePopup_NewTransaction(); //It is not!
}
[DebtPage.xaml.cs]
public void ClosePopup_NewTransaction()
{
NewTransaction.IsOpen = false;
NewTransaction.Visibility = Visibility.Collapsed;
RetanguloBranco_Background.Visibility = Visibility.Collapsed;
}
Thanks!
I've solved the problem with this:
private void BotaoSave_Click(object sender, RoutedEventArgs e)
{
SalvarTransaction();
Grid gridParent = this.Parent as Grid;
Popup parent = gridParent.Parent as Popup;
if (parent != null)
{
parent.IsOpen = false;
}
}
But I still want to know why ClosePopup_NewTransaction(); does nothing.
Related
I am a beginner at windows forms, and I am having some issues with opening child forms.
My application has 2 buttons, one that goes to the main menu, and another that goes to the options.
The problem is that, if I click on a checkbox in the options menu and leave the options tab and come back, the checkbox will not be checked anymore.
This is my code:
private Form CurrentChildForm;
private void OpenChildForm(Form childForm)
{
if(CurrentChildForm != null)
{
CurrentChildForm.Visible = false;
}
CurrentChildForm = childForm;
childForm.TopLevel = false;
childForm.FormBorderStyle = FormBorderStyle.None;
childForm.Dock = DockStyle.Fill;
PanelForForm.Controls.Add(childForm);
PanelForForm.Tag = childForm;
childForm.BringToFront();
childForm.Show();
}
private void MainMenu_Click(object sender, EventArgs e)
{
OpenChildForm(new MenuForm());
}
private void OptionsMenu_Click(object sender, EventArgs e)
{
OpenChildForm(new OptionsForm());
}
through your Click-Events on the different buttons you are always creating a new instance of your Forms.
A possible solution is to cache the instance of your optionsMenu for example through a private field, because I consider it being SingleInstance.
private Form CurrentChildForm;
private OptionsForm _opForm;
private void OptionsMenu_Click(object sender, EventArgs e)
{
if (_opForm == null)
{
_opForm = new OptionsForm();
}
OpenChildForm(_opForm);
}
Within my application i am trying to use forms in tabpages. These forms are all data managing forms.
I edited the tabcontrol (created a custom tabcontrol) so i can remove tabpages by double clicking on the tabheader.
protected override void OnMouseDoubleClick(MouseEventArgs e)
{
base.OnMouseDoubleClick(e);
//* Default method of closing a tab.
if (Selectedtab != null)
TabPages.Remove(Selectedtab);
}
Although it works. Its actually not doing what I need.
Because any changes on the form in the tabpage, are lost, regardless of the below
public partial class SomeForm : Form
{
private void SomeForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (HasChanges() && CustomMessage.WarningBox("There is unsaved data. Are you sure you want to close"))
return;
((TabControl)((TabPage)this.Parent).Parent).TabPages.Remove((TabPage)this.Parent);
}
}
When setting a breakpoint at this function, it never enters.
Now my question: Is it possible to call the Close method of the form from Tabcontrol. Preferably something like below.
protected override void OnMouseDoubleClick(MouseEventArgs e)
{
base.OnMouseDoubleClick(e);
if (Selectedtab != null)
{
if (Selectedtab.EmbeddedForm != null)
TabPages.ASelectedtab.EmbeddedForm.Close();
}
}
The main problem I am facing is I don't know how to access a function on a form from only knowing the selected tab. And I cant find it either.
Solution after using KyleWangs answer as base:
Custom Control:
protected override void OnMouseDoubleClick(MouseEventArgs e)
{
base.OnMouseDoubleClick(e);
string frmSearchName = "Frm" + SelectedTab.Name.Substring(3);
Form f = (Form)Application.OpenForms[frmSearchName];
if (f != null)
f.Close();
else
TabPages.Remove(SelectedTab);
}
Form:
private void SomeForm_FormClosing(object sender, FormClosingEventArgs e)
{
SetChanges();
if (HasChanges() && !CustomMessage.WarningBox("There is unsaved data. Are you sure you want to close?"))
e.Cancel = true;
else
((TabControl)((TabPage)this.Parent).Parent).TabPages.Remove((TabPage)this.Parent);
}
You can use property Application.OpenForms to get the open form instance.
private void Form1_Load(object sender, EventArgs e)
{
tabControl1.TabPages.Clear();
PageForm1 f1 = new PageForm1();
AddNewTab(f1);
}
private void AddNewTab(Form frm)
{
TabPage tab = new TabPage(frm.Text);
frm.TopLevel = false;
frm.Parent = tab;
frm.Visible = true;
tabControl1.TabPages.Add(tab);
frm.Location = new Point((tab.Width - frm.Width) / 2, (tab.Height - frm.Height) / 2);
tabControl1.SelectedTab = tab;
}
private void tabControl1_DoubleClick(object sender, EventArgs e)
{
Form f = (Form)Application.OpenForms[tabControl1.SelectedTab.Text];
f.Close();
tabControl1.TabPages.RemoveAt(tabControl1.SelectedIndex);
}
I have a button that opens a Window.
If the button is pressed again, it opens a duplicate of the same window.
info = new Info();
info.Owner = Window.GetWindow(this);
info.Show();
How do you check if the Window is already open, and deny a duplicate from being opened again?
I can't use info.ShowDialog() because it disables the Main Window.
Solutions that have not worked:
Info info = new Info();
if (!info.IsActive)
{
info = new Info();
info.Owner = Window.GetWindow(this);
info.Show();
}
Info info = new Info();
if (info.Visibility != Visibility.Visible)
{
info.Owner = Window.GetWindow(this);
info.Show();
}
public static bool IsWindowOpen<T>(string name = "") where T : Window
{
return string.IsNullOrEmpty(name)
? Application.Current.Windows.OfType<T>().Any()
: Application.Current.Windows.OfType<T>().Any(w => w.Name.Equals(name));
}
private void buttonInfo_Click(object sender, RoutedEventArgs e)
{
if (!IsWindowOpen<Window>("Info"))
{
Info info = new Info();
info.Owner = Window.GetWindow(this);
info.Show();
}
}
Create a form only when value is not null.
If the form was closed put the value back to null with the FormClosed event.
public static Info info;
if(info == null){
info = new Info();
info.Show();
}
put an event form close on the info form
private void info_FormClosed(object sender, FormClosedEventArgs e)
{
MainForm1.info = null;
}
It works for me
The sensible approach is to just keep track of the Window instance so you don't have to find it back later. Add a field:
private Info infoWindow;
If it is null then you know that the window doesn't exist yet, so you'll want to create it. Use the Closed event to set the variable back to null. If it is not null then you want to make sure that the window gets restored. So:
private void button_Click(object sender, RoutedEventArgs e) {
if (infoWindow == null) {
infoWindow = new Info();
infoWindow.Closed += (s, ea) => infoWindow = null;
infoWindow.Owner = this; // optional
infoWindow.Show();
}
else {
if (infoWindow.WindowState == WindowState.Minimized) {
infoWindow.WindowState = WindowState.Normal;
}
infoWindow.Activate();
}
}
And you probably also want to close the window automatically when the window that contains the button is closed:
private void Window_Closed(object sender, EventArgs e) {
if (infoWindow != null) infoWindow.Close();
}
You could use .IsLoaded field or bind the .ContentRendered event
Edit 1 -
Window1:
public class Window1 : Window
{
private Info info = null;
private Boolean IsInfoOpened = false;
protected void OpenInfo()
{
if (this.IsInfoOpened) return;
this.info = new Info();
this.info.ContentRendered += delegate { this.IsInfoOpened = true; };
this.info.Closed += delegate { this.IsInfoOpened = false; }
this.info.Show();
}
}
how to enable menu strip in child form?
i just want to enable the menu button strip when i will close the child form
how to code that in child form?
Student....
private void tsmNewEmp_Click(object sender, EventArgs e)
{
if(NewEmp == null)
{
NewEmp = new NewEmployee();
NewEmp.MdiParent = this;
}
NewEmp.Show();
tsmNewEmp.Enabled = false;
tsmNewContract.Enabled = false;
}
You can use the FormClosed event to update the buttons in the parent form after the child is closed:
private void tsmNewEmp_Click(object sender, EventArgs e)
{
if(NewEmp == null)
{
NewEmp = new NewEmployee();
NewEmp.MdiParent = this;
NewEmp.FormClosed += FormClosed_1;
}
NewEmp.Show();
tsmNewEmp.Enabled = false;
tsmNewContract.Enabled = false;
}
private void FormClosed_1(object sender, FormClosedEventArgs e)
{
tsmNewEmp.Enabled = true;
tsmNewContract.Enabled = true;
}
Control[] controls = this.MdiParent.Controls.Find("Menu", true);
foreach (Control ctrl in controls)
{
if (ctrl.Name == "Menu")
{
MenuStrip strip = ctrl as MenuStrip;
strip.Items["login"].Enabled = false;
strip.Items["logout"].Enabled = false;
}
}
Pls i have a problem with trying to check the background Image of a button. I want to delete the button if its image matches an Image in the Resource Folder. I have Tried
private void DeleteCard(Button btn)
{
if (btn.BackgroundImage.Equals( FunWaysInc.Properties.Resources.Card_1))
{
playersCards.Remove(Properties.Resources.Card_1);
MessageBox.Show(playersCards.Count.ToString());
}
else
{
MessageBox.Show("NO");
}
}
private void frmQuickSpark_Load(object sender, EventArgs e)
{
Button tstbtn = new Button();
tstbtn.BackgroundImage = Properties.Resources.Card_1;
DeleteCard(tstbtn);
}
but the message box displayed is the one that displays "NO"..
Pls what is happening????
when adding the button
button.Tag = "ToDelete";
then later
foreach (Button b in this.Controls.OfType<Button>())
{
if(b.Tag == "ToDelete")
{
//delete
}
}
Here what you have to do. You need to enumerate all your images and store pointer in Tag property of the button.
private Dictionary<string, Image> _table = new Dictionary<string, Image>();
private void frmQuickSpark_Load(object sender, EventArgs e)
{
_table.Add("Image1", Properties.Resources.Card_1);
_table.Add("Image2", Properties.Resources.Card_2);
Button btn = new Button();
SetButtonImage(btn);
DeleteCard(btn);
}
private void SetButtonImage(Button button)
{
button.BackgroundImage = _table["Image1"];
button.BackgroundImage.Tag = "Image1";
}
private void DeleteCard(Button btn)
{
if (btn.BackgroundImage.Tag == "Image1")
{
playersCards.Remove(btn); // not sure what your logic of removal
MessageBox.Show(playersCards.Count.ToString());
}
else
{
MessageBox.Show("NO");
}
}
I already found an answer to my question.. I just modified my code
private void DeleteCard(Image img)
{
playersCards.Add(Properties.Resources.Card_1);
if (img == playersCards[0])
{
playersCards.Remove(Properties.Resources.Card_1);
MessageBox.Show(playersCards.Count.ToString());
}
else
{
MessageBox.Show("NO");
}
}
private void frmQuickSpark_Load(object sender, EventArgs e)
{
Button tstbtn = new Button();
tstbtn.BackgroundImage = Properties.Resources.Card_1;
Image img = tstbtn.BackgroundImage;
DeleteCard(img);
}
It works perfectly.