How to get width of button in DateTimePicker-Control/Controls in general? - c#

I implemented a custom DateTimePicker. On the DateTimePicker there is a button. In the examples I've found it's width is set to 16. This is working but I would like to have a dynamic approach.
So, is there a way to get the size of this button or is there a general way to get information about .Net-Control sub elements like size etc.?
Trying DateTimePicker.Controls didn't help me (it's empty).

Most all Win32 arrow buttons key off the scroll bar metric:
You want SystemInformation.VerticalScrollBarArrowHeight
Two helpful classes: SystemInformation and ControlPaint. When dealing with custom controls they can be invaluable.
Int32 size = SystemInformation.VerticalScrollBarArrowHeight;
ControlPaint.DrawScrollButton(e.Graphics, 0, 0, size, size, ScrollButton.Down, ButtonState.Normal);

All windows forms controls have a struct assigned to them called "Size" if you refer to the myButton.Size.Width you should be able to modify the size of the button which is an int. Conversely you can do the same with myButton.Size.Height .

The DateTime picker uses the native Windows controls. The "button" you mention is something that is drawn by Windows itself.
In your current OS, there might be something that resembles a button, but in an old or future OS the DateTime picker might be completely different.
Just draw your control the nicest and easiest for the user, and they will be happy.

Can you post the code for the DateTimePicker that you've implemented? It might help illustrate your exact problem. By the sounds of it, the button isn't contained within the DateTimePicker, but on the form adjacent to it. Please correct me if I'm wrong.
So, if you had the following:
DateTimePicker dtp = new DateTimePicker();
Button btn = new Button();
btn.Size.Width = btn.Size.Width * 2;
You can access and re-set the size of the button control.
If the button really is contained within the DateTimePicker, then expose it as a public property (or ideally an internal property), and access it directly via the DTP.

Related

Windows Forms Adding Text without Textbox

I am using Visual Studio 2019 in order to create a Windows Form Application. I need some titles in my application, which means these strings will not be modified by the user.
For now, I created textboxes for these titles and made these textboxes "read-only". However, this does not satisfy my aesthetical expectations.
Therefore, I wonder if there is a way to add a string without adding a textbox, to the form. Is there a way?
Thanks in advance :)
Consider using a Label control rather than a TextBox.
The only time I would use a TextBox as a label is if I want the user to be able to copy the info, and I make it borderless, readonly and have the same colour as the background of the form. It's not superb UX though as there isn't anything that screams "you can highlight and copy this text" other than an I beam cursor, which is pretty much "mystery meat navigation" - better off putting a copy button next to it if you expect the user to copy info often
Why not use Label for your titles?
Since label, by default, cannot be modified by the user, thats what you want. Textbox is used for the user input, not for the titles.
Use Label control that is the right control to use for your requirement

What is the (technical) difference between a Label and a TextBox?

I have seen a lot of answers on the web stating that a Label's text can't be selected/copied in the way that a TextBox's contents can,
but what is the underlying reason that a Label's text can't be copied?
Windows itself can find text under the cursor position, so why can't the WinForm Label control?
In order for a user to select or copy a control's text, the control must allow you to set focus to it, either by clicking or tabing to the control.
A Label doesn't allow this, by design.
Label controls are typically used to provide descriptive text for a control. For example, you can use a Label to add descriptive text for a TextBox control to inform the user about the type of data expected in the control.
So while Labels and TextBoxes both inherit from System.Windows.Control they are different things, intended for different purposes. In the same way that oranges and apples are both fruit, but are different.
However, if you're creating an application and want to have something that looks like a label, but allows the user to select (but not edit) the text, then you can use a TextBox with the following properties set:
Backcolor = Control
ReadOnly = true
BorderStyle = none
As shown below...
Alternatively, if you have an application and want to get text from something like a label, you can use the Win32 API function GetWindowText, if you know the handle to the window that contains the text. In a Win32 context a "window" means just about anything distinct that is on the screen, not just the windows that you can drag around with your mouse. WinForms is an abstraction on top of all this.
As for getting the handle to the window that is under the mouse cursor, see this question.

How to change DateTimePicker Color inWindows Forms

Is it possible to change the color of dateTimePicker if I change date in Calendar?
Maybe not a direct answer (certainly a belated one):
I always take the DateTimePicker control and place it docked (fill) inside a Panel. I make sure the panel has at least a padding of 1 (on all sides). Then, if I need to change the background color or to simulate a border color change, I do it on the surrounding panel instead of the DateTimePicker control directly.
My use case is to simulate when the control is invalid, i.e. put a Color.Red border on the surrounding panel if the date field in question is required.
There is not a property for changing Backgroundcolor change property but There are several properties that allows you to customize the appearance and behavior of the DateTimePicker control.
If you are using Windows Vista or Windows 7 and using themes such as Aero, then the properties that modifies the color of the calendar has no effect. To see the results of changing the colors of the calendar, we need to disable Visual Styles. For the sake of demonstration, we will do just that. Find Program.cs in the solution explorer and open it by double clicking it. Comment out or delete the line:
Application.EnableVisualStyles();
For more information use this The DateTimePicker Control
You didn't show any effort but..
DateTimePicker class has ValueChanged event that occurs when the Value property changes.
It is not clear which color that you want to change but you can use CalendarMonthBackground or CalendarForeColor properties that you can use with it.
private void DateTimePicker1_ValueChanged(Object sender, EventArgs e)
{
//
}
Looks like changing BackColor or ForeColor properties has no effect on current control. In such a case, general recommendation is like; you have to render it yourself and this can be really hard. Solution on Changing the background color of a DateTimePicker in .NET probably will not work either.
Also there is thread on MSDN forum that uses a custom control inherited from DateTimePicker .

Add label to textbox custom usercontrol in WinForm

I am writing a windows forms application that has a lot of textboxes. I want to add a label or a caption to the textbox, so that I don’t have to drag a lot labels onto the form and deal with positioning etc. So far I have found 2 possible ways to do this.
Create a user control with the label and textbox. How do I get the
control, label and textbox to size appropriately depending on the
text entered since the control will be reusable and different text sizes will be entered. How to get all the
properties and events of the textbox to remain the same.
Extend a normal textbox and add a string property called label or
caption, and show this property at the left of the textbox. I know
this can be done in Web.UI with CSS but is it possible in a winform
and how?
Any suggestions on how to do either of these?
Thanks.
You can create a UserControl that contains a label and a textbox. When you add the user control to your form, both the label and the textbox within will be added simultaneously. You can expose properties of the label and textbox to assign values at design or run time.
Using this method, you can add multiples of the user control to standardize the layout. As far as resizing the controls based on the text, you'll have to subscribe to events and change the sizing manually.
For example, you can subscribe to the TextChanged event of the label and the textbox. When the event fires, you calculate the size of the string and then adjust the width and position of the controls accordingly.
If you get to the point where you have too many textboxes, I would suggest switching to a DataGridView. The GridView component is very well suited for what you're describing, but of course it requires you to accept a grid layout.
One of the bonuses involved in using a GridView is hard to appreciate until you see it in action: it creates only one HWINDOW at a time (two if you're in editing mode). If you create Labels and TextBoxes all over your form, each one is registered with the operating system as an HWINDOW object. All those HWINDOW objects take time to display! In .NET 1.0, WinForms was so slow that dialogs with more than about two dozen controls were unusable. Even though .NET 2.0 is much better in this regard, but you'll still get significantly better performance by using a single control that manages lots of data, as opposed to lots of controls that each manage one piece of data.
Oh, and another option, if you like: you can also try a PropertyGrid. It has the advantage that it will also show help and allow you to create complex editing controls for each element.

Hide/show part of the window with a button click

I have a simple form containing a main view and also some text boxes and an "add" button that I use for adding data that is displayed in the main view (and stored in a text file). What I want to do is to add a little button that will toggle hiding/showing of the adding controls. Such button usually is a small square containing two arrowheads pointing up/down depending on the state. How do I do that?
(I'm sorry for the terrible title, but I don't know the proper name for this. Could anyone tell me?)
I don't think there's something built-in in WinForms for that. When I needed to do something similar, I just changed the Height of the form...
this.ClientSize = new System.Drawing.Size(required_width, required_height);
use a bool for hiding/showing
You can use the forms Height property and the controls could be hidden with Control.Visible = false
I think the word you're looking for is "Collapsible panel".
A quick google/codeproject search will provide you with some links:
http://www.codeproject.com/KB/miscctrl/TgXPPanel.aspx
http://www.codeproject.com/KB/miscctrl/XPCollapsGroupBox.aspx
http://www.codeproject.com/KB/miscctrl/CollapsibleGroupBox.aspx
I suggest you use a SplitContainer control and play with the Panel2.Collapsed property by sitting it to true or false
put the control that you want to hide/show inside panel2 and put the button in panel1. Change the Orientation property to Vertical, and there you go

Categories