Alignment Troubles with Panel1.Controls.Add(browser)

vmars316 621 Reputation points
2020-10-04T23:34:40.9+00:00

Hello & Thanks ;
Having Alignment Troubles with Panel1.Controls.Add(browser) .
And 3 controls in all .
When a webpage is displayed (skewed) about 50px below the top
and 20px to the right of where it should be .
How can I fix this ?
Below is Form1.vb followed by Form1.Design.vb .
Thanks for your Help...

Imports cef
Imports CefSharp
Imports CefSharp.WinForms
Imports System.Windows.Forms

Public Class Form1
    Private WithEvents browser As ChromiumWebBrowser
    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim setting As New CefSettings
        '        setting.RemoteDebuggingPort = 8088
        CefSharp.Cef.Initialize(setting)
        browser = New ChromiumWebBrowser("https://www.google.com/") With {
                .Dock = DockStyle.Fill
                }
        Panel1.Controls.Add(browser)
        browser.Load(TextBox1.Text)

    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        browser.Load(TextBox1.Text)
    End Sub

End Class



<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()>
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()>
    Private Sub InitializeComponent()
        Me.TextBox1 = New System.Windows.Forms.TextBox()
        Me.Button5 = New System.Windows.Forms.Button()
        Me.Panel1 = New System.Windows.Forms.Panel()
        Me.SuspendLayout()
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(336, 14)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(210, 26)
        Me.TextBox1.TabIndex = 4
        '
        'Button5
        '
        Me.Button5.Location = New System.Drawing.Point(552, 14)
        Me.Button5.Name = "Button5"
        Me.Button5.Size = New System.Drawing.Size(75, 32)
        Me.Button5.TabIndex = 5
        Me.Button5.Text = "Button5"
        Me.Button5.UseVisualStyleBackColor = True
        '
        'Panel1
        '
        Me.Panel1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
            Or System.Windows.Forms.AnchorStyles.Left) _
            Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.Panel1.Location = New System.Drawing.Point(9, 63)
        Me.Panel1.Name = "Panel1"
        Me.Panel1.Size = New System.Drawing.Size(1201, 602)
        Me.Panel1.TabIndex = 7
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(9.0!, 20.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(1222, 677)
        Me.Controls.Add(Me.Button5)
        Me.Controls.Add(Me.TextBox1)
        Me.Controls.Add(Me.Panel1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub
    Friend WithEvents TextBox1 As TextBox
    Friend WithEvents Button5 As Button
    Friend WithEvents Panel1 As Panel
End Class
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,888 questions
{count} votes

Accepted answer
  1. Timon Yang-MSFT 9,591 Reputation points
    2020-10-07T09:16:32.537+00:00

    Hi,
    I found the solution, you can add the following in App.config.

      <System.Windows.Forms.ApplicationConfigurationSection>
        <add key="DpiAwareness" value="PerMonitorV2" />
      </System.Windows.Forms.ApplicationConfigurationSection>
    

    The premise is to use a newer .net framework version (4.7 or later).
    Best Regards,
    Timon

    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Timon Yang-MSFT 9,591 Reputation points
    2020-10-05T06:51:26.987+00:00

    Hi VernonMarsden,
    I used your code to reproduce the current situation, but I don't quite understand what you mean.
    This is what it looks like now, what do you want it to look like, can you explain it with a picture?
    I think this will make the problem clearer.
    30073-11.png

    Best Regards,
    Timon


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. vmars316 621 Reputation points
    2020-10-05T16:26:53.443+00:00

    Here is an image:
    Thanks for your Help...
    30187-cef-browserr.png


  3. vmars316 621 Reputation points
    2020-10-06T15:07:05.687+00:00

    Thanks ,
    Looks like it is DPI related .
    I run display options at 150% .
    So I added the following to all CefSharp.WinForms.XML files .

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>  
    <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">  
      <asmv3:application>  
        <asmv3:windowsSettings>  
          <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>  
          <dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2</dpiAwareness>  
        </asmv3:windowsSettings>  
      </asmv3:application>  
    </assembly>  
    

    setting-the-default-dpi-awareness-for-a-process

    It helps a bit , but still it is a bit off .
    30422-chromevb01-02.png

    If I run at 100% then things look fine .
    I am rewriting my KidSafeBrowser (freeware) from vs vb.net .vb (EI11)
    to CefSharp .vb and c# . But I can't expect kids to adjust 'display settings' if needed .

    If I can't do it programatically , is there a way to use some sort of
    scripting or .bat , etc. ?

    Thanks for your Help...

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.