Form bigger than it supposed to be - c#

I'm just trying to create a very small form with an "X" button.
when I execute the whole thing, it's bigger than it supposed to be:
On the visual studio design editor:
But when running, this shows up:
The designer code:
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.BackColor = System.Drawing.Color.Black;
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.button1.ForeColor = System.Drawing.Color.Gray;
this.button1.Location = new System.Drawing.Point(6, 7);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(27, 26);
this.button1.TabIndex = 0;
this.button1.Text = "X";
this.button1.UseVisualStyleBackColor = false;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form3
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.ClientSize = new System.Drawing.Size(40, 40);
this.Controls.Add(this.button1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "Form3";
this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
this.ResumeLayout(false);
}
What have I done wrong?

You can see the below designer code as you were missing to set some of the properties for the button as well as the form.
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.BackColor = System.Drawing.Color.Black;
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.Font = new System.Drawing.Font("Arial", 12F,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
((byte)(0)));
this.button1.ForeColor = System.Drawing.Color.Gray;
this.button1.Location = new System.Drawing.Point(0, 0);
this.button1.Name = "button1";
this.button1.AutoSize = true; //you were missing this for the button
this.button1.Size = new System.Drawing.Size(27, 26);
this.button1.TabIndex = 0;
this.button1.Text = "X";
this.button1.UseVisualStyleBackColor = false;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoSize = true; //you were missing this for the form
this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;//also this
this.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.ClientSize = new System.Drawing.Size(40, 40);
this.Controls.Add(this.button1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
this.ResumeLayout(false);
}
Please check the designer code above and it works.

I think you should try to set the Size.Width and Size.Height of the form manually to whatever you want it to be.
Hope it helps!

Related

Font size of a single character within a Label/TextBox is not as expected

I am writing a Chinese GUI program using Visual Studio.
I have already tested this character on Visual Studio 2008 and 2017.
This problem happen in both version.
The character I was using is 戶 (\u6236).
The Font size become smaller when it is in the Form like Label, TextBox, Button text.
However, the character's size does not change in the Title Bar, Status Bar or Menu bar.
Can someone explain why this happen?
Cannot post image yet so I put down a link here.
As you can see, only this character become smaller.
partial class Form1
{
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.button1 = new System.Windows.Forms.Button();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.label1 = new System.Windows.Forms.Label();
this.listView1 = new System.Windows.Forms.ListView();
this.columnHeader1 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.textBox1 = new System.Windows.Forms.TextBox();
this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
this.toolTip2 = new System.Windows.Forms.ToolTip(this.components);
this.toolStrip1 = new System.Windows.Forms.ToolStrip();
this.toolStripLabel1 = new System.Windows.Forms.ToolStripLabel();
this.statusStrip1 = new System.Windows.Forms.StatusStrip();
this.toolStripStatusLabel1 = new System.Windows.Forms.ToolStripStatusLabel();
this.toolStrip1.SuspendLayout();
this.statusStrip1.SuspendLayout();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(7, 131);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "住戶";
this.button1.UseVisualStyleBackColor = true;
//
// checkBox1
//
this.checkBox1.AutoSize = true;
this.checkBox1.Location = new System.Drawing.Point(7, 160);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(47, 17);
this.checkBox1.TabIndex = 1;
this.checkBox1.Text = "住戶";
this.checkBox1.UseVisualStyleBackColor = true;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.5F);
this.label1.Location = new System.Drawing.Point(4, 180);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(28, 15);
this.label1.TabIndex = 2;
this.label1.Text = "住戶";
//
// listView1
//
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.columnHeader1});
this.listView1.Location = new System.Drawing.Point(7, 28);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(121, 97);
this.listView1.TabIndex = 3;
this.listView1.UseCompatibleStateImageBehavior = false;
this.listView1.View = System.Windows.Forms.View.Details;
//
// columnHeader1
//
this.columnHeader1.Text = "住戶";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(7, 198);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.TabIndex = 4;
this.textBox1.Text = "住戶";
//
// toolStrip1
//
this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripLabel1});
this.toolStrip1.Location = new System.Drawing.Point(0, 0);
this.toolStrip1.Name = "toolStrip1";
this.toolStrip1.Size = new System.Drawing.Size(800, 25);
this.toolStrip1.TabIndex = 5;
this.toolStrip1.Text = "toolStrip1";
//
// toolStripLabel1
//
this.toolStripLabel1.Name = "toolStripLabel1";
this.toolStripLabel1.Size = new System.Drawing.Size(32, 22);
this.toolStripLabel1.Text = "住戶";
//
// statusStrip1
//
this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripStatusLabel1});
this.statusStrip1.Location = new System.Drawing.Point(0, 428);
this.statusStrip1.Name = "statusStrip1";
this.statusStrip1.Size = new System.Drawing.Size(800, 22);
this.statusStrip1.TabIndex = 6;
this.statusStrip1.Text = "statusStrip1";
//
// toolStripStatusLabel1
//
this.toolStripStatusLabel1.Name = "toolStripStatusLabel1";
this.toolStripStatusLabel1.Size = new System.Drawing.Size(32, 17);
this.toolStripStatusLabel1.Text = "住戶";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.statusStrip1);
this.Controls.Add(this.toolStrip1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.listView1);
this.Controls.Add(this.label1);
this.Controls.Add(this.checkBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "住戶";
this.toolStrip1.ResumeLayout(false);
this.toolStrip1.PerformLayout();
this.statusStrip1.ResumeLayout(false);
this.statusStrip1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
private System.Windows.Forms.Button button1;
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.ColumnHeader columnHeader1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.ToolTip toolTip1;
private System.Windows.Forms.ToolTip toolTip2;
private System.Windows.Forms.ToolStrip toolStrip1;
private System.Windows.Forms.ToolStripLabel toolStripLabel1;
private System.Windows.Forms.StatusStrip statusStrip1;
private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel1;
}
I believe the issue you are having is to do with the auto scaling that windows performs to ensure correct scale on different screen sizes.
Have a read of Automatic scaling in Windows Forms
It maybe relevant to what your experiencing.

Place image on the left of a button, and keep the image fit the button

I want an image on the left of the text, but I also want the image to fit the button (not bigger than the button or too small). There seems to be two options:BackgroundImage and Image property.
If I use BackgroundImage, I can make the image fit the button size, but the image always seems to be placed on the centre. If I use Image, I can place the image on the left of the text, but then the image does not fit the button.
Is there any way to get both?
As requested, I attached the source code. I just used the GUI designer and the code was generated by it, so it contains lots of lines. In short, I made the top two buttons use Background and the bottom two buttons use Image.
//
// button2
//
this.button2.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.button2.Image = global::delete_menu.Properties.Resources.folder;
this.button2.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.button2.Location = new System.Drawing.Point(63, 158);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(118, 25);
this.button2.TabIndex = 0;
this.button2.Text = "image";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button1_Click);
//
// button1
//
this.button1.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("button1.BackgroundImage")));
this.button1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.button1.ContextMenuStrip = this.contextMenuStrip1;
this.button1.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.button1.Location = new System.Drawing.Point(204, 63);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(245, 56);
this.button1.TabIndex = 0;
this.button1.Text = "background";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button3
//
this.button3.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.button3.Image = global::delete_menu.Properties.Resources.folder;
this.button3.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.button3.Location = new System.Drawing.Point(204, 158);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(245, 56);
this.button3.TabIndex = 0;
this.button3.Text = "image";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button1_Click);
//
// button4
//
this.button4.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("button4.BackgroundImage")));
this.button4.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.button4.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.button4.Location = new System.Drawing.Point(63, 63);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(118, 25);
this.button4.TabIndex = 0;
this.button4.Text = "background";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(505, 278);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button4);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.contextMenuStrip1.ResumeLayout(false);
this.ResumeLayout(false);

Error when trying to Initialize WebBrowser

Can anyone have a look at my code, I don't quite follow what is the error about
private void InitializeComponent()
{
this.WebBrowserHost = new System.Windows.Forms.Integration.ElementHost();
this.SuspendLayout();
//
// WebBrowserHost
//
this.WebBrowserHost.Dock = System.Windows.Forms.DockStyle.Fill;
this.WebBrowserHost.Location = new System.Drawing.Point(0, 0);
this.WebBrowserHost.MinimumSize = new System.Drawing.Size(20, 20);
this.WebBrowserHost.Name = "WebBrowserHost";
this.WebBrowserHost.Size = new System.Drawing.Size(284, 262);
this.WebBrowserHost.TabIndex = 0;
//
// AuthorizationWindow
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.WebBrowserHost);
this.Name = "AuthorizationWindow";
this.Text = "AuthorizationWindow";
this.ResumeLayout(false);
}
In my form I just have a WebBrowser control and I am having the error:
Cannot implicitly convert type
'System.Windows.Forms.Integration.ElementHost' to
'System.Windows.Forms.WebBrowser'
After Initialize I need ElementHost.Child like this
public AuthorizationWindow()
{
// This call is required by the designer.
InitializeComponent();
WebBrowser = new System.Windows.Controls.WebBrowser();
WebBrowserHost.Child = WebBrowser;
WebBrowser.Navigating += WebBrowser_Navigating;
WebBrowser.LoadCompleted += WebBrowser_LoadCompleted;
_authorization = new AuthorizationState();
}
Did you try changing the line
this.WebBrowserHost = new System.Windows.Forms.Integration.ElementHost();
to
this.WebBrowserHost = new System.Windows.Forms.WebBrowser();
?

can't insert webbrowser inside panel in c#winapp

I am unable to add webbrowser control to panel in winapp,getting this error"Unable to get the window handle for the 'WebBrowser' control. Windowless ActiveX controls are not supported."i also tried to do it using STAthread like
Thread uy = new Thread(me_p);
uy.SetApartmentState(ApartmentState.STA);
uy.Start();
private void me_p(object obj)
{
//throw new NotImplementedException();
this.p_bottom.Controls.Add(this.webBrowser2);
}
It works fine when done this way
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.webBrowser1 = new System.Windows.Forms.WebBrowser();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.Controls.Add(this.webBrowser1);
this.panel1.Location = new System.Drawing.Point(47, 149);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(200, 100);
this.panel1.TabIndex = 0;
//
// webBrowser1
//
this.webBrowser1.Dock = System.Windows.Forms.DockStyle.Fill;
this.webBrowser1.Location = new System.Drawing.Point(0, 0);
this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20);
this.webBrowser1.Name = "webBrowser1";
this.webBrowser1.Size = new System.Drawing.Size(200, 100);
this.webBrowser1.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 261);
this.Controls.Add(this.panel1);
this.Name = "Form1";
this.Text = "Form1";
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
}
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.WebBrowser webBrowser1;
You can have a browser pre initialized and later use it in your threads.

How to compile and add the form designed by user to a project

I am going to let users create Forms during RunTime and add them to a project. I have done the designing and the UI of the form with the help of an open source Form Designer.
Here is the image of the Form Designer:
Lets assume I have the Form1.cs and Form1.cs[Designer] files which are enough for a WinForm. But how do I compile it to a DLL or an EXE and add it to the project? Any ideas? Any clues?
Thanks!!
EDIT
It creates this code.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.CheckBox checkBox1;
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(71, 49);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseCompatibleTextRendering = true;
this.button1.UseVisualStyleBackColor = true;
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(71, 94);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.TabIndex = 1;
//
// checkBox1
//
this.checkBox1.Location = new System.Drawing.Point(38, 184);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(104, 24);
this.checkBox1.TabIndex = 2;
this.checkBox1.Text = "checkBox1";
this.checkBox1.UseCompatibleTextRendering = true;
this.checkBox1.UseVisualStyleBackColor = true;
//
// form1
//
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.checkBox1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Name = "form1";
this.ResumeLayout(false);
this.PerformLayout();
}
}
I will do some extra coding to the rest of the code myself. but I dont know how to compile it and add it to the main exe?
The clue is System.CodeDom.Compiler
i think Form1.cs will contain no code (other then InitializeComponents call in constructor). so if you merge it with the designer code with simple string operations your job will be easier.

Categories

Resources