In my project a have an toolStrip element which behaves strange after modification in Resources.resx file. Doesn't matter what I do: add a new bitmap, delete old bitmap, change Display style of dropdown buttons... Every time I got error messages and no toolStrip at the form.
In my Form.Designer.cs happens next:
Before modification code looks like:
//
// toolStrip1
//
this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripDropDownButton1,
this.toolStripSeparator4,
this.toolStripDropDownButton2});
this.toolStrip1.Location = new System.Drawing.Point(0, 0);
this.toolStrip1.Name = "toolStrip1";
this.toolStrip1.Size = new System.Drawing.Size(1584, 27);
this.toolStrip1.TabIndex = 347;
this.toolStrip1.Text = "toolStrip1";
After:
//
// toolStrip1
//
this.toolStrip1.Location = new System.Drawing.Point(0, 0);
this.toolStrip1.Name = "toolStrip1";
this.toolStrip1.Size = new System.Drawing.Size(1584, 27);
this.toolStrip1.TabIndex = 347;
this.toolStrip1.Text = "toolStrip1";
My DropDownButton before modification:
//
// toolStripDropDownButton1
//
this.toolStripDropDownButton1.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.ImageAndText;
this.toolStripDropDownButton1.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.openFileToolStripMenuItem,
this.saveCalculationsFileToolStripMenuItem,
this.saveRawCalculationsFileToolStripMenuItem});
this.toolStripDropDownButton1.Image = global::Project_Tribo_v._0._2.Properties.Resources.folder_document;
this.toolStripDropDownButton1.ImageTransparentColor = System.Drawing.Color.Magenta;
this.toolStripDropDownButton1.Name = "toolStripDropDownButton1";
this.toolStripDropDownButton1.Size = new System.Drawing.Size(61, 24);
this.toolStripDropDownButton1.Text = "File";
And after:
//
// toolStripDropDownButton1
//
this.toolStripDropDownButton1.Image = global::Project_Tribo_v._0._2.Properties.Resources.folder_document;
this.toolStripDropDownButton1.ImageTransparentColor = System.Drawing.Color.Magenta;
this.toolStripDropDownButton1.Name = "toolStripDropDownButton1";
this.toolStripDropDownButton1.Size = new System.Drawing.Size(61, 24);
this.toolStripDropDownButton1.Text = "File";
It's not a big problem, while I have small number of toolStrip elements, but stil...
Any ideas how to avoid or repair this?
Related
I have a basic combobox in a form. Compared to other controls(Button,label, etc) the height of the combobox doesn't change when the resolution is changed.
public partial class Form1 : Form
{
string result;
string fontInformation;
private bool scaleFactorKnown = false;
private SizeF scaleFactor;
public Form1()
{
SizeChanged += Form1_SizeChanged;
InitializeComponent();
label1.Location = new Point(12, 36);
label1.Size = new Size(100, 21);
label1.Scale(scaleFactor);
//
// textBox1
//
textBox1.Location = new Point(133, 33);
textBox1.Size = new Size(100, 21);
textBox1.Scale(scaleFactor);
//
// comboBox1
//
comboBox1.Location = new Point(250, 33);
comboBox1.Size = new Size(100, 21);
comboBox1.Scale(scaleFactor);
// button1
//
button1.Location = new Point(365, 32);
button1.Size = new Size(100, 21);
button1.Scale(scaleFactor);
//
// radioButton1
//
radioButton1.Location = new Point(480, 32);
radioButton1.Size = new Size(100, 21);
radioButton1.Scale(scaleFactor);
//
// checkBox1
//
checkBox1.Location = new Point(586, 33);
checkBox1.Size = new Size(100, 21);
checkBox1.Scale(scaleFactor);
//
// textBox2
//
textBox2.Location = new Point(26, 102);
textBox2.Size = new Size(660, 250);
textBox2.Scale(scaleFactor);
}
private void Form1_SizeChanged(object sender, EventArgs e)
{
if (!scaleFactorKnown)
{
scaleFactor = AutoScaleFactor;
scaleFactorKnown = true;
}
Size controlSize = new Size((int)(comboBox1.Width * scaleFactor.Width),
(int)(comboBox1.Height * scaleFactor.Height)); //use for sizing
//set bounds
comboBox1.Bounds = new Rectangle(comboBox1.Location, controlSize);
}
}
I have tried the method Scale() to scale all other controls, it works for other controls except for combobox. I also tried manually changing the bound but it didn't work. I also tried change the anchor and dock as well.
Expected result: Combobox height(At 150%)=42
Actual result: Combobox
height(At 150%)=28
Would appreciate any help on how to fix this issue.
You have to set the IntegralHeight property of the ComboBox to false:
comboBox1.Location = new Point(250, 33);
comboBox1.Size = new Size(100, 21);
comboBox1.Scale(scaleFactor);
comboBox1.IntegralHeight = false;
Hi I have a windows form in my application with a tab control inside it. I have noticed that when I restore the application from the taskbar after minimizing it, the tabpage shrinks to half it's normal size and all controls inside move downwards. I have the tabpage docked to fill inside a TablePanelLayout. The tabs stay in place though. This does not happen upon resizing the form. Please help.
This is the relevant code from the designer.cs file
//
// tabControl1
//
this.tabControl1.Appearance = System.Windows.Forms.TabAppearance.FlatButtons;
this.tabControl1.Controls.Add(this.tabPage1);
this.tabControl1.Controls.Add(this.tabPage2);
this.tabControl1.Controls.Add(this.tabPage3);
this.tabControl1.Controls.Add(this.lookupTab);
this.tabControl1.Controls.Add(this.tabPage4);
this.tabControl1.Dock = System.Windows.Forms.DockStyle.Fill;
this.tabControl1.ItemSize = new System.Drawing.Size(71, 35);
this.tabControl1.Location = new System.Drawing.Point(3, 110);
this.tabControl1.Multiline = true;
this.tabControl1.Name = "tabControl1";
this.tabControl1.SelectedIndex = 0;
this.tabControl1.Size = new System.Drawing.Size(1453, 601);
this.tabControl1.TabIndex = 0;
this.tabControl1.SelectedIndexChanged += new System.EventHandler(this.tabControl1_SelectedIndexChanged);
//
// tabPage1
//
this.tabPage1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.tabPage1.Controls.Add(this.panel2);
this.tabPage1.Location = new System.Drawing.Point(4, 39);
this.tabPage1.Name = "tabPage1";
this.tabPage1.Padding = new System.Windows.Forms.Padding(3);
this.tabPage1.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.tabPage1.Size = new System.Drawing.Size(1445, 558);
this.tabPage1.TabIndex = 0;
this.tabPage1.Text = "Settings";
this.tabPage1.UseVisualStyleBackColor = true;
//
// tabPage2
//
this.tabPage2.Controls.Add(this.label5);
this.tabPage2.Controls.Add(this.label4);
this.tabPage2.Controls.Add(this.label2);
this.tabPage2.Location = new System.Drawing.Point(4, 39);
this.tabPage2.Name = "tabPage2";
this.tabPage2.Padding = new System.Windows.Forms.Padding(3);
this.tabPage2.Size = new System.Drawing.Size(1445, 558);
this.tabPage2.TabIndex = 1;
this.tabPage2.Text = "Calibration";
this.tabPage2.UseVisualStyleBackColor = true;
I found what was wrong. Posting it here in case someone else has the same problem.
I set this.tabControl1.Multiline = true; to false and the problem disappeared.
Hi I am looking to print a StackPanel that contains a listbox which can contain an infinite number of items and therefore needs to print over multiple pages. I found this code online and it works fine.
public static FixedDocument GetFixedDocument(FrameworkElement toPrint, PrintDialog printDialog)
{
if (printDialog == null)
{
printDialog = new PrintDialog();
}
var capabilities = printDialog.PrintQueue.GetPrintCapabilities(printDialog.PrintTicket);
var pageSize = new Size(printDialog.PrintableAreaWidth, printDialog.PrintableAreaHeight);
var visibleSize = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);
var fixedDoc = new FixedDocument();
//If the toPrint visual is not displayed on screen we neeed to measure and arrange it
toPrint.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
toPrint.Arrange(new Rect(new Point(0, 0), toPrint.DesiredSize));
//
var size = toPrint.DesiredSize;
//Will assume for simplicity the control fits horizontally on the page
double yOffset = 0;
while (yOffset < size.Height)
{
var vb = new VisualBrush(toPrint)
{
Stretch = Stretch.None,
AlignmentX = AlignmentX.Left,
AlignmentY = AlignmentY.Top,
ViewboxUnits = BrushMappingMode.Absolute,
TileMode = TileMode.None,
Viewbox = new Rect(0, yOffset, visibleSize.Width, visibleSize.Height)
};
var pageContent = new PageContent();
var page = new FixedPage();
((IAddChild)pageContent).AddChild(page);
fixedDoc.Pages.Add(pageContent);
page.Width = pageSize.Width;
page.Height = pageSize.Height;
var canvas = new Canvas();
FixedPage.SetLeft(canvas, capabilities.PageImageableArea.OriginWidth);
FixedPage.SetTop(canvas, capabilities.PageImageableArea.OriginHeight);
canvas.Width = visibleSize.Width;
canvas.Height = visibleSize.Height;
canvas.Background = vb;
page.Children.Add(canvas);
yOffset += visibleSize.Height;
}
return fixedDoc;
}
However this causes certain items of a listbox to be cut off at the bottom of a page and continued on the next page (as shown below). Is it possible to modify this code in any way to determine the size of the page and if the current listboxitem does not fit onto this page that it starts on the next page? Quite new to all this so any help would be greatly appreciated.
I had a similiar task once and came up with this code, which uses a 'dummy' renderer to determine the height of the element up front and then either adds it to the current page or creates a new one. For sure, that's not a very beautiful solution, but it did the job at the time. Maybe you can take sth. away from it.
Size A4Size = new Size(793.92, 1122.24);
Size InfiniteSize = new Size(double.PositiveInfinity, double.PositiveInfinity);
var pages = new List<FrameworkElement>();
int pageNumber = 0;
var printDate = DateTime.Now;
var size = A4Size;
var currentPage = new Report(size, printDate);
currentPage.Render();
var listElements = new Queue<ElementsToPrint>(...);
var dummyRenderer = new Viewbox();
dummyRenderer.Child = currentPage;
dummyRenderer.Measure(InfiniteSize);
dummyRenderer.Arrange(new Rect(dummyRenderer.DesiredSize));
dummyRenderer.UpdateLayout();
var template = (DataTemplate)View.FindResource("ItemTemplate");
dummyRenderer.Child = null;
var availableHeight = currentPage.View.ActualHeight;
while (listElements.Count > 0)
{
var elementToRender = listElements.Dequeue();
dummyRenderer.Child = new ListViewItem()
{
Content = elementToRender,
ContentTemplate = template,
Foreground = Brushes.Black
};
dummyRenderer.Measure(InfiniteSize);
dummyRenderer.Arrange(new Rect(dummyRenderer.DesiredSize));
dummyRenderer.UpdateLayout();
var renderedItem = (ListViewItem)dummyRenderer.Child;
dummyRenderer.Child = null;
var willItFit = availableHeight > renderedItem.ActualHeight;
if (willItFit)
{
currentPage.DataListView.Items.Add(renderedItem);
availableHeight -= renderedItem.ActualHeight;
}
else
{
dummyRenderer.Child = currentPage;
dummyRenderer.Measure(InfiniteSize);
dummyRenderer.Arrange(new Rect(dummyRenderer.DesiredSize));
dummyRenderer.UpdateLayout();
dummyRenderer.Child = null;
pages.Add(currentPage);
// Set up a new Page
pageNumber++;
currentPage = new DiaryReport(size,pageNumber,printDate,anonymous);
dummyRenderer.Child = currentPage;
dummyRenderer.Measure(InfiniteSize);
dummyRenderer.Arrange(new Rect(dummyRenderer.DesiredSize));
dummyRenderer.UpdateLayout();
dummyRenderer.Child = null;
availableHeight = currentPage.DataListView.ActualHeight;
currentPage.DataListView.Items.Add(renderedItem);
availableHeight -= renderedItem.ActualHeight;
}
I have a program which is some kind of tests. In this test, I have function AddQuestion that adds one panel with question. To place these panels one by one, I have a variable loc that saves location of next panel. First two Panel's with questions adds correct, but next ones are located wrong(far away at the bottom). What can it be?
public void AddQuestion(int number, Question quest)
{
Panel p = new Panel();
p.Name = "panel" + (number);
p.Size = new Size(550, 400);
p.Location = new Point(40, loc);
p.BackColor = System.Drawing.Color.Bisque;
p.AutoScroll = true;
Panel pict_block = new Panel();
pict_block.Size = new Size(480, 200);
pict_block.Location = new Point(10, 10);
PictureBox pict = new PictureBox();
pict.Image = quest.image;
pict.Size = new Size(240, 180);
pict.SizeMode = PictureBoxSizeMode.StretchImage;
pict.Location = new Point(130, 1);
pict_block.Controls.Add(pict);
p.Controls.Add(pict_block);
Label number_text = new Label(); //номер питання
number_text.Text = "Питання № " + number ;
number_text.Font = new Font("Aria", 8, FontStyle.Bold);
number_text.AutoSize = false;
number_text.Location = new Point(400, 210);
p.Controls.Add(number_text);
Label q_text = new Label(); // текст питання
q_text.Text = quest.question_text;
q_text.Font = new Font("Aria", 9, FontStyle.Bold);
q_text.AutoSize = false;
q_text.Size = new Size(400, 50);
q_text.Location = new Point(5, 220);
p.Controls.Add(q_text);
int iter = q_text.Location.Y + 60;
if (CheckIfMuliple(number))
{
foreach (string key in quest.answers.Keys)
{
CheckBox rb = new CheckBox();
rb.Text = key;
rb.AutoSize = true;
rb.Size = new Size(300, 25);
rb.Location = new Point(q_text.Location.X + 15, iter);
iter += 30;
p.Controls.Add(rb);
}
}
else
{
foreach (string key in quest.answers.Keys)
{
RadioButton rb = new RadioButton();
rb.Text = key;
rb.Size = new Size(300, 25);
rb.AutoSize = true;
rb.Location = new Point(q_text.Location.X + 10, iter);
iter += 30;
p.Controls.Add(rb);
}
}
questions_panel.Controls.Add(p);
loc += 450;
}
Good location:
Bad Location:
Also I noticed that when I add some panels, then scrool at the middle of the form and add new question, it's not located at the bottom, but somewhere at the center. From next screenshot, 6 question and then 15 question:
p.Location = new Point(40, loc);
This will not work correctly when the outer panel is scrolled. You have to offset it by the that panel's scroll position. Fix:
p.Location = new Point(40 + questions_panel.AutoScrollPosition.X,
loc + questions_panel.AutoScrollPosition.Y);
loc += 450;
This will not work correctly when your program runs on a machine with a video adapter that runs with a different dots-per-inch setting. Pretty common these days, modern versions of Windows make it very easy to change. The panel will automatically be rescaled to match the DPI setting. Fix:
loc += p.Height + 50;
Getting this right manually is very difficult.
I suggest that you change questions_panel to a TableLayoutPanel. It takes care of positioning new controls automatically for you.
TableLayoutPanel
Represents a panel that dynamically lays out its contents in a grid
composed of rows and columns.
I was able to duplicate your problem, and was able to fix it by always offsetting the top of the new Panel based on the location of the last panel. I changed your code:
questions_panel.Controls.Add(p);
loc += 450;
to:
if (questions_panel.Controls.Count > 0)
{
//Location of Top of last panel added then offset vertically by 450
p.Location = new Point(p.Location.X, questions_panel.Controls[questions_panel.Controls.Count-1].Location.Y +450);
questions_panel.Controls.Add(p);
}
else
questions_panel.Controls.Add(p);
private void newThumbNail(int docType, string fileName)
{
thmbNail[thmbNailCnt] = new GroupBox();
thmbNail[thmbNailCnt].Parent = panel1;
thmbNail[thmbNailCnt].Visible = true;
thmbNail[thmbNailCnt].Location = new Point(2, 5);
thmbNail[thmbNailCnt].Size = new Size(222, 50);
picBox[thmbNailCnt] = new PictureBox();
picBox[thmbNailCnt].Parent = thmbNail[thmbNailCnt];
picBox[thmbNailCnt].Visible = true;
picBox[thmbNailCnt].Location = new Point(6, 13);
picBox[thmbNailCnt].Size = new Size(31, 31);
//picBox[thmbNailCnt].Image = new Bitmap("images/excel.png");
texBox[thmbNailCnt] = new TextBox();
texBox[thmbNailCnt].Parent = thmbNail[thmbNailCnt];
texBox[thmbNailCnt].Visible = true;
texBox[thmbNailCnt].Location = new Point(53, 24);
texBox[thmbNailCnt].Size = new Size(163, 20);
texBox[thmbNailCnt].Text = fileName;
texBox[thmbNailCnt].Enabled = false;
Controls.Add(thmbNail[thmbNailCnt]);
Controls.Add(picBox[thmbNailCnt]);
Controls.Add(texBox[thmbNailCnt]);
}
this is a function that dynamically adds a groupBox with some controls int it inside a panel. Unfortunately it does not appears inside the panel. The panel was created before hand using the c# design tools. It is placed directly on top of the windows form at 15,52 having a size of 279,489. Help please.
It seems that you are adding these controls to the form controls collection.
Instead you should use the panel controls collection like:
panel1.Controls.Add(thmbNail[thmbNailCnt]);
panel1.Controls.Add(picBox[thmbNailCnt]);
panel1.Controls.Add(texBox[thmbNailCnt]);
Try Panel.Controls.Add(thmbNail[thmbNailCnt])
Also a tip to make your code faster and easier to read:
// Not modified to use Panel.Controls.Add()
GroupBox box = new GroouBox();
thmbNail[thmbNailCnt] = box;
box.Parent = panel1;
box.Visible = true;
box.Location = new Point(2, 5);
box.Size = new Size(222, 50);