How to connect a pup up to a button ?
I want when i click a button pup up window to appear.
I have already made the pop up with some elements child-ed in the grid in the pop up.
I've tried this but it doesn't work
private void Task_click(object sender, RoutedEventArgs e)
{
if (PopUp.Visibility == Visibility.Collapsed)
PopUp.Visibility = Visibility.Visible;
}
I think maybe other parameter has to be changed for the pup up to appear.
Thanks for the help :D
From what I understood, you are doing Windows Form programing and the popup you are talking about must be a Custom dialog box (Simpler Version's Tutorial). Assuming you have set the properties of your PopUp suitable for a dialog box, you can use following :
using (PopUp pp = new PopUp())
{
DialogResult result = pp.ShowDialog();
if (result == DialogResult.OK)
{
// do your stuff
}
}
Related
Complete C# beginner here, just wondering how to open an existing form from within the current form, and close the first one. I have literally no code yet so any help is much appreciated
I had a bit of a mess around, of course not knowing anything. Something like this is about all I tried just going off of intellisense prompts:
Application.Run(Terry2);
It obviously didn't work. Here was the error
Error CS0119 'Window2' is a type, which is not valid in the given context.
I have no idea where to start so thanks in advance for the help. I am using Visual Studio 2022.
Actually there are plenty of examples for this code on the internet, first you should create both of your forms and then just create an instance of your form2 in form1 under the event of your form1's button and call it's Show() method.
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ShowDialog(); // Shows Form2 you can also use f2.Show()
}
Here is a step by step explenation of the process that you should follow. I recommend you to watch some fundamental c# programming tutorials as well.
Click Here
If you've used windows for some time, you've noticed that there are two types of Dialog Boxes (forms): Modal and Modeless Dialog Boxes
Modal dialog boxes: while this one is being shown, you cannot use the other dialog boxes of the application; you'll have to finish this one before you can continue using your application. Example: File Open dialog box.
Modeless Dialog Box. A kind of dialog box that gives some extra information next to the other dialog box of your application. While this one is being shown, you can switch back to your original dialog box and enter some input.
How to show a modal dialog box
For this, you use Form.ShowDialog
In your form:
private DialogResult AskFileName()
{
using (Form myForm = new SaveFileDialog();
{
// Before showing the dialog box, set some properties:
myForm.Title = "Save file";
myForm.FileName = this.DefaultFileName;
myForm.ValidateNames = true;
...
// show the file save dialog, and wait until operator closes the dialog box
DialogResult dlgResult = myForm.ShowDialog(this);
// if here, you know the operator closed the dialog box;
return dlgResult;
}
}
private void SaveFile()
{
DialogResult dlgResult = this.AskFileName();
switch (dglResult)
{
case DialogResult.Ok:
// File is saved:
this.HandleFileSaved();
break;
case DialogResult.Cancel();
// operator pressed cancel
this.ReportFileNotSaved();
break;
...
}
}
A form is disposable, hence you should use the using statement. After creation of the form you have time to set the properties. The form is shown using ShowDialog. While this dialog box is shown, your form can't get the focus. After the operator closes the dialog box, your form gets the focus again. The return value of ShowDialog indicates the result.
If you want to save the file as a result of the operator selecting the menu item "file save", or pressing the button "Save", do the following:
private void OnMenuItemFileSave_Clicked(object sender, ...)
{
this.SaveFile();
}
private void OnButtonSave_Clicked(object sender, ...
{
this.SaveFile();
}
How to show a modeless Dialog Box
A modeless dialog box is shown using Form.Show. After you call this, your dialog box can get the focus. Therefore you'll have to remember that the form is being shown.
class MyModelessDialogBox : Form {...}
class MyForm : Form
{
private MyModelessDialogBox {get; set;} = null;
private void ShowMyModelessDialogBox()
{
if (MyModelessDialogBox != null)
{
// Dialog box already shown, TODO: report to operator?
...
return;
}
else
{
// Dialog box not shown yet; create it and show it
this.MyModelessDialogBox = new MyModelessDialogBox();
// if needed, before showing set some properties
// make sure you get notified if the dialog box is closed
this.MyModelessDialogBox.FormClosed += new FormClosedEventHandler(this.MyModelessDialogBoxClosed);
// show the dialog box:
this.MyModelessDialogBox.Show(this); // I am the owner / parent window
}
// when the modeless dialog box is closed, you get notified:
void MyModelessDialogBoxClosed(object sender, FormClosedEventArgs e)
{
// if needed, inspect e.CloseReason
// if needed, handle the DialogResult
// finally: not my form anymore. The form should disposes itself:
this.MyModelessDialogBox = null;
}
}
}
}
Before closing your form you should check if the dialog box is being shown, and close:
private void OnFormClosing(object sender, FormClosingEventArgs e)
{
if (this.MyModelessDialogBox != null)
{
// the modeless dialog box is being shown. Order it to close itself:
this.MyModelessDialogBox.Close();
// this will lead to MyModelessDialogBoxClosed
}
}
Sometimes you have a dialog box that refuses to close, for instance because the dialog box warns the operator and he clicks cancel. In that case, you should not Close the dialog box directly, but add a method to ask the dialog box nicely.
In the dialog box:
bool RequestToClose()
{
bool allowedToClose = this.AskOperatorIfCloseAllowed();
if (allowedToClose)
{
// close this dialog box.
this.Close();
// the owner of the dialog box will be notified via MyModelessDialogBoxClosed
}
return allowedToClose;
}
I am developing a WinForms Application , I design a Hamburger Menu app, Lets say I have a "Button1" & "Button2" on the left panel of my app and I have 2 UserControl(userControl1, userControl2) , then when I click the button1, I call userControl1.BringToFront();
this is my exact code:
if (!panelUSerControl.Controls.Contains(DashboardView.Instance)) {
panelUSerControl.Controls.Add(DashboardView.Instance);
DashboardView.Instance.Dock = DockStyle.Fill;
DashboardView.Instance.BringToFront();
} else {
DashboardView.Instance.BringToFront();
}
So userControl1 is now displayed on the screen , my question is when I click the button2(this will show userControl2), how can I display a confirmation message with option(Yes/No) to inform the user that he/she will leave the userControl1? , and if the user selects No, the app will stays in the userControl1 , and if yes , will show userControl2
Thanks in advance,
NicoTing
You should do this in your button2 click event, i.e. display a confirmation message there and then proceed to show the second control or not:
private void button2_click(object sender, System.EventArgs e)
{
if (MessageBox.Show("Show userControl2?", "Confirmation",
MessageBoxButtons.YesNo) == DialogResult.Yes))
{
userControl2.BringToFront();
}
}
There is a "leave" event on each control but this fires when the user has already left the control, so you cannot use it to confirm before leaving the control.
this is what I did:
if (panelUSerControl.Controls.Contains(userControl1.Instance) && panelUSerControl.Controls.GetChildIndex(userControl1.Instance) == 0) {
if (MessageBox.Show("Are you sure you want to cancel your delivery?", "Confirmation", MessageBoxButtons.YesNo) == DialogResult.No) {
return;
}
}
I have a data grids within two tabs. so when second tab click it should open a filter window (not a <popup>, its a <window>) . I am doing it as follows.
if (tabControl1.SelectedIndex == 1) {
DashboardFilterView filterWindow = new DashboardFilterView();
filterWindow.ShowDialog(); }
When I click the close button of window it is closed.
Question :
After closing popup window, if I click on row of datagrid which is in my current tab, again popup window is displayed.
How can I prevent this from happening more than once?
Since tabControl1_SelectionChanged event is firing for even grid row click, I added a check to confirm whether this event occurs from tab.
e.OriginalSource is TabControl solved the problem.
private void tabControl1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.OriginalSource is TabControl)
{
if (tabControl1.SelectedIndex == 0)
{
// Do something
}
else if (tabControl1.SelectedIndex == 1)
{
DashboardFilterView filterWindow = new DashboardFilterView();
filterWindow.ShowDialog();
}
}
}
Is possible to show "page setup" and "printer setup" as modeless forms? I used code as follows, but that forms display as modal forms:
// page setup
private void btnPageSetup_Click(object sender, EventArgs e)
{
this.pageSetupDialog1.PageSettings = new PageSettings();
this.pageSetupDialog1.PrinterSettings = this.printDocument1.PrinterSettings;
this.pageSetupDialog1.ShowDialog();
if (this.pageSetupDialog1.PageSettings != null)
{
this.printDocument1.DefaultPageSettings = this.pageSetupDialog1.PageSettings;
}
}
// print setup
private void btnPrintSetup_Click(object sender, EventArgs e)
{
this.pageSetupDialog1.Document = this.printDocument1;
if (this.pageSetupDialog1.ShowDialog() == DialogResult.OK)
{
this.printDocument1.Print();
}
}
You can show a form as non-modal by calling Show rather than ShowDialog.
However, you'll also have to shuffle your code around, because your main form will no longer sit and wait for one of the subforms to close in order to check what the user did.
For example, you'll have to change the Print Setup code such that your PageSetupDialog prints the document itself when the user clicks OK, rather than relying on the main form to act when the user has clicked OK.
Similarly, you'll need to change the Page Setup code such that your PageSetupDialog sets Document.DefaultPageSettings itself, rather than "returning" settings in the PageSettings property and relying on the main form handling them.
I am having a message box a simple one inside an if condition and the thing is i want the message box to automatically close when the user points to the OK button in the message box, what i am not able to figure out is how to access the message box OK button
private void button3_Click(object sender, RoutedEventArgs e)
{
Clipboard.Clear();
//string queryvalue;
//queryvalue = SelectedQuery.Value;
//SelectedQuery.Value = queryvalue;
if (QueryChooser.SelectedItem == null)
{
button3.Background = Brushes.PaleVioletRed;
MessageBox.Show("Select a value");
}
else
{
Clipboard.SetText(SelectedQuery.Value);
}
}
In cases like this I've found that it is easier to just create a simple Window resembling a MessageBox and pop it using ShowDialog(), this way you'll have a more flexible "MessageBox".
if (MessageBox.Show("Select a value") == DialogResult.Ok) {
// do something
}
UPDATE
As Saeb already mentioned you should create your own simple MessageBox dialog.