This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 8 years ago.
Sorry for asking this question again but, I don't really know how to debug this.
The debugger is giving me nullreferenceexception in this line:
if (listBox1.SelectedItem.ToString() == "Chicken $15")
I figure that the reason why it is giving me nullreferenceexception is because listbox1 is null so I think I have to initialize it. But how? I dont know how to initialize a listbox.
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 lab6
{
public partial class Form1 : Form
{
double total = 0;
int x = 0;
string ord = "";
public Form1()
{
InitializeComponent();
}
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
}
private void editToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void placeToolStripMenuItem_Click(object sender, EventArgs e)
{
checkBox1.Checked = false;
radioButton1.Checked = false;
radioButton2.Checked = false;
radioButton3.Checked = false;
radioButton4.Checked = false;
switch (checkBox1.Checked)
{
case true:
total += 1;
ord += "Water\n";
break;
}
if (comboBox1.Text == "Extra Meat")
{
total += 1;
ord += ord + "Extra Meat\n";
}
if (comboBox1.Text == "Extra Rice")
{
total += 1;
ord += "Extra Rice\n";
}
if (comboBox1.Text == "Extra Veggies")
{
total += 1;
ord += "Extra Veggies\n";
}
if (listBox1.SelectedItem.ToString() == "Chicken $15")
{
total += 15;
ord += " Chicken\n";
}
else { }
if (listBox1.SelectedItem.ToString() == "Pizza $8") //< my pathetic attempt to figure it out with intelisense
{
total += 8;
ord += "Pizza\n";
}
else
{
}
if (listBox1.SelectedItem.ToString() == "Spaghetti $12")//< my pathetic attempt to figure it out with intelisense
{
total += 12;
ord += " Spaghetti\n";
}
else { }
if (listBox1.SelectedItem.ToString() == "Fries $8")
{
total += 8;
ord += " Fries\n";
}
else { }
if (listBox1.SelectedItem.ToString() == "Burger $10")
{
total += 10;
ord += " Burger\n";
}
else { }
//radiobutton
if (radioButton1.Checked)
{
total+=5;
ord += "Pineapple Juice\n";
}
if (radioButton2.Checked)
{
total+=6;
ord += "Mango Juice\n";
}
if (radioButton3.Checked)
{
total+=7;
ord += "Apple Juice\n";
}
if (radioButton4.Checked)
{
total+=8;
ord += "Orange Juice\n";
}
MessageBox.Show("Order Done");
listBox1.SelectedItems.Clear();
}
private void clearToolStripMenuItem_Click(object sender, EventArgs e)
{
ord = "";
total = 0;
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
}
private void label3_Click(object sender, EventArgs e)
{
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
}
private void radioButton4_CheckedChanged(object sender, EventArgs e)
{
}
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void label4_Click(object sender, EventArgs e)
{
}
private void displayToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("Order: " + ord+"\nTotal: "+total);
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
It looks like you're assuming that listBox1.SelectedItem is never null, try doing somthing like
if (listBox1.SelectedItem != null)
{
// code here
}
Related
I need to calculate the Lb1SumF plus Lb2SumF equal Lb3SumF.
I ran it, and somehow the label3 does not display the expected result.
Here is a screenshot from result.
Here is my code.
private void Form1_Load(object sender, EventArgs e)
{
TX1.TabIndex=0;
}
private void TX1_TextChanged(object sender, EventArgs e)
{
try
{
int sumF;
sumF = Convert.ToInt32(Lb1PriceF.Text) * Convert.ToInt32(TX1.Text);
Lb1SumF.Text = Convert.ToString(sumF); //Label1 sum
}
catch
{
Lb1SumF.Text = "0";
}
}
private void TX2_TextChanged(object sender, EventArgs e)
{
try
{
int sumF;
sumF = Convert.ToInt32(Lb2PriceF.Text) * Convert.ToInt32(TX2.Text);
Lb2SumF.Text = Convert.ToString(sumF); //Label2 sum
}
catch
{
Lb2SumF.Text = "0";
}
}
private void Lb3_TextChanged(object sender, EventArgs e)
{
int i = Convert.ToInt32(Lb1SumF.Text);
int j = Convert.ToInt32(Lb2SumF.Text);
Lb3.Text = Convert.ToString(i+j); // Label3 sum
}
Lb3_TextChanged might never be invoked as you are not changing the text of the label. I would suggest to change it to a private method and not an event handler. Here is what the code could be like:
private void TX1_TextChanged(object sender, EventArgs e)
{
try
{
int sumF;
sumF = Convert.ToInt32(Lb1PriceF.Text) * Convert.ToInt32(TX1.Text);
Lb1SumF.Text = Convert.ToString(sumF); //Label1 sum
// Call to update sum
UpdateSum();
}
catch
{
Lb1SumF.Text = "0";
}
}
private void TX2_TextChanged(object sender, EventArgs e)
{
try
{
int sumF;
sumF = Convert.ToInt32(Lb2PriceF.Text) * Convert.ToInt32(TX2.Text);
Lb2SumF.Text = Convert.ToString(sumF); //Label2 sum
// Call to update sum
UpdateSum();
}
catch
{
Lb2SumF.Text = "0";
}
}
// private void Lb3_TextChanged(object sender, EventArgs e)
private void UpdateSum()
{
int sum = 0;
if(!string.IsNullOrEmpty(Lb1SumF.Text) && !string.IsNullOrEmpty(Lb2SumF.Text))
{
sum = Convert.ToInt32(Lb1SumF.Text) + Convert.ToInt32(Lb2SumF.Text);
}
Lb3.Text = Convert.ToString(sum);
}
Replace you code with this it will work, you are using * operator where you have to use + operator, I have commented that lines in your code and replaced it for better understanding.
Happy Coding
namespace WindowsFormsApp8
{
public partial class Form1 : Form
{
private void Lb1SumF_Click(object sender, EventArgs e)
{
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
TX1.TabIndex=0;
}
private void label4_Click(object sender, EventArgs e)
{
}
private void TX1_TextChanged(object sender, EventArgs e)
{
try
{
int sumF;
//sumF = Convert.ToInt32(Lb1PriceF.Text) * Convert.ToInt32(TX1.Text); // You were doing wrong here , you were multiplying these values
sumF = Convert.ToInt32(Lb1PriceF.Text) + Convert.ToInt32(TX1.Text);
Lb1SumF.Text = Convert.ToString(sumF); //Label1 sum
}
catch
{
Lb1SumF.Text = "0";
}
}
private void TX2_TextChanged(object sender, EventArgs e)
{
try
{
int sumF;
//sumF = Convert.ToInt32(Lb2PriceF.Text) * Convert.ToInt32(TX2.Text); //you are doing it wrong here , you are multiplying
sumF = Convert.ToInt32(Lb2PriceF.Text) + Convert.ToInt32(TX2.Text);
Lb2SumF.Text = Convert.ToString(sumF); //Label2 sum
}
catch
{
Lb2SumF.Text = "0";
}
}
private void Lb3_TextChanged(object sender, EventArgs e)
{
int i = Convert.ToInt32(Lb1SumF.Text);
int j = Convert.ToInt32(Lb2SumF.Text);
Lb3.Text = Convert.ToString(i+j); // Label3 sum
}
private void Lb3SumF_Click(object sender, EventArgs e)
{
}
}
}
I'm trying to make it so that my form does not appear blank. It was working before, It wouldn't let me save my file at one point so I renamed it to form3temp and now the form shows up when I run it, but not when I look at it in design view. Form 5 also does the same thing. The rest of my 7 forms are fine.
How it appears inside design
https://i.gyazo.com/06994988ff2f307d5042c0bafec1c43e.png
How it appears when I run the application
https://gyazo.com/97c1838e55f5f595b09211f2ee742c9f
namespace LawnCarePrototype
{
public partial class Form31 : Form
{
public static decimal yardCost = 0.0M;
decimal yardSizeMultiplier = 0.0M;
public Form31()
{
InitializeComponent();
}
private void Form3_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
yardSizeMultiplier = 0.0M;
yardCost = 0.0M;
if (radioButton1.Checked)
{
yardSizeMultiplier = 0.8M;
}
if (radioButton2.Checked)
{
yardSizeMultiplier = 1.0M;
}
if (radioButton3.Checked)
{
yardSizeMultiplier = 1.2M;
}
if (checkBox1.Checked)
{
yardCost =+ 15.00M * yardSizeMultiplier;
//2 EDGE
Global.edgeExp = 15.00M * yardSizeMultiplier;
}
if (checkBox2.Checked)
{
yardCost =+30.00M * yardSizeMultiplier;
Global.bushExp = 30.00M * yardSizeMultiplier;
//3 BUSH
}
if (checkBox3.Checked)
{
yardCost =+ 25.00M * yardSizeMultiplier;
Global.leafExp = 25.00M * yardSizeMultiplier;
//4 LEAF
}
if (checkBox4.Checked)
{
yardCost =+ 35.00M * yardSizeMultiplier;
//1 LAWN
Global.MowExp = 35.00M * yardSizeMultiplier;
}
if (yardCost == 0.00M)
{
MessageBox.Show("Please Input Your Selection");
}
else
{
Form4 form4obj = new Form4();
form4obj.Show();
this.Close();
}
Global.subTotal = yardCost;
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2obj = new Form2();
form2obj.Show();
this.Close();
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
}
private void checkBox4_CheckedChanged(object sender, EventArgs e)
{
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
}
}
I expect the form design to appear because I don't see any errors.
Not sure what could be causing this in my code.
Side Note: Sorry for the poor code format!
I have a project where I need to communicate with Arduino over the serial ports. The problem I face is that I cannot print continously the data I receive from the serial monitor on multiple lines on a richtextbox. When I press the button "Reveice" I do receive only one value and after this, pressing again the Receive button will overwrite the line.
I'm tring to fix this for few days, but it's my first time programming in c# so I'm asking for your help.
The code:
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 System.IO.Ports;
namespace aplicatie_comanda_v1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
getAvilablePorts();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
serialPort1.Close();
progressBar1.Value = 0;
button3.Enabled = true;
button1.Enabled = false;
receive.Enabled = false;
richTextBox1.Clear();
}
private void label1_Click(object sender, EventArgs e)
{
}
void getAvilablePorts()
{
string[] ports = SerialPort.GetPortNames();
comboBox1.Items.AddRange(ports);
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
try
{
if (comboBox1.Text == "" || comboBox2.Text == "" && serialPort1 != null && serialPort1.IsOpen)
{
richTextBox1.Text = "Select COM port and BAUD rate !";
serialPort1.Close();
}
else
{
string cmd = Convert.ToString(comboBox1.Text);
int baud = Convert.ToInt32(comboBox2.Text);
serialPort1.PortName = cmd;
serialPort1.BaudRate = baud;
serialPort1.DtrEnable = true;
serialPort1.RtsEnable = true;
serialPort1.Open();
progressBar1.Value = 100;
button1.Enabled = true;
button2.Enabled = true;
textBox1.Enabled = true;
button3.Enabled = false;
}
}
catch (UnauthorizedAccessException)
{
richTextBox1.Text = "Unauthorized !";
}
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string text = textBox1.Text;
serialPort1.Write(text);
}
private void receive_Click(object sender, EventArgs e)
{
try
{
richTextBox1.Text = serialPort1.ReadLine() + "\n";
}
catch (TimeoutException)
{
richTextBox1.Text = "Timeout !";
}
}
private void button4_Click(object sender, EventArgs e)
{
serialPort1.Write("w");
}
private void button5_Click(object sender, EventArgs e)
{
serialPort1.Write("s");
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
}
private void button6_Click(object sender, EventArgs e)
{
serialPort1.Write("a");
}
private void button7_Click(object sender, EventArgs e)
{
serialPort1.Write("d");
}
private void button12_Click(object sender, EventArgs e)
{
serialPort1.Write("b");
}
private void button13_Click(object sender, EventArgs e)
{
string cmd = Convert.ToString(trackBar1.Value);
serialPort1.Write(cmd);
}
private void button8_Click(object sender, EventArgs e)
{
serialPort1.Write("q");
}
private void button11_Click(object sender, EventArgs e)
{
serialPort1.Write("e");
}
private void button9_Click(object sender, EventArgs e)
{
serialPort1.Write("z");
}
private void button10_Click(object sender, EventArgs e)
{
serialPort1.Write("c");
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
while (serialPort1.IsOpen)
{
try
{
string date = serialPort1.ReadLine();
richTextBox1.Text = date + "\n";
}
catch (TimeoutException)
{
richTextBox1.Text = "Timeout !";
}
}
}
}
}
Print screen of the final app: http://i.imgur.com/5f8EOly.png
Thank you !
I haven't written any Serial applications in C# yet, but already did a few projects involving Java <-> Arduino communication.
My first guess would be that you overwrite the existing line with the received line.
richTextBox1.Text = serialPort1.ReadLine() + "\n";
instead you would want:
richTextBox1.Text += serialPort1.ReadLine() + "\n";
Also you should take a look at this article on MSDN:
https://msdn.microsoft.com/en-us/library/system.io.ports.serialport.datareceived(v=vs.110).aspx
This shows how you could use Events to continuously receive text from the Arduino.
I am trying to make a reset button my my C# Windows Form. But, when I have it clear the textboxes with a code like this:
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
textBox5.Clear();
textBox6.Clear();
then, it gives me the following error: "Input string was not in a correct format"
Here is my code:
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 System.Collections;
namespace MidTermPizzas
{
public partial class Form1 : Form
{
pizzaOrder aOrder = new pizzaOrder();
public Form1()
{
InitializeComponent();
}
private void formTitle_Click(object sender, EventArgs e)
{
}
//click File, Exit
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("Enjoy your pizza!");
this.Close();
}
//click View, Summary of Orders Placed
private void summaryOfOrdersToolStripMenuItem_Click(object sender, EventArgs e)
{
SummaryOfOrdersPlaced myForm = new SummaryOfOrdersPlaced();
myForm.Show();
}
//amount of Pizzas label
private void label1_Click(object sender, EventArgs e)
{
}
//form load
private void Form1_Load(object sender, EventArgs e)
{
}
//sales tax label
private void label3_Click(object sender, EventArgs e)
{
}
//text in box to the right of "Amount of Pizzas"
private void textBox1_TextChanged(object sender, EventArgs e)
{
aOrder.numberOfPizzas = int.Parse(textBox1.Text);
}
//text in box to the right of "Amount of Cokes"
private void textBox2_TextChanged(object sender, EventArgs e)
{
aOrder.numberOfCokes = int.Parse(textBox2.Text);
}
//text in box to the right of "Sales Tax"
private void textBox4_TextChanged(object sender, EventArgs e)
{
}
//File Tool Strip Menu
private void fileToolStripMenuItem_Click(object sender, EventArgs e)
{
}
//amount of cokes label
private void label2_Click(object sender, EventArgs e)
{
}
//reset button
private void button1_Click_1(object sender, EventArgs e)
{
//textBox1.Clear();
//textBox2.Clear();
//textBox3.Clear();
//textBox4.Clear();
//textBox5.Clear();
//textBox6.Clear();
}
//text in box to the right of "Amount Due"
private void textBox3_TextChanged_1(object sender, EventArgs e)
{
}
//text in box to the right of "Amount Paid"
private void textBox5_TextChanged(object sender, EventArgs e)
{
aOrder.getAmountPaid = double.Parse(textBox5.Text);
}
//click Calculate Change Due button
private void calculateChangeDue_Click(object sender, EventArgs e)
{
textBox6.Text = Convert.ToString(aOrder.GetChangeDue());
}
//text in box to right of Change Due
private void textBox6_TextChanged(object sender, EventArgs e)
{
}
//amount due label
private void label4_Click(object sender, EventArgs e)
{
}
//amount paid label
private void label5_Click(object sender, EventArgs e)
{
}
//change due label
private void label6_Click(object sender, EventArgs e)
{
}
//click Calculate Amount Due
private void calculateAmountDue_Click(object sender, EventArgs e)
{
textBox3.Text = Convert.ToString(aOrder.GetAmountDue());
}
//click Calculate Sales Tax
private void button2_Click(object sender, EventArgs e)
{
textBox4.Text = Convert.ToString(aOrder.TaxDue());
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace MidTermPizzas
{
class pizzaOrder
{
public int numberOfCokes
{
get;
set;
}
public int numberOfPizzas
{
get;
set;
}
public double InputOrder()
{
const double COKE_PRICE = 1.49;
const double PIZZA_PRICE = 7.99;
double inputOrder = (numberOfCokes * COKE_PRICE) + (numberOfPizzas * PIZZA_PRICE);
return inputOrder;
}
public double TaxDue()
{
const double TAX = .073;
double taxDue = (this.InputOrder() * TAX);
return taxDue;
}
public double GetAmountDue()
{
double getAmountDue = this.InputOrder() + this.TaxDue();
return getAmountDue;
}
public double getAmountPaid
{ get; set; }
public double GetChangeDue()
{
double getChangeDue = this.getAmountPaid - this.GetAmountDue();
return getChangeDue;
}
}
}
private void textBox1_TextChanged(object sender, EventArgs e){
aOrder.numberOfPizzas = textBox1.Text == "" ? 0 : int.Parse(textBox1.Text);
}
Do the same for other textBoxes, you should use TryParse and also use 1 TextChanged event handler for all the textBoxes.
The following code uses TryParse and suppose if the parsing fails, the default value will be 0:
private void textBox1_TextChanged(object sender, EventArgs e){
int v;
if(int.TryParse(textBox1.Text, out v)){
aOrder.numberOfPizzas = v;
} else aOrder.numberOfPizzas = 0;
}
Either use TryParse as suggested by other answers or place 0 for text boxes used for numeric input in your clear method. Another approach is to use MaskedTextBox for text boxes used for numeric input.
Here you can use the manual method as well. Write the following code inside your button click function,
if (textBox1.Text != string.Empty || textBox2.Text != string.Empty || textBox3.Text != string.Empty || textBox4.Text != string.Empty || textBox5.Text != string.Empty || textBox6.Text != string.Empty)
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
}
I'm new to c#, and after looking through the other threads on this subject I'm still at a loss of how to fix the bug:C.
I'm trying to make a simple calculator and here is the code for that:
//Global Variables
string sign;
double val1;
double val2;
int trackkeypoint = 0;
public void Calculator()
{
InitializeComponent();
}
private void cmd0_Click(object sender, EventArgs e)
{
txtbox.Text = txtbox.Text + cmd0.Text;
}
private void cmd1_Click(object sender, EventArgs e)
{
txtbox.Text=txtbox.Text+cmd1.Text;
}
private void cmd2_Click(object sender, EventArgs e)
{
txtbox.Text=txtbox.Text+cmd2.Text;
}
private void cmd3_Click(object sender, EventArgs e)
{
txtbox.Text=txtbox.Text+cmd3.Text;
}
private void cmd4_Click(object sender, EventArgs e)
{
txtbox.Text=txtbox.Text+cmd4.Text;
}
private void cmd5_Click(object sender, EventArgs e)
{
txtbox.Text=txtbox.Text+cmd5.Text;
}
private void cmd6_Click(object sender, EventArgs e)
{
txtbox.Text=txtbox.Text+cmd6.Text;
}
private void cmd7_Click(object sender, EventArgs e)
{
txtbox.Text=txtbox.Text+cmd7.Text;
}
private void cmd8_Click(object sender, EventArgs e)
{
txtbox.Text=txtbox.Text+cmd8.Text;
}
private void cmd9_Click(object sender, EventArgs e)
{
txtbox.Text=txtbox.Text+cmd9.Text;
}
private void cmdequal_Click(object sender, EventArgs e)
{
val2 = double.Parse(txtbox.Text);
double result;
if(sign=="+")
{
result = val1 + val2;
txtbox.Text = result.ToString();
}
else if(sign=="-")
{
result = val1 - val2;
txtbox.Text = result.ToString();
}
else if(sign=="X")
{
result = val1 * val2;
txtbox.Text = result.ToString();
}
else if(sign=="/")
{
result = val1 / val2;
txtbox.Text = result.ToString();
}
}
private void cmdclear_Click(object sender, EventArgs e)
{
//Clears text
txtbox.Text = "";
val1 = 0;
val2 = 0;
sign = "";
}
private void cmdplus_Click(object sender, EventArgs e)
{
sign = "+";
val1 = double.Parse(txtbox.Text);
txtbox.Text = "";
}
private void cmdsubtract_Click(object sender, EventArgs e)
{
sign = "-";
val1 = double.Parse(txtbox.Text);
txtbox.Text = "";
}
private void cmdmultiply_Click(object sender, EventArgs e)
{
sign = "X";
val1 = double.Parse(txtbox.Text);
txtbox.Text = "";
}
private void cmddivide_Click(object sender, EventArgs e)
{
sign = "/";
val1 = double.Parse(txtbox.Text);
txtbox.Text = "";
}
private void cmdsqrt_Click(object sender, EventArgs e)
{
double v;
v = double.Parse(txtbox.Text);
txtbox.Text = Math.Sqrt(v).ToString();
}
private void cmdsquare_Click(object sender, EventArgs e)
{
double v;
v = double.Parse(txtbox.Text);
txtbox.Text = Math.Pow(v,2).ToString();
}
private void cmdsin_Click(object sender, EventArgs e)
{
double v;
v = double.Parse(txtbox.Text);
txtbox.Text = Math.Sin(v).ToString();
}
private void cmdcos_Click(object sender, EventArgs e)
{
double v;
v = double.Parse(txtbox.Text);
txtbox.Text = Math.Cos(v).ToString();
}
private void cmdtan_Click(object sender, EventArgs e)
{
double v;
v = double.Parse(txtbox.Text);
txtbox.Text = Math.Tan(v).ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void txtbox_TextChanged(object sender, EventArgs e)
{
}
private void txtbox_KeyPress(object sender, KeyPressEventArgs e)
{
int keycode;
keycode = e.KeyChar;
//accept only number from key 0 to key 9, key back, and key dot
if (keycode >= 48 && keycode <= 57 || keycode==8 || keycode==32 || keycode==46)
{
//key dot allowed only one time
if (keycode == 46) ++trackkeypoint;
if (trackkeypoint > 1) { e.Handled = true; --trackkeypoint; }
}
else e.Handled = true;
}
private void txtbox_KeyDown(object sender, KeyEventArgs e)
{
}
}
}
and I get this error:
I tried changing that to CWindowsGUI and that didn't work, or removing the offending bits, or a host of other layman fixes. It also shows up in the designer window:
The namespace is the same on the CWindowsGUI.Designer.cs as the actual code thing
Looking at the class you provided I think you renamed your Form1 to Calculator
So try:
Application.Run(new Calculator());
Edit:
CWindowsGUI.cs
public partial class Calculator : Form
{
public Calculator() // Not public void Calculator()
{
InitializeComponent();
}
CWindowsGUI.Designer.cs
public partial class Calculator
First you need to fix some errors
private void Form1_Load(object sender, EventArgs e)
{
}
you do not have any form named as Form1 it seems that when you created application it created a form for you named as Form1 you just renamed it from solution explorer.
delete this load event.
public void Calculator()
{
InitializeComponent();
}
is it a constructor of your form CWindowsGUI
constructor name should be same as form name correct it.
Do You wants to run CWindowsGUI form first ???
Application.Run();
method defines that which form you wants to set as startup that's all
then try this :
Application.Run(new CWindowsGUI());
instead of
Application.Run(new Calculator());
my suggestion would be that delete this form(CWindowsGUI) and create a new form then code on that page also donot forget to change the form in
Application.Run(new CWindowsGUI());