How do you display a custom UserControl as a dialog in C#/WPF (.NET 3.5)?
Place it in a Window and call Window.ShowDialog.
(Also, add references to: PresentationCore, WindowsBase and PresentationFramework if you have not already done so.)
private void Button1_Click(object sender, EventArgs e)
{
Window window = new Window
{
Title = "My User Control Dialog",
Content = new MyUserControl()
};
window.ShowDialog();
}
Window window = new Window
{
Title = "My User Control Dialog",
Content = new OpenDialog(),
SizeToContent = SizeToContent.WidthAndHeight,
ResizeMode = ResizeMode.NoResize
};
window.ShowDialog();
Has worked like a magic for me.
Can it be made as a modal dialog?
Ans : ShowDialog it self make it as Modal Dialog.. ...
As far as I know you can't do that. If you want to show it in a dialog, that's perfectly fine, just create a new Window that only contains your UserControl, and call ShowDialog() after you create an instance of that Window.
EDIT:
The UserControl class doesn't contain a method ShowDialog, so what you're trying to do is in fact not possible.
This, however, is:
private void Button_Click(object sender, RoutedEventArgs e){
new ContainerWindow().ShowDialog();
}
namespace System.Window.Form
{
public static class Ext
{
public static DialogResult ShowDialog(this UserControl #this, string title)
{
Window wind = new Window() { Title = title, Content = #this };
return wind.ShowDialog();
}
}
}
The use of it maybe as simple as UserControlInstance.ShowDialog().
A better customized implementation would be by extending the Window class and customizing it using the the designer and code to get any functionality.
I know this is for .net 3.5, but here is a workable solution for .net 2.0
MyUserControl myUserControl= new MyUserControl();
Form window = new Form
{
Text = "My User Control",
TopLevel = true,
FormBorderStyle = FormBorderStyle.Fixed3D, //Disables user resizing
MaximizeBox = false,
MinimizeBox = false,
ClientSize = myUserControl.Size //size the form to fit the content
};
window.Controls.Add(myUserControl);
myUserControl.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
window.ShowDialog();
You can also use MaterialDesignThemes.Wpf (downloadable on NuGet, .NET 4.5+). Then you can simply do:
{
var view = new YourUserControl();
var result = await DialogHost.Show(view, "RootDialog", ClosingEventHandler);
}
private void ClosingEventHandler(object sender, DialogClosingEventArgs eventArgs)
{ } //Handle Closing here
If the answer by 'sixlettervariables' is modified as so, it works
private void button1_Click ( object sender, RoutedEventArgs e )
{
Window window = new Window
{
Title = "My User Control Dialog",
Content = new UserControl ( ),
Height = 200, // just added to have a smaller control (Window)
Width = 240
};
window.ShowDialog ( );
}
Related
I am having a little problems with my frame.
I write a static method, it draws a content dialog with a textbox inside, if the textbox isn't empty, it should open the page_2.
Currently I am using "ContentFrame.Navigate(typeof(Page_2), "operation");" ,
But my project have a pane Hamburguer menu on mainpage and it should be on all the pages.
and if I use ContentFrame.Navigate it disappears.
Here are my static content dialog:
public static async void D_NewProject(double Widthmax)
{
ContentDialog D_NewProject = new ContentDialog()
{
Title = "New Project",
MaxWidth = Widthmax, // this.ActualWidth,
};
StackPanel D_panel = new StackPanel();
TextBox TextBox_D1 = new TextBox()
{
PlaceholderText = "Name:",
TextWrapping = TextWrapping.Wrap,
};
D_panel.Children.Add(TextBox_D1);
D_NewProject.Content = D_panel;
D_NewProject.PrimaryButtonText = "OK";
D_NewProject.PrimaryButtonClick += delegate
{
if (TextBox_D1.Text != "")
{
/**/
Frame ContentFrame = Window.Current.Content as Frame;
ContentFrame.Navigate(typeof(Page_2), "operation");
/**/
}
};
await D_NewProject.ShowAsync();
}
If you need more code like the pane, you can ask for it.
Any help is appreciated.
If you want to have a layout page that would be active in all page,
you need to make a method in App.xaml.cs which loads the current page inside of this layout.
I can suggest you this method that I use it myself:
public static void LoadLayout(Type pageType)
{
// just ensure that the window is active
RootFrame = new _Layout();
RootFrame.ContentFrame.Navigate(pageType, null);
RootFrame.ContentFrame.Navigated += ContentFrame_Navigated;
ContentFrame_Navigated(null, null);
Window.Current.Content = RootFrame;
}
The ContentFrame_Navigated delegate is used just for controlling the items on the layout (for example: Buttons, ...).
i have been creating a cefsharp browser in C#. i have made it so you can have multiple tabs and it loads the pages correctly. however, i cannot seem to find how i can rename the tab to the name of the page.
this is the code in the load event for form1.cs:
Cef.Initialize();
toolTip1.SetToolTip(button1, "Settings");
TabPage tab = new TabPage();
Tab newtab = new Tab();
newtab.Show();
newtab.TopLevel = false;
newtab.Dock = DockStyle.Fill;
tab.Controls.Add(newtab);
tabControl1.TabPages.Add(tab);
i have tried:
private void myBrowser_isLoading(object sender)
{
myBrowser.Parent.Parent.Text = myBrowser.Title;
}
but that doesn't work.
then this is the code for tab.cs:
public Tab()
{
InitializeComponent();
// Start the browser after initialize global component
InitializeChromium();
}
public CefSharp.WinForms.ChromiumWebBrowser myBrowser;
public bool nav = new bool();
public void InitializeChromium()
{
myBrowser = new CefSharp.WinForms.ChromiumWebBrowser("http://www.google.com");
this.Controls.Add(myBrowser);
myBrowser.Dock = DockStyle.Fill;
myBrowser.Parent = panel2;
if (nav == true)
{
myBrowser.Load(textBox1.Text);
nav = false;
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Cef.Shutdown();
}
again, i am using c# with the latest build of cef sharp(or atleast the one installed from the nuget package manager).
In your tab function in form1.cs, you need to add a title changed function like this
browser.TitleChanged += OnBrowserTitleChanged;
you also need to specify what browser is and set dockstyle to fill like this
ChromiumWebBrowser browser = new ChromiumWebBrowser("google.com");
tab.Controls.Add(browser);
browser.Dock = DockStyle.Fill;
now for the OnBrowserTitleChanged, you will need an EventArg which will tell the tab to have the document title in this format
this.InvokeOnUiThreadIfRequired(() => browserTabControl.SelectedTab.Text = args.Title);
this will add the document title to the tabcontrol browserTabControl is the name of the tabcontrol you will have to change browserTabControl to whatever name you have for the tabcontrol. Also the code you have does not belong with cef initialize. you need to create an addNewTab method with all the functions that will be processed when you want to add a new tab. Also, you cannot use a panel if you want to have tabs. you need a tabcontrol
I am trying to open a file inside a rich text box that is dynamically created in a tabpage on click. But it is inside a split container along with another element. For some reason when I try to access it, I get a Object reference not set to an instance of an object error.
Here's the code:
Dynamic creation of said tab page:
public class Texttab : TabPage
{
readonly RichTextBox _text = new RichTextBox();
ConsoleControl.ConsoleControl ca = new ConsoleControl.ConsoleControl();
private SplitContainer split = new SplitContainer();
public Texttab()
{
split.Dock = DockStyle.Fill;
split.Orientation = Orientation.Horizontal;
split.Name = "split";
_text.Dock = DockStyle.Fill;
_text.Name = "textbox";
_text.Font = fontx;
_text.BackColor = Color.FromName(back);
_text.ForeColor = Color.FromName(front);
ca.Dock = DockStyle.Fill;
ca.Name = "cmdbox";
ca.StartProcess("cmd", null);
ca.Font = fonty;
Controls.Add(split);
split.Panel1.Controls.Add(_text);
split.Panel2.Controls.Add(ca);
}
}
Code accessing the TEXTBOX and opening the text file:
private void OpenToolStripMenuItemClick(object sender, EventArgs e)
{
var dx = new OpenFileDialog();
dx.ShowDialog();
dx.Filter = Resources.Form1_openToolStripMenuItem_Click_Text_Files___txt____txt_Python_Files___py____py_Javascript_Files___js____js_C_Files___c____c_CPP_Files___cpp____cpp_Shell_Files___sh__bat____sh___bat_All_Files__________;
RichTextBox selectedRtb = (RichTextBox)tabControl1.SelectedTab.Controls["split"].Controls["textbox"];
selectedRtb.LoadFile(dx.FileName, RichTextBoxStreamType.PlainText);
}
Thanks, any help on the issue is appreciated. I am almost positive the issue has something to do with the split control. Thanks again!
You will have to access the RichTextBox like this instead since the Panels in the SplitContainer are not named items.
(RichTextBox)((SplitContainer )tabControl1.SelectedTab.Controls["split"]).Panel1.Controls["textbox"]
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
create custom tooltip C#
Does anyone know of a way to make a box 'popup' when the user cursors over a certain item?
For example, I want to have a PictureBox on a C# forms application and when the user cursors over it, a box of text will pop up.
I'm aware of ToolTip however I was thinking of something more customisable; in my mind I'm thinking of the kind of popup boxes you see in World of WarCraft when you cursor over an item in your inventory (obviously it doesn't have to be THAT flashy, but at least one where the text colour, background colour, text etc. are all modifiable).
You can use a ToolStripControlHost to host a control (for instance a panel) and add the content you want. Then you add that control to a ToolStripDropDown using the Items collection, and use the Show(Control,Point) method to show the control.
Thought I'd add an example
public class Form1 {
public Form1() {
ToolStripDropDown customToolTip = new ToolStripDropDown();
customToolTip.Items.Add(new CustomPopupControl("Hello", "world"));
MouseMove += (o, e) => {
Point location = e.Location;
location.Offset(0, 16);
customToolTip.Show(this, location);
};
}
class CustomPopupControl : ToolStripControlHost {
public CustomPopupControl(string title, string message)
: base(new Panel()) {
Label titleLabel = new Label();
titleLabel.BackColor = SystemColors.Control;
titleLabel.Text = title;
titleLabel.Dock = DockStyle.Top;
Label messageLabel = new Label();
messageLabel.BackColor = SystemColors.ControlLightLight;
messageLabel.Text = message;
messageLabel.Dock = DockStyle.Fill;
Control.MinimumSize = new Size(90, 64);
Control.Controls.Add(messageLabel);
Control.Controls.Add(titleLabel);
}
}
}
I mean if it a button or an image button you can add something like MouseHover action and then show your message
private void button1_MouseHover(object sender, System.EventArgs e)
{
MessageBox.Show("yourmessage");
}
you need to customize the tooltip. refer to
http://www.codeproject.com/Articles/98967/A-ToolTip-with-Title-Multiline-Contents-and-Image
There are some other articles there, but this one works fine for me.
You may need to add code for your requirement.
It seems that the Label has no Hint or ToolTip or Hovertext property. So what is the preferred method to show a hint, tooltip, or hover text when the Label is approached by the mouse?
You have to add a ToolTip control to your form first. Then you can set the text it should display for other controls.
Here's a screenshot showing the designer after adding a ToolTip control which is named toolTip1:
yourToolTip = new ToolTip();
//The below are optional, of course,
yourToolTip.ToolTipIcon = ToolTipIcon.Info;
yourToolTip.IsBalloon = true;
yourToolTip.ShowAlways = true;
yourToolTip.SetToolTip(lblYourLabel,"Oooh, you put your mouse over me.");
System.Windows.Forms.ToolTip ToolTip1 = new System.Windows.Forms.ToolTip();
ToolTip1.SetToolTip( Label1, "Label for Label1");
just another way to do it.
Label lbl = new Label();
new ToolTip().SetToolTip(lbl, "tooltip text here");
Just to share my idea...
I created a custom class to inherit the Label class. I added a private variable assigned as a Tooltip class and a public property, TooltipText. Then, gave it a MouseEnter delegate method. This is an easy way to work with multiple Label controls and not have to worry about assigning your Tooltip control for each Label control.
public partial class ucLabel : Label
{
private ToolTip _tt = new ToolTip();
public string TooltipText { get; set; }
public ucLabel() : base() {
_tt.AutoPopDelay = 1500;
_tt.InitialDelay = 400;
// _tt.IsBalloon = true;
_tt.UseAnimation = true;
_tt.UseFading = true;
_tt.Active = true;
this.MouseEnter += new EventHandler(this.ucLabel_MouseEnter);
}
private void ucLabel_MouseEnter(object sender, EventArgs ea)
{
if (!string.IsNullOrEmpty(this.TooltipText))
{
_tt.SetToolTip(this, this.TooltipText);
_tt.Show(this.TooltipText, this.Parent);
}
}
}
In the form or user control's InitializeComponent method (the Designer code), reassign your Label control to the custom class:
this.lblMyLabel = new ucLabel();
Also, change the private variable reference in the Designer code:
private ucLabel lblMyLabel;
I made a helper to make life easier.
public static class ControlUtilities1
{
public static Control AddToolTip(this Control control, string title, string text)
{
var toolTip = new ToolTip
{
ToolTipIcon = ToolTipIcon.None,
IsBalloon = true,
ShowAlways = true,
ToolTipTitle = title,
};
toolTip.SetToolTip(control, text);
return control;
}
}
Call it after controls are ready:
InitializeComponent();
...
linkLabelChiValues.AddToolTip(title, text);
It's an way to keep consistent tool tip styles.