Open a new form by reading comboBox value - c#

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

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.
:)

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

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

C# Windows Forms - Textbox in Form2 into List item Form1 [duplicate]

This question already has answers here:
Send values from one form to another form
(20 answers)
Closed 7 years ago.
I am having a problem adding text from my Form 2, to the List item in Form 1. Could anyone help please, Sorry if it's a bit jumbled, I'm new with coding :) What I am trying to achieve is a to-do list application, in the 2nd form, text should be entered in a text box - when the user clicks enter, this text should be entered into the list item.
Another question is, in this case would I be better using a list item or a list box?
Here is the code:
FORM 2
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 ToDoList
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public void button2_Click(object sender, EventArgs e, string tbtext)
{
//Form1 form1 = new Form1();
//form1.listBox1.Items.Add(tbtext);
}
public void button1_Click(object sender, EventArgs e)
{
this.Close();
}
public void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
ListViewItem item = new ListViewItem(textBox1.Text);
Form1 f1 = new Form1();
f1.listView1.Items.Add(item);
}
private void textBox1_TextChanged_1(object sender, EventArgs e)
{
}
}
}
FORM 1
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 ToDoList
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void filesToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
public void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
public void button2_Click(object sender, EventArgs e)
{
}
public void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void F1Button1_Click(object sender, EventArgs e)
{
}
private void tB1_TextChanged(object sender, EventArgs e)
{
}
public void listView1_SelectedIndexChanged_1(object sender, EventArgs e)
{
}
}
}
You create different object Form1, and the object is destroyd after you quit button2_Click. Is it local variable.
You need to create the datasource for your TODO list and propaget it to both forms, for example by method Set DataSource. Then you have one object accesible by
both forms.

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