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

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

Related

How to use serialPort1 over 2 Forms?

I am having trouble with using serialPort1 in both of my forms. I have opened the serialPort but it just isn't receiving or sending anything. Am I not able to do it this way or is there a simpler way of doing this. Please let me know.
Form 1 Code
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 Arduino_GUI
{
public partial class Form1 : Form
{
public delegate void d1(string indata);
public Form1()
{
InitializeComponent();
serialPort1.Open();
}
private void onButton_Click(object sender, EventArgs e)
{
serialPort1.Write("A"); // on
}
private void offButton_Click(object sender, EventArgs e)
{
serialPort1.Write("a"); // off
}
private void btnOForm2_Click(object sender, EventArgs e)
{
serialPort1.Close();
Form2 f2 = new Form2();
f2.Show();
this.Hide();
}
}
}
Form 2 Code
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 Arduino_GUI
{
public partial class Form2 : Form
{
public delegate void d1(string indata);
public Form2()
{
InitializeComponent();
serialPort1.Open();
}
private void btnMain_Click(object sender, EventArgs e)
{
serialPort1.Close();
Form1 f1 = new Form1();
f1.Show();
this.Hide();
}
private void button1_Click(object sender, EventArgs e)
{
serialPort1.WriteLine("2");
}
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) // receiving data
{
String indata = serialPort1.ReadLine();
d1 writeit = new d1(Write2Form);
Invoke(writeit, indata);
}
public void Write2Form(string indata)
{
char firstchar;
string test;
firstchar = indata[0];
switch (firstchar)
{
case 'r':
test = indata.Substring(1);
label1.Text = test;
break;
}
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
Form1 f1 = new Form1();
f1.Show();
f1.Close();
}
}
}
Arduino Code
String data;
char d1;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if(Serial.available()){
data = Serial.readString();
d1 = data.charAt(0);
switch(d1){ // select action based on first character
case '2': // Do when Form2 opens
Serial.println('r');
break;
}
}
}
Thank you for going through this all and I hope you can help me.
:)

Transfer values from one class to another using a list

I am facing a problem in my winforms app I am building in Visual Studio 2019. My app has 8 forms and I constructed a new class in which I want to save the name of the forms that users visited through a list and save them in a .txt file. I am providing you the pieces of code that I am trying to implement.
Code of one of 8 forms
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SQLite;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Speech.Synthesis;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Delfoi_Tourist_Guide
{
public partial class Welcome : Form
{
SQLiteConnection connection;
private SpeechSynthesizer speech = new SpeechSynthesizer();
public Welcome()
{
InitializeComponent();
User_History.HistList.Add(this.Name);
User_History.SaveHistory();
}
private void Welcome_Load(object sender, EventArgs e)
{
String conn = "Data Source=Delfoidb1.db;Version=3";
connection = new SQLiteConnection(conn);
connection.Open(); //or Create Database
}
private void Timer1_Tick(object sender, EventArgs e)
{
label1.Show();
label2.Show();
button1.Visible = true;
button2.Visible = true;
timer2.Enabled = true;
timer1.Enabled = false;
}
private void button1_Click(object sender, EventArgs e)
{
if (speech.State == SynthesizerState.Speaking)
{
speech.Pause();
}
Form2 form2 = new Form2();
form2.Show();
this.Visible = false;
}
private void button6_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void timer2_Tick(object sender, EventArgs e)
{
speech.SelectVoice("Microsoft Stefanos");
speech.SpeakAsync(label2.Text);
timer2.Enabled = false;
}
private void button2_Click(object sender, EventArgs e)
{
if (speech.State == SynthesizerState.Speaking)
{
speech.Pause();
}
Form8 form8 = new Form8();
form8.Show();
this.Visible = false;
}
private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
{
MessageBox.Show("Developed by Ανδρέας Κρεούζος(ΜΠΠΛ20040) and Δημήτρης Γιογάκης(ΜΠΠΛ20008)");
}
private void helpToolStripMenuItem_Click(object sender, EventArgs e)
{
//Implement try - catch to avoid exception specifically for url error
try
{
Help.ShowHelp(this, "_tmphhp/Delfoi_Tourist_Guide_Help.chm", HelpNavigator.KeywordIndex, "Topic 1");
}
catch (Exception exception)
{
MessageBox.Show(exception.ToString());
}
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
if (speech.State == SynthesizerState.Speaking)
{
speech.Pause();
}
Welcome welcome = new Welcome();
welcome.Show();
this.Visible = false;
}
private void έξοδοςToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
Code of the separate class that saves the data as txt
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Delfoi_Tourist_Guide
{
public static class User_History
{
public static List<string> HistList = new List<string>();
public static void SaveHistory()
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
string path = saveFileDialog.FileName;
StreamWriter sw = new StreamWriter(path);
sw.WriteLine(HistList);
sw.Close();
}
}
}
}
My problem is that I can't transfer the data (placed on the InitializeComponent) from forms to my class. I am getting my txt file with nothing in it. My data is actually saved on my public static list but I can't transfer it next to the rest of my User_History class.
Any ideas on how to accomplish this???
You can try this SaveHistory() method
public static void SaveHistory()
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
string path = saveFileDialog.FileName;
using (StreamWriter writer = new StreamWriter(path, false))
{
foreach (string history in HistList)
{
writer.WriteLine(history);
}
}
}
}

CefSharp Set Object reference for buttons 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);
}
}
}

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

How can i pass string from new form to form1 richtextbox?

In Form1
private void button4_Click(object sender, EventArgs e)
{
AddText at = new AddText();
at.Show();
richTextBox2.Text = at.text;
}
In new Form
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 test
{
public partial class AddText : Form
{
public string text = "";
public AddText()
{
InitializeComponent();
}
private void AddText_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
text = textBox1.Text;
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
When I click button1 in the new form it does assign the text from textBox1 to the variable text.
But it's not passing it to the Form1.richTextBox2.
I guess the problem is that I try to assign the text in Form1 in the button click event:
richTextBox2.Text = at.text;
But this happen before the button Click event in the new form.
Where/how in Form1 should I assign the text to richTextBox2 ?
I used ShowDialog() it's working only when I close the new form window. Only when I close it I see the text in the richTextBox2. But I want to see the text in richTextBox2 when I click the Ok(button1) button without closing the form.
Form1
private void button4_Click(object sender, EventArgs e)
{
AddText at = new AddText(this);
at.Show();
richTextBox2.Text = at.text;
}
public void SetText(string text)
{
richTextBox2.Text = text;
}
New form
public partial class AddText : Form
{
private Form1 _form1;
public AddText(Form1 form1)
{
InitializeComponent();
_form1 = form1;
}
private void AddText_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
_form1.SetText(textBox1.Text);
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}

Categories