can't insert webbrowser inside panel in c#winapp - c#

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.

Related

Adding C# form into another form

I need to add the form into the panel of the another parent form, but that is not working. When I try to add within the main parent form. I have a dashboard form and when I click the button a sidebar for gets open into one of the panels and again I need to load the form into the second panel of the dashboard form when I click on the button of the sidebar form.
public void btn_add_Click(object sender, EventArgs e)
{
Admin_Dashboard frm = new Admin_Dashboard();
Brand.Add_Brand myform = new Brand.Add_Brand();
myform.FormBorderStyle = FormBorderStyle.None;
myform.TopLevel = false;
myform.AutoScroll = true;
frm.content.Controls.Clear();
frm.content.Controls.Add(myform);
myform.Show();
}
This is what you do:
Make a custom control, which everything that the form has.
Change the form so it only has the custom control.
In the panel where you want the form... you put the custom control.
Bonus chatter: you can create a class to represent all the information that the custom control has to show. Then you can pass it around to show it both in the form and in the panel. Oh, and that class is a we call a view model.
I'm not sure what frm.content.Controls is, but this works...
using System;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
class Form1 : Form
{
private Panel panel1;
private Button button1;
public Form1()
{
this.panel1 = new Panel();
this.button1 = new Button();
this.SuspendLayout();
this.panel1.Location = new System.Drawing.Point(305, 51);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(457, 338);
this.panel1.TabIndex = 0;
this.button1.Location = new System.Drawing.Point(321, 9);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.button1);
this.Controls.Add(this.panel1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.FormBorderStyle = FormBorderStyle.None;
form2.TopLevel = false;
form2.AutoScroll = true;
panel1.Controls.Clear();
panel1.Controls.Add(form2);
form2.Show();
}
}
class Form2 : Form
{
private Button button1;
public Form2()
{
this.button1 = new Button();
this.SuspendLayout();
this.button1.Location = new System.Drawing.Point(321, 9);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.Yellow;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.button1);
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false);
}
}
}
Am I missing something?

Form bigger than it supposed to be

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!

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;

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

Categories

Resources