Hi I'm currently working in a project that involves the use of DateTimePicker control, as the peroson in-charge of the UI of the system, I would my controls to "responsive (in web dev)" so that in any screen sizes my control will adjust to it, but until I encounter the calendar dropdown of the dateTimePIcker, in the runtime after the initialization everytime I assign a new size for the CalendarSize is not being inherit or I don't really know whats happening, so On change of window size I want my dropdown calendar to have the same width of the dateTimePicker and construct the height by a formula.
BTW: My DateTimePicker is Anchored Top, Left, Right so the width changes.
In My Code:
private void ResizeDateTimePicker()
{
int dtpCurrWidth = this.dateTimePicker.Size.Width;
int dtpCurrHeight = dtpCurrWidth - (dtpCurrWidth / 2);
((Telerik.WinControls.UI.RadDateTimePickerElement)(this.dateTimePicker.GetChildAt(0))).CalendarSize = new System.Drawing.Size(dtpCurrWidth, 200);
//I change the Calendar Size of the DateTimePicker with this code because this is how telerik change the CalendarSize in Design View
}
In my Design:
this.dateTimePicker.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.dateTimePicker.BackColor = System.Drawing.SystemColors.ControlLightLight;
this.dateTimePicker.Font = new System.Drawing.Font("Segoe UI", 10F, System.Drawing.FontStyle.Bold);
this.dateTimePicker.Location = new System.Drawing.Point(79, 28);
this.dateTimePicker.Name = "dateTimePicker";
this.dateTimePicker.Size = new System.Drawing.Size(377, 37);
this.dateTimePicker.TabIndex = 0;
this.dateTimePicker.TabStop = false;
this.dateTimePicker.Text = "Friday, September 7, 2012";
this.dateTimePicker.ThemeName = "Material";
this.dateTimePicker.SizeChanged += new System.EventHandler(this.dateTimePicker_sizeChanged);
this.dateTimePicker.Value = new System.DateTime(2012, 9, 7, 0, 0, 0, 0);
((Telerik.WinControls.Primitives.FillPrimitive)(this.dateTimePicker.GetChildAt(0).GetChildAt(0))).SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
((Telerik.WinControls.UI.RadMaskedEditBoxElement)(this.dateTimePicker.GetChildAt(0).GetChildAt(2).GetChildAt(1))).Text = "Friday, September 7, 2012";
((Telerik.WinControls.UI.RadTextBoxItem)(this.dateTimePicker.GetChildAt(0).GetChildAt(2).GetChildAt(1).GetChildAt(0))).Alignment = System.Drawing.ContentAlignment.MiddleLeft;
Found out a solution, wherein instead of modifying the size of the calendar i can just modify its min and max size like this.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.radDateTimePicker1.SizeChanged += RadDateTimePicker1_SizeChanged;
}
private void RadDateTimePicker1_SizeChanged(object sender, EventArgs e)
{
int width = this.radDateTimePicker1.Width;
int height = 300;//calculate it according to your formula
Size desiredSize = new Size(width, height);
RadDateTimePickerCalendar cal = this.radDateTimePicker1.DateTimePickerElement.GetCurrentBehavior() as RadDateTimePickerCalendar;
// Control popup size
cal.DropDownMinSize = desiredSize;
cal.DropDownMaxSize = desiredSize;
}
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
#Hello guys, I have this problem with Forms in C#, I want to make a font viewer, actually I want to
make two Combobox that make change the "graphics" and display the same name of the font in its style
and in a specific size of the second Combobox. The main problem is that the style and the size don't
change. Any error could be cool for me to be told :) Here is my code.#
namespace CodeImage35
{
class Visualizer : Form
{
Label choose;
PictureBox picbox; //this is for the text
Graphics textm; //the text in PictureBox
ComboBox fontsm; //font of the text
ComboBox size; //size of the font
String fonts; //I used this for get the fonts,maybe never is used
public Visualizer() //Here is the UI
{
this.Width = 600;
this.Height = 300;
this.Text = "Visualizer of fonts";
choose = new Label();
choose.Size = new Size(100, 20);
choose.Location = new Point(20, 20);
choose.Text = "Choose Font:";
picbox = new PictureBox();
picbox.Size = new Size(300, 200);
picbox.Location = new Point(100,150);
fontsm = new ComboBox();
fontsm.Location = new Point(110, 20);
fontsm.Size = new Size(350, 20);
size = new ComboBox();
size.Location = new Point(480, 20);
size.Size = new Size(100, 20);
System.Drawing.Text.InstalledFontCollection fonts = new InstalledFontCollection(); //this
//is for the fonts
foreach ( FontFamily style in fonts.Families )//I add them in the combobox here
{
fontsm.Items.Add(style.Name);
}
for( int s = 1; s <= 50; size++ )
{
size.Items.Add(s); //I add here the sizes from 1 to 50
}
picbox.Paint += new PaintEventHandler(write_in_PictureBox);
Controls.Add(fontsm);
Controls.Add(choose);
Controls.Add(size);
Controls.Add(picbox);
}
void write_in_PictureBox(object sender, PaintEventArgs e)
{
String text2 = fontsm.Text;
textm = e.Graphics;
int x = 0;
Int32.TryParse(size.Text, out x); //I tried with this function to make string to int for
//the parameter of "DrawString
textm.DrawString( text2 , new Font( text2, x ), Brushes.Black, new Point(10, 10) );
}
}
static class MainClass
{
static void Main()
{
Visualizer screen1 = new Visualizer();
Application.Run(screen1);
}
}
}
To begin fix this compiler error:
for( int s = 1; s <= 50; size++ )
{
size.Items.Add(s); //I add here the sizes from 1 to 50
}
the correct variable to increment is s . Also you cold avoid to start from 1, a value really too small to render a font and appreciate the result. Starting from 6 or 8 should be fine. Apart from that...
In order to draw a string, based on the input of the two combobox, you have to handle the SelectedValueChanged event (or SelectedIndexChanged). Those events happen when the user change (select) a value in the combobox.
Instead, the PaintEventHandler happens when a control is being painted. See Paint event.
Following your example you can attach the SelectedValueChanged event to a method that draws the string like you did in write_in_PictureBox method .
//put this at the end of the constructor method.
//The event will be triggered on both combobox, allowing the user to see the result of it's selection whatever combobox is edited
fontsm.SelectedValueChanged += (sender, args) => DrawFont();
size.SelectedValueChanged += (sender, args) => DrawFont();
//that's just a method to handle the event
private void DrawFont()
{
using (var g = picbox.CreateGraphics()) //remember to always dispose a disposable resource
{
//clear the area before draw the new string
g.Clear(DefaultBackColor);
if (!Int32.TryParse(size.Text, out int x)) x = 12; //if no value is selected for size combobox, use 12 as default
string text = fontsm.Text;
g.DrawString(text, new Font(text, x), Brushes.Black, new Point(10, 10));
}
}
By the way... if you just want to show a font preview... it would be much easier for you to just put a label and change it's text and font property :) .
I'm using a FlowLayoutPanel to dynamically display an array of PictureBoxes and Labels. They're going in the order: PictureBox-Label-PictureBox-Label and so on. I need those labels to appear above each picture so they'll match, basically layring them ontop of picturebox margins. I tried using Controls.SetChildIndex(temp, 2); but it seem to just swap the postion of the picturebox. I also tried using temp.BringToFront(); but then all the pictureboxes being displayed at the top of the panel and all the labels are below (I need each label to match each picturebox above them). Here's the code:
public void RunMeta()
{
Label mostPickedLabel = new Label();
mostPickedLabel.Text = "Most picked heroes";
flowLayoutPanel1.Controls.Add(mostPickedLabel);
mostPickedLabel.Margin = new Padding(15, 0, 1000, 0);
mostPickedLabel.Font = new Font("Lucida Sans Unicode", 15);
mostPickedLabel.ForeColor = Color.DarkCyan;
mostPickedLabel.Size = new Size(200, 30);
foreach (var mostPickedHero in FetchDataFromDota2Site.MostUsedHeroesAndImages)
{
PictureBox temp = new PictureBox();
temp.ImageLocation = mostPickedHero.ImageSource;
temp.SizeMode = PictureBoxSizeMode.StretchImage;
temp.Left = temp.Width * flowLayoutPanel1.Controls.Count;
temp.Margin = new Padding(15, 30, 15, 30);
flowLayoutPanel1.Controls.Add(temp);
flowLayoutPanel1.AutoScroll = true;
Label heroName = new Label();
heroName.Text = mostPickedHero.MostPickedHeroName;
heroName.Font = new Font("Lucida Sans Unicode", 8);
heroName.ForeColor = Color.White;
flowLayoutPanel1.Controls.Add(heroName);
}
}
I am trying to include a calendar like custom user control that picks month and year. In the user control code I am setting two properties, Month and Year to current month and year.
public MonthPicker()
{
InitializeComponent();
m_MonthLabels = new Label[]
{
lblJanuary,
lblFebruary,
lblMarch,
lblApril,
lblMay,
lblJune,
lblJuly,
lblAugust,
lblSeptember,
lblOctober,
lblNovember,
lblDecember
};
m_NotSelected = new Font("Sans Serif", 8.25F, FontStyle.Regular);
m_Selected = new Font("Sans Serif", 8.25F, FontStyle.Bold);
Month = DateTime.Now.Month;
Year = DateTime.Now.Year;
lblYear.Text = Year.ToString();
groupBox2.Visible = false;
groupBox1.Height = 20;
CalendarIsDisplayed = false;
CalendarIsNotChanged = false;
SetMonthLabelSelected(Month);
}
However, when I include it in my Application form, the designer takes hard coded values for Month and Year. Consequently, when the month changes it still shows me older month.
// tsMonthPicker
//
this.tsMonthPicker.BackColor = System.Drawing.SystemColors.Control;
this.tsMonthPicker.CalendarIsDisplayed = false;
this.tsMonthPicker.CalendarIsNotChanged = false;
this.tsMonthPicker.Location = new System.Drawing.Point(104, 14);
this.tsMonthPicker.Margin = new System.Windows.Forms.Padding(0);
this.tsMonthPicker.Month = 1;
this.tsMonthPicker.Name = "tsMonthPicker";
this.tsMonthPicker.Size = new System.Drawing.Size(215, 20);
this.tsMonthPicker.TabIndex = 6;
this.tsMonthPicker.Value = new System.DateTime(2017, 1, 1, 0, 0, 0, 0);
this.tsMonthPicker.Year = 2017;
this.tsMonthPicker.Change += new Time_and_Billing_System.MonthPicker.MonthPickerChangeHandler(this.tsMonthPicker_Changed);
this.tsMonthPicker.Load += new System.EventHandler(this.tsMonthPicker_Load);
How do I change my code in user control so that the form designer file automatically takes -
this.tsMonthPicker.Month = System.DateTime.Now.Month;
this.tsMonthPicker.Year = System.DateTime.Now.Year;
Put these two lines in the Form_Load method, for the form in which this MonthPicker is embedded.
private void Form1_Load(object sender, EventArgs e)
{
this.tsMonthPicker.Month = System.DateTime.Now.Month;
this.tsMonthPicker.Year = System.DateTime.Now.Year;
}
In the Object Viewer, while the Form object is chosen, select the Events view (Lightning Bolt), find Load in the list, double-click that space, and this method will appear in the code.
You could use the DesignMode property for this. You don't state if you need a default value of Now each time the app is launched. If that is the case then you don't need the DesignMode.
public MonthPicker()
{
if (this.DesignMode)
{
//These values are only set when the control is created in design mode
this.tsMonthPicker.Month = System.DateTime.Now.Month;
this.tsMonthPicker.Year = System.DateTime.Now.Year;
}
}
I'm trying dynamically add panels within a panel dependent on the count of people in a list using the following code when the form loads:
private void Form1_Load(object sender, EventArgs e)
{
const int xConst = 2;
var people = new List<string>
{
"Person1",
"Person2",
"Person3",
"Person4",
};
var y = 2;
for (var x = 0; x < people.Count; x++)
{
var newpan = new MyPanel
{
BorderStyle = BorderStyle.None,
Height = 25,
Width = panel1.Width - 5,
Location = new Point(xConst, y)
};
var newlbl = new Label
{
BorderStyle = BorderStyle.None,
AutoSize = false,
Text = people[x],
Font = new Font("Segoe UI", 9.5F, FontStyle.Bold, GraphicsUnit.Point, ((byte)(0))),
Size = new Size(75,20),
Location = new Point(newpan.Location.X + 2, newpan.Location.Y + 2),
};
var newbtn = new Button
{
FlatStyle = FlatStyle.Flat,
FlatAppearance = { BorderSize = 0 },
UseVisualStyleBackColor = true,
Text = #"+",
Size = new Size(15,21),
Location = new Point(newpan.Width - 20,newpan.Location.Y - 1),
Font = new Font("Segoe UI", 9.0F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0)))
};
newpan.Controls.Add(newlbl);
newpan.Controls.Add(newbtn);
panel1.Controls.Add(newpan);
y += 27;
}
}
The problem is that if I specify the Location property in both the button and the label, only the first iteration of the labels and buttons show up for the Person1 iteration. But, if I leave the Location property out, they all show up. The problem with that is that I have a custom panel that overrides some stuff allowing me to put a customer border and color around the panels, and if I don't specify a location, the labels and buttons aren't positioned correctly on the panel, so it covers my border and looks sloppy.
Can someone help me figure out why this is happening? I've stepped through the program completely and watched all the values I can think of increment accordingly in the watch window. All the panels show up correctly, so I don't understand why their respective labels and buttons don't show up when I specify the location.
Looking at your code I notice that you are setting your newpan height to 25, and its position is offset by 27 with each iteration. You also are using
'Location = new Point(newpan.Location.X + 2, newpan.Location.Y + 2)
to set the location of your button and label within your newpan panel. newpan.Location is referenced in the coordinates of your panel1, your button and label's location is referenced in the coordinates of your newpan panel therefore after the first iteration of your For statement your label and buttons y location value is 29 which is greater than the height of your newpan panel making it not able to be seen, the next iteration after that will be y will be 56 and so forth. Each content control, in this case your panels will have its own coordinate system, the easiest fix would be to do something like this:
'Location = new Point( 2, 2) //for your label
'Location = new Point(newpan.Width - 20, - 1) //for your button
The other alternative is to do like jmcilhinney suggests and make an UserControl with your button and label already in position, you would then create individual instances of it and assign it to your panel1.
I try to create simple app with 2 columns using SpliterContainer and control panel with buttons. And I would like that on every screen it will look good. That's why I decided to use relative position of elements.
I read documentation and different forums, but I get something strange. Second column of splitter doesn't appear at all.
Please, can you help me find the reason of that problem?
private void Form1_Load(object sender, EventArgs e)
{
WindowState = FormWindowState.Maximized;
int screenWidth = Screen.PrimaryScreen.Bounds.Width;
int screenHeight = Screen.PrimaryScreen.Bounds.Height;
//set form size
this.Size = new Size(screenWidth, screenHeight);
//set button panel size
const double percentOfHeightPanel = 0.05;
int heightOfPanelButton = Convert.ToInt32(screenHeight * percentOfHeightPanel);
this.panel_button.Size = new System.Drawing.Size(screenWidth, heightOfPanelButton);
this.panel_button.Location = new Point(0, 0);
//set splitContainer size
int widthOfContainer = Convert.ToInt32(0.5 * screenWidth);
int heightOfContainers = Convert.ToInt32(screenHeight * (0.95));
splitContainer1.Panel1.MinimumSize = new Size(widthOfContainer, heightOfContainers);
splitContainer1.Panel2.MinimumSize = new Size(widthOfContainer, heightOfContainers);
splitContainer1.Location = new Point(0, heightOfPanelButton);
//this.splitContainer1.Panel2MinSize = screenWidth - widthOfContainer;
//set textBox size
this.textBox1.Multiline = true;
this.textBox1.Location = new Point(0, heightOfPanelButton);
this.textBox1.MinimumSize = new System.Drawing.Size(widthOfContainer, heightOfContainers);
this.textBox2.Multiline = true;
this.textBox2.Location = new Point(widthOfContainer, heightOfPanelButton);
this.textBox1.MinimumSize = new System.Drawing.Size(widthOfContainer, heightOfContainers);
}
If you want two have two splitter panels of the same size set
splitContainer1.SplitterDistance =
(splitContainer1.Width - splitContainer1.SplitterWidth) / 2;
Then set
splitContainer1.IsSplitterFixed = true;
You can set these two properties manually at design time. The user will then not be able to resize the panels and the panels will automatically resize to be of same size.
Consider using a TableLayoutPanel instead.
If further, the two sides should look the same, place your controls on a UserControl and place two instances of them into the two panels with a docked property set to Fill.