CefSharp WinForms Web Browser Won't Display - c#

I have a dead simple example trying to get the CEF Browser to append on a Winforms Form. For some reason it won't display.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
CefSharp.WinForms.ChromiumWebBrowser test = new CefSharp.WinForms.ChromiumWebBrowser("http://google.com");
this.Controls.Add(test);
}
}
Below is the VS Solution I am using. I added the package via Nuget

Your code above is a bit too dead simple :)
It's missing a call to Cef.Initialize()
See the Main() method of the CefSharp.MinimalExample.WinForms example for a working example and further details on how to use CefSharp.
You also need to give the control a DockStyle.Fill, like:
public Form1()
{
InitializeComponent();
var test = new CefSharp.WinForms.ChromiumWebBrowser("http://google.com")
{
Dock = DockStyle.Fill,
};
this.Controls.Add(test);
}

Related

Using System.Windows.Media API in a Winforms application results in weird UI behaviour

I am trying to use System.Windows.Media.Typeface to get some details of a font.
When I use it in my WinForms application the UI is resizing and the fonts are changing.
I noticed that my display resolution is at 125%. When I test this on 100% screen it does not happen.
Is there any issue of referencing PresentationCore.Dll in a winforms app. Or is
it about Typeface class.
namespace WindowsFormsApp1
{
public partial class WinFormample : Form
{
public WinFormample()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
System.Windows.Media.Typeface t = new System.Windows.Media.Typeface("Arial");
}
}
}

UNIT test. How to open the form in the test?

There is a Windows Forms application.
When the application starts, the form "Form0" will start.
Form0.ShiwInTaskBar = False;
I want to test the application.
I completed:
- created the project "UnitTestProject1".
- prescribed a link to a solution that I will test.
When testing, the Form0 should open.
The "Form0" class does not have a "Show ()" method.
How to open the form in the test?
Form0
namespace rsh
{
public partial class Form0 : Form
{
public Form0()
{
InitializeComponent();
}
}
}
Тест
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
//
using rsh;
namespace UnitTestProject1
{
[TestClass]
public class TestsStart
{
[TestMethod]
public void TestStart()
{
// Тест "Form0"
Form0 form0 = new Form0();
form0.
}
}
}
If you want to test the code:
You should be directly hitting the code as you are doing white box testing. You'll not be able to touch the UI side.
If you want to test the UI:
Please use a tool to interact with the UI of an Windows application:
Click Here..
Of course it is possible if you for your own testing
1.- In your testing project add the System.Windows.Forms reference (References section)
2.- do the below:
Form0 form0 = new Form0();
form0.ShowDialog();

How to pass data from Window to MainWindow in WPF?

Im newbie to programming and now i have a problem with my new login window.
I created a new window for login to my server database,but in my mainwindow how do i call it? I don't know how to draw it up better.
In my login window:
using MyS_Database;
MyServer_Database server;
public void LoginButton_Click(object sender, RoutedEventArgs e)
{
server = new MyServer_Database("ClientName","ServerIP","","",UserID.Text,UserPassword.Password.ToString(),"01",MyServer_Database.LoginType.User,out serverresult)
swith(serverresult)
{
case 0:
Mainwindow.Show();
this.Close();
break;
default:
Environment.Exit(0);
}
}
But when I call a method on my MainWindow which is communicating with the server i have to call it like:
server.GetDataFromUserList();
But It doesn't recognize server,I tried something like Win1.server or similars but I failed.
How could i pass it? Thank you in advance!
The quick answer is to make server public like this:
public MyServer_Database Server { get; set; }
Then you can call it in MainWindow with:
Win1.Server.GetDataFromUserList();
But this is not a good approach. Better approach is to abstract away these kind of operations in different classes via MVVM. Read more about MVVM and you'll find great approaches.
Edit
You should also do one one of these two things:
Don't close Win1 instead do this.Hide();
Keep the instance of MyServer_Database in MainWindow by passing it to the constructor of Win1.
You should put this in MainWindow
public MyServer_Database Server { get; set; }
and When needed call this:
var Win1 = new LoginWindow(Server);
Then you have access to Server object in MainWindow

Twitch Bot Form wont Initialize

I'm trying to make a chat bot for twitch and i want to make it in a Form so its very user friendly, i got the twitch communication done but now my form wont initialize. Please help.
namespace TwitchBotForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
IrcClient irc = new IrcClient("irc.twitch.tv", 6667, "z_bot909", "oauth:dn2tixd1xd7krggyn49ztw08hmfjea");
irc.joingRoom("z_dog909");
irc.sendChatMessage("Startup Complete");
while (true)
{
string message = irc.readMessage();
string[] splitMessage = message.Split('#');
if (message.Contains("!info"))
{
irc.sendChatMessage("Please Keep all commands LowerCase I made this bot Myself and i think its pretty cool. do !Help for Commands");
}
if (message.Contains("!me"))
{
irc.sendChatMessage(splitMessage[1].Split(':')[0]); //Username
}
}
}
}
}
You should not put this infinite loop in constructor of your form.
Check this article on how to run background thread with windows forms.

Is there anyway I can InitializeComponent another contentpage from a different page via a "click" for example?

Is there anyway you can "restart"/initialize a page from a click (from another page)? I am working with a rootpage cointaining a Master Detail Page and a contentpage, and I would want them to not Initialize their Components together as they are right now.
Maybe an option is to (if it is possible) make a clickevent with the Master Detail Page Icon if the InitializeComponent question is not possible.
But of course it would be more handy if I could seperate the two pages somehow. I am trying to come up with something below that would initialize another pages components.
button.Clicked += (object sender, EventArgs e) => {
var ourStartPage = new StartPage ();
ourStartPage.InitializeComponent ();
}
public class Page1 : ContentPage {
public Page1() {
// comment out the default InitializeComponent()
// InitializeComponent();
}
// create a public method to do the init instead
public void InitThisPage() {
InitializeComponent();
}
}

Categories