How to have user prompt dialogs in Windows 8 metro apps? - c#

I would like to get some user inputs for my app like Name, DOB, etc from a modal window
for this I need a dialog to be displayed which would contain the textboxes and other controls.
Normally in WinForms/WPF I would create a class inherited from the Form/Window class and use the Show/ShowDialog method to present the form to the user
How do I achieve this behavior in Windows 8 metro apps using XAML/C# ?
I have looked at the MessageDialog class under the Windows.UI.Popups namespace
but its for showing only message like the classic MessageBox.
I have looked at another CoreWindowFlyout class and also not sure if that can be used for the behavior I am expecting.

You can simply create a custom UserControl with all the required inputs (such as TextBoxes) and then show it using Popup class.
for example:
Popup myPopup = new Popup();
myPopup.Child = new CustomUserControl();
myPopup.IsOpen = true;

Following on from the spot on answer from MBZ, have you tried looking at "Windows 8 app samples - C#, VB.NET, C++, JavaScript" particularly the "XAML Popup sample".

Related

Prism WPF: How to Open A New Window/Dialog

When using code behind, the code looks like this:
AnotherWindow x = new AnotherWindow();
x.Show() ;
// or x.ShowDialog()
But how can I achieve this using MVVM? Specifically Prism?
In case you need to build a dialog for asking user login input or progressing dialog, MahApps.Metro can be a useful toolkit as it provides you with some built-in dialog UI/functionalities with MVVM pattern. For more information, check some examples here:
https://mahapps.com/controls/dialogs.html
In Prism, there's the InteractionRequest for short-lived dialogs. If you're looking for a long living dialog, like a second application window or shell, you're stuck with new Window ... Show.
To make your dialog service mvvm-friendly, you should hide it behind an interface and make it as generic as possible. Using view model first here eliminates the need to specify a window type, because you can provide a default window that just contains one large ContentControl, and the view can be mapped as DataTemplate.

How to show Windows Form Element Host's Message Box as Modal?

I have windows form application in C#.
I have class library for WPF user control. And I am using that in Windows Form App using Element Host. Now in WPF User control i need to show one message using MessageBox. it's working but displayed modeless on Main Form of Windows Form Application. I want to Show this Message box as model.
Any help please.
Edits:
let me Explain by example:
I have “WindowsFormApp1”
Another is “WPFUserCtrlLib” which is class library. And it has UserControl named as “WPFUserCtrl” so I have “WPFUserCtrl.xaml” and “WPFUserCtrl.xaml.cs” Files.
In WindowsFormApp1 I have MainForm named “Form1”. In Form1 I am using “WPFUserCtrl” using Element Host.
Now there is some logic resides in “WPFUserCtrl.xaml.cs” say
String ErrorMsg=“Error There”;
if(condition)
{
//Do Task
}
else
{
MessageBox.Show(ErrorMsg);
}
So here I need to show ErrorMsg. When this MessageBox is displayed, it is modeless as I am able to access controls on “Form1” like menus, buttons and all.
Did you set the owner of the WPF window to the winforms window?
According to this link that is necessary in order to make the modal dialog work within winforms.
var window = new WpfWindow();
var helper = new WindowInteropHelper(window);
helper.Owner = this.Handle;
window.ShowDialog();
I have tried lot. Couldn't get solution so posted question here and continued to try again with diff approach. And Now I found the solution.
Solution is: use Dispatcher.Invoke
if(condition)
{
//Do Task
}
else
{
Dispatcher.Invoke(new Action(() => {
MessageBox.Show(ErrorMsg);
})
);
}
Now my MessageBox is model.

How to Embed a Form within another Form in c#?

In an internet webpage , there is a constant menu usually placed on the top or left of a page from which user can navigate (They call it Iframes) ..
I would like to know if it is possible to do something like that using WinForm applications or WPF applications in c# .
At present I am simply inheriting forms from a base class . and each time the user needs to navigate , I have no option but to open up a new form with the same Peristent menu ...
Any suggestions here ?
I managed to use usercontrol to embed a form into another ..
Form1 has a userControl , Form2 embedded inside the user control .
things to note was ..
the embedded forms toplevel property should be set to false
the embedded forms FormBorderStyle should be set to none
userControl1.Controls.Clear();
Form2 f = new Form2();
f.Toplevel=flase;
f.Show();
f.TopLevel = false;
userControl1.Controls.Add(f);
You could use an MDI-container in WinForms.
see here and here for more information.
You mean like an MDI application (http://www.codeproject.com/KB/cs/myBestMDI.aspx) or just using a SplitContainer on the form? Really there are many options. WPF has ElementHost I think. Did you do any research yet? What did you find?
You could also take the toolbox approach. Have a parent program start the menu form and then other forms can use it... or it can launch from it... what ever your use case is.
Well, there should be a frame component in WPF which will give you such an option. Then you would use a "view" concept to open the WPF Pages you direct the user to.

Winform Templating System like ASP.NET MasterPage or MS Access SubForm

Does a Winform Framework exist for something similar to ASP.NET Masterpage or MS Access SubForm ?
With MS Access SubForm you can do like ASP.NET Masterpage. It's a huge loss of time with Winform when having to create a lot of complex form. You have to compensate with either Code Generation which create code duplication or do Runtime Dynamic Form which is much more difficult.
I searched on the Internet but can't find any.
The closes thing to Master Pages is Form Inheritance. It is regular class inheritance but also supported by the Designer. To try it:
1) Add a form with Ok and Cancel Buttons, Build project (essential)
2) Choose Project, Add new item, Windows and then the Inherited Form template. Pick the Form from step 1) as the base Form. Add some controls.
3) Repeat step 2) a few times
4) make some Buttons to show the Forms, Build and Test
5) Go back to the Form from 1) and change a few things (Background), run again
Your other tool are UserControls, they work much the same as in ASP.NET. You develop them like Forms and apply them as Controls.
You can add forms to a form, or to a panel on a form.
public Form1()
{
InitializeComponent();
Form2 embeddedForm = new Form2();
embeddedForm.TopLevel = false;
Controls.Add(embeddedForm);
embeddedForm.Show();
}
You will need to set the FormBorderStyle to None, unless you want to have an actual moveable form inside your form.
I was in a bit of a hurry at the time of posting, but Henk is right. You should consider creating a user control for this instead. Not to be confused with a custom control, which is intended for when you need to do your own drawing instead of using collections of standard Windows controls.

.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?

Categories

Resources