CefSharp Set Object reference for buttons C#? - c#

WinForms:
I am new to Cefsharp and C# , just wrote a freeware 'Kid Safe Browser' in VS vb.net in visual basic , but IE11 is too limited .
I am getting "Object reference not set" for all Buttons . How to fix this ? For example:
How do I set an Object Reference for Chrome Load and Navigate Back Forward Refresh etc. ?
System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=CefSharpBrowser01
StackTrace:
at CefSharpBrowser01.SafeBrowser.GotoBtn_Click(Object sender, EventArgs e) in
C:\Users\vmars\source\repos\CefSharpBrowser01\Form1.cs:line 64
using CefSharp.WinForms;
using CefSharp.WinForms.Internals;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CefSharp;
using cef;
namespace CefSharpBrowser01
{
public partial class SafeBrowser : Form
{
public SafeBrowser()
{
InitializeComponent();
}
ChromiumWebBrowser chrome;
private void SafeBrowser_Load(object sender, EventArgs e)
{
CefSettings settings = new CefSettings();
//Initialize;
Cef.Initialize(settings);
AddressTxt.Text = "https://www.google.com";
chrome = new ChromiumWebBrowser(AddressTxt.Text);
this.Controls.Add(chrome);
chrome.Dock = DockStyle.Fill;
chrome.AddressChanged += Chrome_AddressChanged;
}
private void Chrome_AddressChanged(object sender, AddressChangedEventArgs e)
{
this.Invoke(new MethodInvoker(() =>
{
AddressTxt.Text = e.Address;
}));
}
private void BackBtn_Click(object sender, EventArgs e)
{
if (chrome.CanGoBack)
chrome.Back();
}
private void ForwardBtn_Click(object sender, EventArgs e)
{
if (chrome.CanGoForward)
chrome.Forward();
}
private void RefreshBtn_Click(object sender, EventArgs e)
{
chrome.Refresh();
}
private void GotoBtn_Click(object sender, EventArgs e)
{
chrome.Load(AddressTxt.Text);
}
}
}
Thanks for your Help...

Your problem really isn't anything to do with CefSharp, it's that you haven't created an instance of a class that you are trying to reference. Refactoring your code like I've suggested in the comments so the class is actually instantiated.
namespace CefSharpBrowser01
{
public partial class SafeBrowser : Form
{
public SafeBrowser()
{
InitializeComponent();
CefSettings settings = new CefSettings();
//Initialize;
Cef.Initialize(settings);
AddressTxt.Text = "https://www.google.com";
chrome = new ChromiumWebBrowser(AddressTxt.Text);
this.Controls.Add(chrome);
chrome.Dock = DockStyle.Fill;
chrome.AddressChanged += Chrome_AddressChanged;
}
ChromiumWebBrowser chrome;
private void Chrome_AddressChanged(object sender, AddressChangedEventArgs e)
{
this.Invoke(new MethodInvoker(() =>
{
AddressTxt.Text = e.Address;
}));
}
private void BackBtn_Click(object sender, EventArgs e)
{
if (chrome.CanGoBack)
chrome.Back();
}
private void ForwardBtn_Click(object sender, EventArgs e)
{
if (chrome.CanGoForward)
chrome.Forward();
}
private void RefreshBtn_Click(object sender, EventArgs e)
{
chrome.Refresh();
}
private void GotoBtn_Click(object sender, EventArgs e)
{
chrome.Load(AddressTxt.Text);
}
}
}

Related

Open a new form by reading comboBox value

I'm not very versed in coding, let's say this is some kind of project I started without knowing anything about coding.
I have a Button which should read the selection in the comboBox and then open a specific form.
I've tried using following the switch (comboBox1.SelectedText)
While that didn't quite work, I tried another approach:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace The_Liberion_Magnicite
{
public partial class Form1 : The_Liberion_Magnicite.Character
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
comboBox1.SelectedText.ToString();
{ if (comboBox1.SelectedText.ToString() == "Shiroichi Kazami (PTS)")
{
this.Hide();
Shiro1 CShiro1 = new Shiro1();
CShiro1.Show();
}
else if (comboBox1.SelectedText.ToString() == "Shiroichi Kazami (ATS)")
{
this.Hide();
Shiro2 CShiro2 = new Shiro2();
CShiro2.Show();
}
else if (comboBox1.SelectedText.ToString() == "Akari Hondo")
{
this.Hide();
AkariHondo CAkari = new AkariHondo();
CAkari.Show();
}
else if (comboBox1.SelectedText.ToString() == "Aboa Sekihara")
{
this.Hide();
AobaSeki CAoba = new AobaSeki();
CAoba.Show();
}
}
}
}
}
I'm in some dire need for help ;)

Why do I cannot create a new Tab from another Form?

I want to call the NewTab() function from another form. But it doesn't work. I'm new to C# and Programming and need help.
Form1:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CefSharp;
using CefSharp.WinForms;
namespace Browser
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
ChromiumWebBrowser chrome;
private void Window_Load(object sender, EventArgs e)
{
CefSettings settings = new CefSettings();
Cef.Initialize(settings);
txtUrl.Text = "https://google.com";
chrome = new ChromiumWebBrowser(txtUrl.Text);
chrome.Parent = tabControl.SelectedTab;
chrome.Dock = DockStyle.Fill;
chrome.AddressChanged += Chrome_AddressChanged;
chrome.TitleChanged += Chrome_TitleChanged;
}
private void Chrome_AddressChanged(object sender, AddressChangedEventArgs e)
{
this.Invoke(new MethodInvoker(() =>
{
txtUrl.Text = e.Address;
}));
}
private void buttonRefresh_Click(object sender, EventArgs e)
{
ChromiumWebBrowser chrome = tabControl.SelectedTab.Controls[0] as ChromiumWebBrowser;
if (chrome != null)
{
chrome.Refresh();
}
}
private void buttonNavigate_Click(object sender, EventArgs e)
{
ChromiumWebBrowser chrome = tabControl.SelectedTab.Controls[0] as ChromiumWebBrowser;
if(chrome != null)
{
chrome.Load(txtUrl.Text);
}
}
private void buttonForward_Click(object sender, EventArgs e)
{
ChromiumWebBrowser chrome = tabControl.SelectedTab.Controls[0] as ChromiumWebBrowser;
if (chrome != null)
{
if (chrome.CanGoForward)
{
chrome.Forward();
}
}
}
private void buttonBack_Click(object sender, EventArgs e)
{
ChromiumWebBrowser chrome = tabControl.SelectedTab.Controls[0] as ChromiumWebBrowser;
if (chrome != null)
{
if(chrome.CanGoBack)
{
chrome.Back();
}
}
}
private void Window_FormClosing(object sender, FormClosingEventArgs e)
{
Cef.Shutdown();
}
public void btnNewTab_Click(object sender, EventArgs e)
{
NewTab("https://google.com");
}
public void NewTab(string url)
{
TabPage tab = new TabPage();
tab.Text = "New Tab";
tabControl.Controls.Add(tab);
tabControl.SelectTab(tabControl.TabCount - 1);
ChromiumWebBrowser chrome = new ChromiumWebBrowser(url);
chrome.Parent = tab;
chrome.Dock = DockStyle.Fill;
txtUrl.Text = url;
chrome.AddressChanged += Chrome_AddressChanged;
chrome.TitleChanged += Chrome_TitleChanged;
}
private void Chrome_TitleChanged(object sender, TitleChangedEventArgs e)
{
this.Invoke(new MethodInvoker(() =>
{
tabControl.SelectedTab.Text = e.Title;
}));
}
private void closeTabToolStripMenuItem_Click(object sender, EventArgs e)
{
tabControl.Controls.Remove(tabControl.SelectedTab);
}
private void openMultipleTabsToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
}
}
}
Form2:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Browser
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 form1 = new Form1();
form1.NewTab("https://google.com");
}
}
}
I try to make a Webbrowser with special features. I use CefSharp for this. Sorry for the ugly Code I'm new to C# and Programming. Got most of this from a Youtube tutorial. I'm trying to make another window that you can open with a menustrip that lets you open multiple tabs at once. But for now, I don't even get one new Tab opened. It could be nice if you help me with this.
You create a new instance of Form1 but you don't show it.
Try this:
private void button1_Click(object sender, EventArgs e)
{
Form1 form = new Form1();
form.NewTab("https://google.com");
form.Show();
}

geckofx how do i enable cookies

I'm curious on how to enable cookies in geckofx, so when I restart the app it shows the cookies when I load the app all it shows is null here is the code to the form have a look and I really need help it might be something to do with proxy.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Gecko;
using MaterialSkin;
namespace FoxChatBETA
{
public partial class Form1 : MaterialSkin.Controls.MaterialForm
{
public Form1()
{
InitializeComponent();
var materialSkinManager = MaterialSkinManager.Instance;
materialSkinManager.AddFormToManage(this);
materialSkinManager.Theme = MaterialSkinManager.Themes.LIGHT;
materialSkinManager.ColorScheme = new ColorScheme(Primary.BlueGrey800, Primary.BlueGrey900, Primary.BlueGrey500, Accent.LightBlue200, TextShade.WHITE);
Xpcom.Initialize(Environment.CurrentDirectory);
GeckoPreferences.User["plugin.state.flash"] = true;
GeckoPreferences.User["network.cookie.thirdparty.sessionOnly"] = true;
GeckoPreferences.User["browser.xul.error_pages.enabled"] = true;
GeckoPreferences.User["media.navigator.enabled"] = true;
GeckoPreferences.User["media.navigator.permission.disabled"] = true;
GeckoPreferences.User["browser.cache.disk.enable"] = true;
GeckoPreferences.User["places.history.enabled"] = false;
GeckoPreferences.Default["extensions.blocklist.enabled"] = false;
}
private void Form1_Load(object sender, EventArgs e)
{
geckoWebBrowser1.Navigate(prefer not to show);
}
private void reloadToolStripMenuItem_Click(object sender, EventArgs e)
{
geckoWebBrowser1.Reload();
}
private void custemnumberToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
}
private void reloadToolStripMenuItem_Click_1(object sender, EventArgs e)
{
geckoWebBrowser1.Navigate(pfere not to show again);
}
private void devToolsToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void cONFIGToolStripMenuItem_Click(object sender, EventArgs e)
{
geckoWebBrowser1.Navigate("about:config");
}
}
}
You have set network.cookie.thirdparty.sessionOnly to true, which according to the documenation makes them only for the current session. The default value is false.
Third-party cookies are allowed within the limits of the general cookie acceptance settings but only retained for the current session.
https://developer.mozilla.org/pl/docs/Cookies_Preferences_in_Mozilla

WebCam_Capture notfound

I'm new and I have a problem with a class.
I have ever that problem " An unhandled exception of type 'System.IO.FileNotFoundException' occurred in WinFormCharpWebCam.exe"
But I added the refrence to my code.
Someone Can help me ?
Thank's
using System;
using System.Linq;
using System.Text;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;
using WebCam_Capture;
namespace WinFormCharpWebCam
{
//Design by Pongsakorn Poosankam
public partial class mainWinForm : Form
{
public mainWinForm()
{
InitializeComponent();
}
WebCam webcam;
private void mainWinForm_Load(object sender, EventArgs e)
{
webcam = new WebCam();
// webcam.InitializeWebCam(ref imgVideo);
}
private void bntStart_Click(object sender, EventArgs e)
{
webcam.Start();
}
private void bntStop_Click(object sender, EventArgs e)
{
webcam.Stop();
}
private void bntContinue_Click(object sender, EventArgs e)
{
webcam.Continue();
}
private void bntCapture_Click(object sender, EventArgs e)
{
imgCapture.Image = imgVideo.Image;
}
private void bntSave_Click(object sender, EventArgs e)
{
Helper.SaveImageCapture(imgCapture.Image);
}
private void bntVideoFormat_Click(object sender, EventArgs e)
{
webcam.ResolutionSetting();
}
private void bntVideoSource_Click(object sender, EventArgs e)
{
webcam.AdvanceSetting();
}
}
}

Windows Form is Really Laggy

I have a windows Form Project that runs smoothly and fine on framework 4.5.1
But yet I have a project on framework 2.0 that is so laggy even after i changed the target framework to 4.5.1
so why is one project slow and the other is normal?
Update :
here is the Main From
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace DataBaseLab_Library_Project
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void button5_Click(object sender, EventArgs e)
{
System.Windows.Forms.Application.Exit();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
MessageBox.Show(" ", "About US");
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
Add AddForm = new Add();
AddForm.Show();
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
Search SearchForm = new Search();
SearchForm.Show();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
}
}
and here is its design
Update 2 :
Form "Add.cs" that dose not lag .
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace DataBaseLab_Library_Project
{
public partial class Add : Form
{
public Add()
{
InitializeComponent();
}
private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
AddBook AddBookForm = new AddBook();
AddBookForm.Show();
}
private void button4_Click(object sender, EventArgs e)
{
this.Close();
MainForm mainForm = new MainForm();
mainForm.Show();
}
private void label3_Click(object sender, EventArgs e)
{
}
private void publisherButt_Click(object sender, EventArgs e)
{
this.Close();
AddPublishercs AddPublisherForm = new AddPublishercs();
AddPublisherForm.Show();
}
private void authorButt_Click(object sender, EventArgs e)
{
//this.Close();
//AddAuthor AddAuthorForm = new AddAuthor();
//AddAuthorForm.Show();
}
private void Add_Load(object sender, EventArgs e)
{
}
}
}
Try removing the panel1 paint eventhandler. This will get called (even tho its empty) ever time the panel has to draw itself, which is all the time in a designer environment.
Remove this code:
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
There will also be code inside of InitializeComponent() similar to this
this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1);
Remove it as well. Clean and rebuild solution. Close all designer windows. Try again.

Categories