How to show DateTimePicker control on messagebox using c#? - c#

Is it possible to show DateTimePicker control on messagebox to select date in Console applciation.

Not on a standard MessageBox. You need to create your own form to display.

`public MyClass()
{
// Create a new DateTimePicker
DateTimePicker dateTimePicker1 = new DateTimePicker();
Controls.AddRange(new Control[] {dateTimePicker1});
MessageBox.Show(dateTimePicker1.Value.ToString());
dateTimePicker1.Value = DateTime.Now.AddDays(1);
MessageBox.Show(dateTimePicker1.Value.ToString());
}
`
This a method to access the date picker control in your message box.
What have you done exactly? Can you explain the requirement a little more elaborate?

MessageBox is a convenient function. That convenience comes at the cost of customization. MessageBox does not provide a way to add your own controls to it; you must create your own form.
Note that even in console applications you can still create windows forms.

I know its late but if someone is looking how to do it. Use CustomMessageBox insetead of MessageBox. Tutorial can be found Here
TimePicker timePicker = new TimePicker()
{
Margin = new Thickness(0, 10, 0, 10)
};
StackPanel myStackPanel = new StackPanel();
myStackPanel.Orientation = System.Windows.Controls.Orientation.Vertical;
myStackPanel.Children.Add(timePicker);
//Create a new custom message box
CustomMessageBox messageBox = new CustomMessageBox()
{
Content = myStackPanel,
LeftButtonContent = "Ok",
RightButtonContent = "Cancel",
IsFullScreen = false
};
messageBox.Show();

Related

Label not visible above ToolStrip

At runtime I add (and remove) several controls, as needed, to a main window which in Designer contains only a ToolStrip with some function buttons. In some cases I want to add an info label next to the toolStrip, but I cannot make it visible, ie. it is hidden below. The code for the label is straightforward
infoLabel = new Label();
infoLabel.AutoSize = true;
infoLabel.Location = new System.Drawing.Point(200, 10);
infoLabel.Size = new System.Drawing.Size(35, 13);
infoLabel.BackColor = System.Drawing.SystemColors.Control;
infoLabel.Font = new System.Drawing.Font("Arial", 13);
infoLabel.ForeColor = System.Drawing.Color.Black;
infoLabel.TabIndex = 1;
infoLabel.Text = "this is info";
infoLabel.BringToFront();
this.Controls.Add(infoLabel);
TabIndex and BringToFront I added as an act of desperation, it does not help. BTW the ToolStrip's TabIndex is 2, and its BackColor I changed to transparent.
However, when I placed a label over the ToolStrip in the Designer, it is visible (ie. on top). I analysed the code then but did not see anything different from what I am writing. What am I missing here?
I suggest calling infoLabel.BringToFront(); at the very end, at least after this.Controls.Add(infoLabel); you current code amended:
infoLabel = new Label();
...
infoLabel.Text = "this is info";
// First Add to this
this.Controls.Add(infoLabel);
// Only then we can make infoLabel be the topmost
// among all existing controls which are on this
infoLabel.BringToFront();
We create infoLabel, add it to this and finally make it topmost on this. To make code more readable I suggest something like this:
// Create a label on this
infoLabel = new Label() {
AutoSize = true,
Location = new System.Drawing.Point(200, 10),
Size = new System.Drawing.Size(35, 13),
BackColor = System.Drawing.SystemColors.Control,
Font = new System.Drawing.Font("Arial", 13),
ForeColor = System.Drawing.Color.Black,
TabIndex = 1,
Text = "this is info",
Parent = this // <- instead of this.Controls.Add(infoLabel);
};
// make infoLabel topmost among all controls on this
infoLabel.BringToFront();
Windows Forms controls do not have a property which you can use to set z-index of controls like one can do in CSS.
You'll need to call Parent.SetChildIndex(control, 0);. The control at the front of Controls collection is the topmost in z-order for a container control.

c# winforms dock fill tree-view make it disappear

I have a winforms application. In my application I have a user control which I loaded programmatically.
Inside this user-control I have tree view that also will be loaded with items programmatically. My problem is that I want to make my tree-view take the whole size of its parent.
What I have tried
I set the user-control Dock property to DockStyle.Fill to make it take the size of its parent.
I have done the same for the tree-view Dock property; set it to DockStyle.Fill.
What I get
The user-control takes the full size as expected but the tree-view looks like it is hidden. I checked the height, and I noticed it's 0. When I tried to change the height while it has DockStyle.Fill I can't, it changes back to 0.
Any ideas?
Update
The auto generated code for the tree-view:
private void InitializeComponent()
{
this.btnAddServer = new System.Windows.Forms.Button();
this.pnlServersContainer = new System.Windows.Forms.FlowLayoutPanel();
this.treeViewServers = new System.Windows.Forms.TreeView();
this.pnlServersContainer.SuspendLayout();
this.SuspendLayout();
//
// btnAddServer
//
this.btnAddServer.Location = new System.Drawing.Point(89, 478);
this.btnAddServer.Name = "btnAddServer";
this.btnAddServer.Size = new System.Drawing.Size(107, 23);
this.btnAddServer.TabIndex = 3;
this.btnAddServer.Text = "Add New Server";
this.btnAddServer.UseVisualStyleBackColor = true;
this.btnAddServer.Click += new System.EventHandler(this.btnAddServer_Click);
//
// pnlServersContainer
//
this.pnlServersContainer.AutoScroll = true;
this.pnlServersContainer.Controls.Add(this.treeViewServers);
this.pnlServersContainer.Dock = System.Windows.Forms.DockStyle.Fill;
this.pnlServersContainer.Location = new System.Drawing.Point(0, 0);
this.pnlServersContainer.Name = "pnlServersContainer";
this.pnlServersContainer.Padding = new System.Windows.Forms.Padding(8, 20, 0, 0);
this.pnlServersContainer.Size = new System.Drawing.Size(318, 463);
this.pnlServersContainer.TabIndex = 2;
//
// treeViewServers
//
this.treeViewServers.Dock = System.Windows.Forms.DockStyle.Fill;
this.treeViewServers.Location = new System.Drawing.Point(11, 23);
this.treeViewServers.Name = "treeViewServers";
this.treeViewServers.Size = new System.Drawing.Size(275, 0);
this.treeViewServers.TabIndex = 0;
this.treeViewServers.DoubleClick += new System.EventHandler(this.treeViewServers_DoubleClick);
//
// ucServersList
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.Controls.Add(this.btnAddServer);
this.Controls.Add(this.pnlServersContainer);
this.Name = "ucServersList";
this.Padding = new System.Windows.Forms.Padding(0, 0, 0, 60);
this.Size = new System.Drawing.Size(318, 523);
this.Load += new System.EventHandler(this.ucServersList_Load);
this.pnlServersContainer.ResumeLayout(false);
this.ResumeLayout(false);
}
I recommend to open Document outline editor in Visual Studio.
This shows all controls in their hierarchical order as tree.
It lets you also drag & drop the controls to the right place.
Open it with View > Other windows > Document outline.
You may fix your problem when looking at the controls order.
I have figured it out. but still don't know why this happened!
my tree-view was inside FlowLayoutPanel not Panel. When i changed it to Panel everything goes fine. that's it!
The problem might be that you have added several items to the same parent control, and then when you fill the parent dock with one of them, the behaviour would not be what you expect.
Use a splitcontainer. And when you want to fill out the dock, make sure your control belongs to two differnt panels of a splitcontainer.
See this for an concrete example.
Is there any specific reason why you use FlowLayoutPanel?
It seems that the FlowLayoutPanel does not deal with any other than Dock.None.
I think you should use a simple Panel for this application, because it does not resize the contained controls - the Dock property behaves as expected.
Replacing the FlowLayoutPanel with a Panel will fix your problem.
This is a super old question... but since there are no accepted answers I’ll give it a go.
This happened to me when my Control was set to autosize. Either removing autosize or specifying a minimum height could solve this issue.

C# Window Form Application, how to efficiently create a dynamic panel on a window form

I would like to create a few panel dynamically in my window form application. Each panel will consist of 3 labels and one text box and one button. Now I know I can hard code this all at once by declaring each variable every time, but it takes a lot of coding and it is obviously not efficient at all.
So my question is: Is there a way to create pre-define panel dynamically where each time a panel is created will have a predefined layout setup already. So all i need to do is to add a panel, its location and size every time, and all the content(like labels, text-box and button) inside the panel are already setup with their location associated with the panel itself. Do I really have to create a class just for that?
Thanks in advance for read and taking your time.
Create Windows Forms control or user control, see http://msdn.microsoft.com/en-us/library/6hws6h2t.aspx
Create a user control, and place on it whatever you like (your labels). Expose public methods/properties of that control so you can control the contents of it. Place as many of those on the form as you like, they will all look and behave same.
Here is an example you can play with...
for (int i = 1; i < 5; i++)
{
var panel1 = new Panel() { Size = new Size(90, 80), Location = new Point(10, i * 100), BorderStyle = BorderStyle.FixedSingle };
panel1.Controls.Add(new Label() { Text = i.ToString(), Location = new Point(10, 20) });
panel1.Controls.Add(new Label() { Text = i.ToString(), Location = new Point(10, 40) });
panel1.Controls.Add(new Label() { Text = i.ToString(), Location = new Point(10, 60) });
Controls.Add(panel1);
}

How to add tooltip to user defined textbox on a winform

I have a uvSelfLoadingTextBox with multiple instances on a form.
I would like to load the tooltip with the _value property at run time.
I've tried
public ucSelfLoadingTextBox()
{
Windows.Forms.ToolTip myToolTip;
myToolTip.AutomaticDelay = 5000;
myToolTip.AutoPopDelay = 50000;
myToolTip.InitialDelay = 100;
myToolTip.ReshowDelay = 500;
myToolTip.SetToolTip(this, _value);
inside the control but that does not work.
I have tried using the tooltip that is dragged onto the form
ucSelfLoadingLogicTextBox uc = new ucSelfLoadingLogicTextBox();
toolTipA.SetToolTip(uc,uc._value );
and that does not work.
What is the correct way to do this?
You forgot to instantiate myToolTip. You need to set it to new Tooltip().
Also, I don't think it's a good practice to assign the tooltip in the textbox's constructor. You could do this in OnCreateControl() (that you need to override).
Your code could therefore become:
protected override void OnCreateControl()
{
base.OnCreateControl();
var myToolTip = new System.Windows.Forms.ToolTip
{
AutomaticDelay = 5000,
AutoPopDelay = 50000,
InitialDelay = 100,
ReshowDelay = 500
};
myToolTip.SetToolTip(this, this.Text);
}
Many visible controls on windows form have ToolTip property. Just set the Tooltip with your newly created one. You can also add tooltip to your form. Have you tried this?
myToolTip.ShowAlways = true;
And try to set this tip to a button control. This may be a good test for your tooltip.

How to create, in code behind, a Custom MessageBox with Images in WPF C#?

How to create, in code behind (with no XAML), a custom MessageBox (Dialog Boxes) in WPF C#?
I googled it and seems not to find a solution. I would want to have a MessageBox with Images and other Controls add to it.
You may use this solution:
string messageBoxText = "Do you want to save changes?";
string caption = "Your caption";
MessageBoxButton button = MessageBoxButton.YesNoCancel;
MessageBoxImage icon = MessageBoxImage.Warning;
MessageBox.Show(messageBoxText, caption, button, icon);
look over this article, you my recode all Xaml into pure c# in Custom Dialog Boxes paragraph if you want.
or you may create your own Window and use MyWindow.ShowDialog().
Like in this code:
Window wnd = new Window();
Grid grid = new Grid();
wnd.Height = 200;
wnd.Width = 150;
grid.RowDefinitions.Add(new RowDefinition {Height = new GridLength(100) });
grid.RowDefinitions.Add(new RowDefinition {Height = GridLength.Auto });
wnd.Content = grid;
Image img = new Image();
Button btn = new Button();
btn.Content = "OK";
btn.VerticalAlignment = VerticalAlignment.Bottom;
Grid.SetRow(img, 0);
Grid.SetRow(btn, 1);
grid.Children.Add(img);
grid.Children.Add(btn);
wnd.Owner = MyMainWindow;
wnd.ShowDialog();
Why don't you just create your custom Window in XAML and then use showDialog() in code-behind?
Everything from XAML can be rewrited in pure c#:
<Window>
<Grid>
<Grid.ColumnDefinition Width="50" />
<Grid.ColumnDefinition Width="*">
<Label Grid.Column="0" Content="Hello World!" />
</Grid>
</Window>
will looks like this:
public void MakeWin(DependencyObject owner)
{
MakeWin(Window.GetWindow(owner));
}
public void MakeWin(Window owner)
{
Window window = new Window();
Grid grid = new Grid();
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(50) });
grid.ColumnDefinitions.Add(new ColumnDefinition {Width = GridLength.Auto});
window.Content = grid;
Label label = new Label { Content = "Hello World!" };
Grid.SetColumn(label, 0); // Depandency property
grid.Children.Add(label);
window.Owner = owner;
window.ShowDialog();
}
For images, there is source code (or a prebuilt one) for you in the WPF toolkit http://wpftoolkit.codeplex.com/wikipage?title=MessageBox&version=31
If you need more than an image, a line of text and a couple of buttons, then you should probably start looking at just using a new Window invoked with ShowDialog()
I've implemented a WPF MessageBox that has the exact same interface has the normal one and is also fully customizable via standard WPF control templates:
A Customizable WPF MessageBox
Features
The class WPFMessageBox has the exact same interface as the current WPF MessageBox class.
Implemented as a custom control, thus fully customizable via standard WPF control templates.
Has a default control template which looks like the standard MessageBox.
Supports all the common types of message boxes: Error, Warning, Question and Information.
Has the same “Beep” sounds as when opening a standard MessageBox.
Supports the same behavior when pressing the Escape button as the standard MessageBox.
Provides the same system menu as the standard MessageBox, including disabling the Close button when the message box is in Yes-No mode.
Handles right-aligned and right-to-left operating systems, same as the standard MessageBox.
Provides support for setting the owner window as a WinForms Form control.

Categories