probably another noob question but I am trying to dynamically control the height of some group boxes as the form is resizing. However the listboxes inside the groupboxes (which are anchored to top and bottom) sometimes decide they want to shrink when the form is reduced in height. I have no idea how to fix, this please help!
private void Fluxx_Resize(object sender, EventArgs e)
{
groupBox1.Height = Height / 2 - 40;
groupBox2.Height = Height / 2 - 40;
groupBox3.Height = Height / 2 - 40;
groupBox4.Height = Height / 2 - 40;
groupBox3.Top = Height - groupBox3.Height - 51;
groupBox4.Top = Height - groupBox4.Height - 51;
}
This seems to work perfectly when resizing the form vertically, but seems to randomly decide the recude the heights of the listboxes contained inside the groupboxes to zero at somepoint. I can then expand the form vertically again and the listboxes still react, but they always start from zero height and wont stay anchored to the top and bottom of the group boxes.
Cheers,
Tim
OK
So if I remove the ability for the controls to resize their width then the height never has an issue. here is the code for one of the blocks for the width resizing. I have no idea why this clashes.
groupBox1.Width = Width / 3 - 20;
listBoxPlayer1.Left = 6;
listBoxPlayer1.Width = groupBox1.Width / 2 - GAP / 3;
listBoxKeepers1.Width = listBoxPlayer1.Width;
labelK1.Left = groupBox1.Width / 2 + SMALL_GAP;
listBoxKeepers1.Left = labelK1.Left;
Calculation of height and width couldn't support every condition,but you could use dock ,if you divide your form in to sections that have container ,you could set every container :
Dock = DockStyle.Fill
so your design will be compatible with each size of form.
Setting all of the listboxes inside of my groupboxes IntegralHeight to false was able to fix my issue. Cheers everyone!
Related
Using Forms9Patch in Xamarin Forms I am able to have the font dynamically resize when running on UWP and changing the size of the window horizontally. It resizes perfectly.
However, I have a few issues that I'm not figuring out ...
When I shrink the window vertically, the text is not resized. I'm doing the
Lines = 1;
AutoFit = Forms9Patch.AutoFit.Width;
LineBreakMode = LineBreakMode.NoWrap;
I went back here - https://baskren.github.io/Forms9Patch/guides/Label.html and re-read it. It says to impose bounds autofitting to achieve this. I've tried and can't get it to work. What's the proper syntax to get this to work?
The starting font size for the label. I'm hard coding that right now. Is there a way to dynamically size it on startup?
I have a work around, but is there a built in way that Forms9Patch deals with screen scaling?
Alternatively, you could try to use something like this, where you add your private field label inside a ContentView, and then add a SizeChanged event to it
ContentView contentView = new ContentView
{
Content = label
};
contentView.SizeChanged += OnContentViewSizeChanged;
and in the event
void OnContentViewSizeChanged(object sender, EventArgs args)
{
string text = "Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams. Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.";
View view = (View)sender;
// Line height is 1.2 if it's iOS or Android, but 1.3 for UWP
double lineHeight = 1.3;
double charWidth = 0.5;
text = String.Format(text, lineHeight, charWidth, view.Width, view.Height);
int charCount = text.Length;
int fontSize = (int)Math.Sqrt(view.Width * view.Height / (charCount * lineHeight * charWidth));
int lineCount = (int)(view.Height / (lineHeight * fontSize));
int charsPerLine = (int)(view.Width / (charWidth * fontSize));
label.Text = text;
label.FontSize = fontSize;
}
Here's some official documentation that recommends it
First question here so if I can improve something in anyway please let me know!
I am currently making a "multi-form" application.
It currently consists out of a launcher bar with various buttons and the launcher bar has a width of 150 pixels (This one is fine).
When the user presses a button another panel will open 10 pixels next to the the first panel with a width of 75. (I wanted to add the current buttons "subcategories" here)
But when calling the second form it keeps setting itself to 100 pixels (Well I think it is 100 pixels since it seems about 2/3 of the first panel)
private void button_click(object sender, EventArgs e)
{
if (activated == 0)
{
var new_y = new form2();
new_y.AutoSize = false;
new_y.Width = 75;
new_y.Height = this.Height;
new_y.Show();
activated = 1;
}
}
I stripped it of some additional code (positioning stuff) so if that could cause any problems please let me know.
But my question : How do I prevent the form setting itself to 100 pixels in width and make it the 75 pixels width I want it to be?
Thanks in advance!
Ps . FormBorderStyle is set to none
Try setting the MinimumSize property:
new_y.Width = 75;
new_y.Height = this.Height;
new_y.MinimumSize = new Size(75, this.Height);
I have a label with text inside i can change the label size or the label font size each time and check many times but maybe there is a way to calculate it:
label18.Text = "מכם מזג האוויר איננו פעיל כרגע";
This is how i see the text now:
The text in red is in hebrew this is the text i want to change it's size and also to put it in the middle according to the picturebox1 top not on the left like it is now.
And i did a black circle just to show what i mean by " the distance from the top of pictureBox1 and almost the top of form1 ".
I mean this gray area from the above the pictureBox1 and the form1 white area on the top only this gray area i want to make the text in this height and in the middle.
How can i calculate this two values ?
I tried this but it's not in the exact middle:
SizeF size = label18.CreateGraphics().MeasureString(label18.Text, label18.Font);
label18.Left = (pictureBox1.Width / 2) - (((int)size.Width) / 2) + pictureBox1.Left;
label18.Top = pictureBox1.Top - 20;
You don't need graphics or to measure anything. Just set in designer text align = middlecenter and autosize = true
label18.Location = new Point(pictureBox1.Location.X + (pictureBox1.Width / 2 - label18.Width / 2,
pictureBox1.Location.Y - label18.Height);
To center a label you need it get it actual size, then to center it using another control use some simple math to get the coordinate for the control (see below Example 1). I don't know what control the grey bar is but you could center in that by using the size.Width property and doing the same type of calculation.
If you want to fill the grey bar I have added Example 2.
Example 1:
private void CenterLabel()
{
//get the size of the text (you could do this before hand if needed)
SizeF size = label18.CreateGraphics().MeasureString(label18.Text, label18.Font);
//center over picture box control and slightly above
label18.Left = (pictureBox1.Width / 2) - (((int)size.Width) / 2) + pictureBox1.Left;
label18.Top = pictureBox1.Top - 20;
}
Example 2
private void CenterLabel()
{
int fontHeightPixels = (int)(greyBar.Height * .85);
Font font = new System.Drawing.Font("Arial", fontHeightPixels, FontStyle.Regular, GraphicsUnit.Pixel);
string text = "I am centered";
//get the size of the text (you could do this before hand if needed)
SizeF size = label18.CreateGraphics().MeasureString(text, font);
label18.Font = font;
label18.Text = text;
//center over picture box control and slightly above
label18.Left = (pictureBox1.Width / 2) - (((int)size.Width) / 2) + pictureBox1.Left;
label18.Top = (greyBar.Height / 2) - (((int)size.Height) / 2) + greyBar.Top;
}
This is relatively simple with Windows forms:
Dock your label to the top of the form by setting the appropriate property in the Forms designer. The property you want to set is Dock and it should be set to Top.
Change the label's AutoSize property to false.
Change the label's height as desired.
Change the label's TextAlign property to MiddleCentre.
That should do it.
There's more then one way to achieve this goal.
I would suggest the following:
First calculate the width of the picturebox (picturebox.Width)
Find the coordinates on the form where the picturebox resides (picturebox.Location) property of the picturebox)
Then you change the location of your label control --> to Label.Location.X = (picturebox.Width /2) and Label.Location.Y = picturebox.Location.Y ==> now you have the label correctly placed .
Next Set the Height of the Label Control to the Top(distance between the edge of the form and picturebox) value of the Picturebox.
No visual studion from where i am typing so cannot do full code example.
You're done.
My form1 size is 800,600
Then i have two panels in the form1 designer:
Panel1 is at location: 0,24 size: 200,437
Panel2 is at location: 584,24 size: 200,437
The result is two panels at each side of the form.
Now i did in my program when you put the mouse somewhere in the form1 area its showing a pictureBox i create in the form1 constructor:
pb = new AnimatedPictureBox.AnimatedPictureBoxs();
pb.Visible = false;
pb.Size = new Size(500, 350);
pb.Location = new Point((ClientSize.Width - pb.Width) / 2,
(ClientSize.Height - pb.Height) / 2);
The problem is that the new pictureBox variable pb is not in the size that will fill all the area between the two panels.
I want the size of the pictureBox to fill almost all the space between the two panels Width and Height maybe to leave some space like 5 spaces each side so there will be a border.
How can i calculate what the pictureBox size should be ?
EDIT**
This is an image where the program is working regular. On each panel on the left and right i added 4 pictureBoxes.
When i move my mouse cursor inside one of the pictureBoxes area its showing its content in a larger pictureBox in the middle.
And this is how it looks like when i put the mouse cursor in one of the pictureBoxes area the pictureBox in the middle is not big enough its Width and Height dosent make the big pictureBox to be excatly between the two panels. The big pictureBox not high and not wide enough.
if you want to make your layout stable even after resizing you should use Dock property for your panels and set Anchor for your picture box. Like that:
panel1.Dock = DockStyle.Left;
panel2.Dock = DockStyle.Right;
pb.Anchor = AnchorStyles.Left | AnchorStyles.Right;
And in general to place it in the center you can use something like that:
var width = this.Width - panel1.Width - panel1.Margin.Horizontal
- panel2.Width - panel2.Margin.Horizontal;
pb.Size = new Size(width, 300); // put your needed height here
pb.Top = this.Height/2 - pb.Height/ 2;
pb.Left = panel2.Left + panel2.Width + panel2.Margin.Right;
Adding some gui modifications and I want to have a button which is 10pixels away from the forms left and right border. With this code the right border of the button is around 20-30 pixel outside the form window. Why is that? How can I position my button to be exactly 10pixels away from the form borders ?
int margin = 10;
meny1 = new Button();
meny1.Top = 50;
meny1.Left = margin;
meny1.Size = new Size(this.Width - (2*margin), 30);
You should calculate with this.ClientWidth although I would expect the difference to be just the BorderSize, not 20 pixels.
Use the Form.ClientWidth. This code worked for me.
button1.Left = 10;
button1.Width = this.ClientRectangle.Width - 20;