Error when trying to Initialize WebBrowser - c#

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();
?

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.

Cannot set tabPage image to a tabControl C#

I have a ImageList, with one item, in the TabControl:
private System.Windows.Forms.ImageList tabControlMain_ilMain;
this.TabControlMain.ImageList = this.tabControlMain_ilMain;
this.tabControlMain_ilMain.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("tabControlMain_ilMain.ImageStream")));
this.tabControlMain_ilMain.TransparentColor = System.Drawing.Color.Silver;
this.tabControlMain_ilMain.Images.SetKeyName(0, "");
and I am setting this image to a specific TabPage:
this.tabControlMain_tp17.ImageIndex = 0;
and I can see in the designer the image attached to the TabPage, but when I am running the application, I cannot see the image, it is just an empty space near the TabPage name.
Does anyone know what could be the problem ?
Thank you!
P.S. What I have seen is that, if I am not selecting the ImageIndex, the TabPage header is getting less length, so I guess the image is taking the space, but cannot see it at runtime.
So i did nothing different than you i think. But mine is working.
Maybe remove and readd the controls.
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.TabControlMain = new System.Windows.Forms.TabControl();
this.tabControlMain_tp17 = new System.Windows.Forms.TabPage();
this.tabControlMain_ilMain = new System.Windows.Forms.ImageList(this.components);
this.TabControlMain.SuspendLayout();
this.SuspendLayout();
//
// TabControlMain
//
this.TabControlMain.Controls.Add(this.tabControlMain_tp17);
this.TabControlMain.ImageList = this.tabControlMain_ilMain;
this.TabControlMain.Location = new System.Drawing.Point(44, 42);
this.TabControlMain.Size = new System.Drawing.Size(192, 191);
//
// tabControlMain_tp17
//
this.tabControlMain_tp17.ImageIndex = 0;
this.tabControlMain_tp17.Location = new System.Drawing.Point(4, 23);
this.tabControlMain_tp17.Text = "tabControlMain_tp17";
//
// tabControlMain_ilMain
//
this.tabControlMain_ilMain.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("tabControlMain_ilMain.ImageStream")));
this.tabControlMain_ilMain.TransparentColor = System.Drawing.Color.Transparent;
this.tabControlMain_ilMain.Images.SetKeyName(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.TabControlMain);
this.TabControlMain.ResumeLayout(false);
this.ResumeLayout(false);
}
private System.Windows.Forms.TabControl TabControlMain;
private System.Windows.Forms.TabPage tabControlMain_tp17;
private System.Windows.Forms.ImageList tabControlMain_ilMain;

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.

OpenFileDialog doesn't show

I have this simple code:
private void buttonOpen_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
textBox2.Text = openFileDialog1.FileName;
}
}
When I run program form doesn't show and exit of debugging mode.
In output view writes:The program '[4244] openfiledialog.vshost.exe: Managed (v4.0.30319)' has exited with code 1073741855 (0x4000001f).
I have Visual Studio 2010 Professional.
Edit:form1.designer.cs
private void InitializeComponent()
{
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.buttonOpen = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// openFileDialog1
//
this.openFileDialog1.FileName = "openFileDialog1";
//
// buttonOpen
//
this.buttonOpen.Location = new System.Drawing.Point(13, 48);
this.buttonOpen.Name = "buttonOpen";
this.buttonOpen.Size = new System.Drawing.Size(75, 23);
this.buttonOpen.TabIndex = 0;
this.buttonOpen.Text = "open";
this.buttonOpen.UseVisualStyleBackColor = true;
this.buttonOpen.Click += new System.EventHandler(this.buttonOpen_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(113, 50);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(279, 20);
this.textBox1.TabIndex = 1;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(13, 98);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(385, 20);
this.textBox2.TabIndex = 2;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(445, 216);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.buttonOpen);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
As a general rule I initialize and use my OpenFileDialog's within the event that is calling it. I can't think of a circumstance in which I would want it to be a property of my window. The first thing I would do is delete it as a property and initialize it in your event.
private void buttonOpen_Click(object sender, EventArgs e)
{
using (OpenFileDialog openFileDialog1 = new OpenFileDialog())
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
textBox2.Text = openFileDialog1.FileName;
}
}
}
You don't need to set the FileName property to anything because the dialog will do it for you.
The only thing I found on your error code was this (Program and debugger quit without indication of problem). Which in your current code I cannot find anything that would cause this. If you are accessing unmanaged code you may need to enable unmanaged code debugging.

My C# Windows Form Application is blank?

I'm Making A windows form application , And everytime I Excute it , All i see is a blank form , My project is named frmMain.cs and this is the header that i use :
using System;
using System.Windows.Forms;
public class frmMain : Form
{
private TextBox txtYear;
private Button btnCheck;
private Button btnClose;
private Label label1;
#region windows code
private void IntitializeComonent()
{
}
#endregion
public frmMain()
{
IntitializeComonent();
}
[STAThread]
public static void Main()
{
frmMain main = new frmMain();
Application.Run(main);
}
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.txtYear = new System.Windows.Forms.TextBox();
this.btnCheck = new System.Windows.Forms.Button();
this.btnClose = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label1.Location = new System.Drawing.Point(25, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(98, 20);
this.label1.TabIndex = 0;
this.label1.Text = "Year To Test: ";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// txtYear
//
this.txtYear.Location = new System.Drawing.Point(129, 24);
this.txtYear.Name = "txtYear";
this.txtYear.Size = new System.Drawing.Size(100, 20);
this.txtYear.TabIndex = 1;
//
// btnCheck
//
this.btnCheck.Location = new System.Drawing.Point(25, 93);
this.btnCheck.Name = "btnCheck";
this.btnCheck.Size = new System.Drawing.Size(75, 23);
this.btnCheck.TabIndex = 2;
this.btnCheck.Text = "Leap Year?";
this.btnCheck.UseVisualStyleBackColor = true;
//
// btnClose
//
this.btnClose.Location = new System.Drawing.Point(154, 93);
this.btnClose.Name = "btnClose";
this.btnClose.Size = new System.Drawing.Size(75, 23);
this.btnClose.TabIndex = 3;
this.btnClose.Text = "&Close";
this.btnClose.UseVisualStyleBackColor = true;
//
// frmMain
//
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.btnClose);
this.Controls.Add(this.btnCheck);
this.Controls.Add(this.txtYear);
this.Controls.Add(this.label1);
this.Name = "frmMain";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Determain a Leap Year";
this.Load += new System.EventHandler(this.frmMain_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
private void frmMain_Load(object sender, EventArgs e)
{
}
}
I also have a habit that i delete program.cs and frmMain.Designer.cs because i dont need them in my program , Does that have anything to do with my problem ? Help Please!
In frmMain() you're calling InitializeComonent() (which is empty) instead of calling InitializeComponent(). Probably just a typo.
Your code is fine. Just a small typo
public frmMain()
{
// In your constructor change 'IntitializeComonent' to 'InitializeComponent'
IntitializeComonent();
}

Categories

Resources