How to create a dialog for use by an Excel AddIn? - c#

I am familiar with the basics of Excel AddIns, but have no idea how to design, implement and later display an internal dialog.
See standing question with images here:
https://social.msdn.microsoft.com/Forums/en-US/935ebeae-1b88-4609-ba33-b0e522d2797f/how-to-create-a-dialog-for-use-by-an-excel-addin?forum=exceldev
TIA
Notes:
(1) My programming language is C#
(2) I prefer to design dialogs by drawing them.

You can use the MessageBox class, for example:
const string message =
"Are you sure that you would like to close the form?";
const string caption = "Form Closing";
var result = MessageBox.Show(message, caption,
MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
// If the no button was pressed ...
if (result == DialogResult.No)
{
// cancel the closure of the form.
e.Cancel = true;
}
If you want to customize the dialog window on your own, you can add a new Windows Form to the project and then add the required controls. After creating an instance of the form in the code you may show it using the Show or ShowDialog methods.

Related

I cannot get the messagebox to work in the C# code

I am trying to add a confirmation messagebox in C# code and the examples I have found and I have added the below example but I keep getting error message "The name 'MessageBox' does not exist in the current context" I am pretty new to C# and need help as I need a confirmation message for the user of the page.
I have tried adding the Using system.windows.form to see if that resolves the messagebox issue but so far no luck
const string message = "Are you sure that you would like to close the form?";
const string caption = "Form Closing";
var result = MessageBox.Show(message, caption,
MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
// If the no button was pressed ...
if (result == DialogResult.No)
{
// cancel the closure of the form.
e.Cancel = true;
}
Based on a compare I will perform, I will need this messagebox to prompt and ask the user if they are sure they want to continue or not and if not it is to return to the page, if so then the new code executes.
In the Solution Explorer: Right Click your Project -> Add -> Reference... -> Assemblies -> In the top right is a searchbar, enter "Forms" -> System.Windows.Forms -> OK
You are missing adding references to the System.Windows.Forms dll. Please add the references and using statement.

DialogResult in WPF Application

I am currently developing an application in C# using WPF, I have always only used WinForms. Normally if I want to ask the user a question instead of making my own dialogue I use
DialogResult result = MessageBox.Show(
"My Message Question", "My Title", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
This is the first time that I have used a WPF form and DialogResult does not seem to be available. What do I use to get the same effect?
Here is how you do the same in WPF:
MessageBoxResult result = MessageBox.Show("My Message Question", "My Title", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (result == MessageBoxResult.Yes)
{
// Do this
}
Use MessageBoxResult instead. And use the MessageBox class. But this message box will look pretty ugly "classic" style.
Another option would be to use Extended WPF toolkit
Yet another option would be to go here and download CrossTechnologySamples.exe then look into the VistaBridge project. I recommend you give a good look here because you will find other samples for other dialogs (like FileOpen, FileSave etc.) that do not exist by default in WPF.

how to force user to respond to message box in c# windows application

I want to show messagebox to the user, such that the user cannot refuse to confirm the message box. User should not be allowed to do anything else in the screen until he confirms the messagebox. This is a windows based c# application. The main thing is, even if i use windows message box. Some times it is hiding behind some screen. But for my case, i want message box to be on top most whenever it appears. I am using some other third party applications, which over rides my message box. I want to overcome this. How to do this...
Invoke messagebox inside the constructor of your form.
public Form1()
{
if (MessageBox.Show(this, "Confirm?", "Attention", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
{
}
else
{
}
InitializeComponent();
}
OR
Invoke another form instance using ShowDialog() method,
public Form1()
{
Form2 frm=new Form2();
frm.ShowDialog(this);
}
You might have to define a custom message box (a Form) and set its TopMost property to true.This will make it on top of ever other window, except other TopMost windows.
That's assuming you want it on top of other applications too, which I'm not sure it's what you're looking for...
The normal windows MessageBox() function should do exactly this unless I'm missing something in your question.
Always specify a owner when displaying a message box.

.net C# windows Form Application : open popup window

I am using Windows Form application. I want to open a small textbox on a window to enter user's name or Email in Starting for the program.
How can I achieve this?
Write one, 'tis almost trivial (creating the form and adding label, textbox and buttons) and using the VB one is perputating something that was only put in to appease the baying mob.
Key method is ShowDialog() which is a method on a Form.
On the form make sure you set the flags for the Ok and Cancel buttons correctly and provide a property (ideally) to allow you to read (and write if necessary) the text box
You can then do something along the lines of the following:
using(MyInputForm mif = new MyInputForm)
{
if (mif.ShowDialog() == DialogResult.OK)
{
dataFromDialog = mif.InputData;
}
else
{
// logic to deal with cancel
}
}
You can do something similar in WPF, don't have an example to hand though.
Maybe the answer to this question will help:
What is the C# version of VB.net’s InputDialog?

C#: What is the proper way to show a form for "frmMainForm" settings and disposing?

Backdrop: There are two forms, the main application form and a form to edit various settings for the operations of main application.
What would be the proper way to show the frmSettings form and how to dispose it once clicked OK or Cancel in the actual frmSettings form?
Perhaps a Dialog would be better suited to your Settings "form." There are subtle differences between a dialog and a form that would make the dialog easier to handle. A return code indicating the button that was clicked makes dialogs useful.
Supposing that you used a dialog - a using statement could be used (off the top of my head):
using (DialogSettings dlgSettings = new DialogSettings)
{
if (dlgSettings.ShowDialog() == DialogResult.OK)
{
}
}
If you insist on using a form then you would have to
Instance the form
show the form
record whether ok or cancel was clicked to a form level variable (from within the forms ok/cancel button click code)
hide the form
save the recorded value from the form
dispose of the form
make use of the saved ok/cancel value
fyi, using "frm" is not a recommended part of the C# coding guidelines. Microsoft prefers you don't use hungarian notation in .NET at all.
using (frmSettings s = new frmSettings() )
{
if( s.ShowDialog() == DialogResult.OK )
{
//do work
}
}
In the main application declare an instance and show it.
using(frmSettings settingsInstance = new frmSettings())
{
settingsInstance.Show(); //or ShowDialog()
}
Then just close the form when done...

Categories

Resources