Hi I am learning C# and struggling with a Form in which a btnCalc_click event should call a method calcArea and produce the output in a textBox1.text
the error is in row: textBox1.Text = calcArea.ToString();
who can help with with the correct syntax ?
namespace Meetkunde
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void txbLengte_TextChanged(object sender, EventArgs e)
{
double length = 0;
length = double.Parse(txbLength.Text);
}
private void txbBreedte_TextChanged(object sender, EventArgs e)
{
double width = 0;
width = double.Parse(txbWidth.Text);
}
public double calcArea(double length, double width)
{
double area = 0;
area = (length * width);
return area;
}
private void label3_Click(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void btnCalc_Click(object sender, EventArgs e)
{
textBox1.Text = calcArea.ToString();
}
}
}
As I mentioned above - you need to pass the method the parameters as you defined them in the method. You don't need Text Changed events for this either - plus they aren't doing anything.
Try changing your code to something like (not real sure of what exactly you named textboxes):
private void btnCalc_Click(object sender, EventArgs e)
{
double width = Convert.ToDouble(txbBreedte.Text);//txbWidth.Text?
double length= Convert.ToDouble(txbLengte.Text);//txbLength.Text?
textBox1.Text = calcArea(length, width).ToString();
}
calcArea.ToString();
when you are calling this, you aren't calling the function, you are referencing the function, not the return
do
calcArea(parameters).ToString();
replace parameters with what you want to calculate.
Related
I have problem, which consists in aesthetic sense, correctly - There is textBox to which i apply true condition of UseSystemPasswordChar.. It's work! But i get bold points. Try to change font size - decreases textbox's field. Below is the code (although why is it here?). Can anyone help, thank you in advance)
public partial class frmRegistr : Form
{
public frmRegistr()
{
InitializeComponent();
}
int counter = 0;
int a = 0;
string b;
private void frmRegistr_Load(object sender, EventArgs e)
{
b = label1.Text;
a = b.Length;
label1.Text = "";
timer1.Start();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
if (counter < a)
{
counter++;
label1.Text = b.Substring(0, counter);
}
else
{
timer1.Stop();
}
}
private void label4_Click(object sender, EventArgs e)
{
timer3.Start();
}
private void label4_MouseHover(object sender, EventArgs e)
{
//if this.MouseLeave
label4.BackColor = Color.FromArgb(((int)(((byte)(154)))), ((int)(((byte)(181)))), ((int)(((byte)(101)))));
}
private void timer2_Tick(object sender, EventArgs e)
{
if (Opacity == 1)
{
timer2.Stop();
}
Opacity += .2;
}
private void timer3_Tick(object sender, EventArgs e)
{
if (Opacity <= 0)
{
this.Close();
}
Opacity -= .2;
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
textBox2.UseSystemPasswordChar = true;
}
}
}
If you want to define your own password character, use property TextBox.PasswordChar. If you want this in a certain font, use Control.Font
As you only have to do this once, do this in the constructor:
public MyForm : Form
{
InitializeComponents(),
this.textBox1.PasswordChar = '_';
this.textBox11.Font = new Font(...)
};
You can also decide to do this using the visual studio designer.
You can setup this in VisualStudio designer, but this is code:
textBox1.PasswordChar = '*';
//* = password character
I have two buttons in my C# windows form application. Button1 and Button2.
i want to use a variable and a list calculated in Button1's event as an input variable in Button2's event. how can I do that? Example:
private void button1_Click(object sender, EventArgs e)
{
int a;
// some steps
// after these steps, assume a gets the value of 5 so a = 5 at this point.
// also there is a list which gets its values after these steps
List<double> parameterValues = new List<double> {
i.GetDouble(), S.GetDouble(), L.GetDouble(),B.GetDouble()
};
}
Here is the code for button2 event, in this I want to be able to use the value of a calculated in button1's code.
private void button2_Click(object sender, EventArgs e)
{
int b = a + 5;
// some code to call the list as well
}
You have to make int a global in order to use it in both buttons.
public int a;
private void button1_Click(object sender, EventArgs e)
{
a = 5;
// some steps
// after these steps, assume a gets the value of 5 so a = 5 at this point.
}
private void button2_Click(object sender, EventArgs e)
{
int b = a + 5;
}
You have a scope issue currently. The value you want to use inside button click 2 must be at least modular to the forms class in order to use in both methods. In this example, "outerValue" is modular and can be accessed by both. Have a read through this article to get a better overview of variable scope.
https://msdn.microsoft.com/en-us/library/ms973875.aspx
public partial class Form1 : Form
{
private int outerValue = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
int a = 5;
outerValue = a + 5;
}
private void button2_Click(object sender, EventArgs e)
{
int b = outerValue + 5;
}
}
I'm making a little something in Windows Form (C# Visual Studio)
But I need to know how I can set an int and then to use it in another event. For example, :
private void BtnYes_Click(object sender, EventArgs e)
{
int yes = 1;
int no = 0;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (yes == 1) {
//EVENT
}
}
When I do this I get some errors. Can anyone help me with this? Or just tell me how to do something like this using a different technique?
U need to use a field for this:
private int _yes;
private void BtnYes_Click(object sender, EventArgs e)
{
_yes = 1;
int no = 0;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (_yes == 1) {
//EVENT
}
}
The variable "yes" you are declaring is only visible in the scope of the method. By making it a field of class, this would make it visible to all methods in the class (when private).
class YourClass
{
private int yes = 1;
private int no = 0;
private void BtnYes_Click(object sender, EventArgs e)
{
//Remove the type declaration here to use the class field instead. If you leave the type declaration, the variable here will be used instead of the class field.
yes = 1;
no = 0;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (yes == 1) {
//EVENT
}
}
}
Hi it might sound a simple task but i am a bit confused here
I have an event :
private void ManReg_Click(object sender, EventArgs e)
{
int store = data[0];
}
and then another event function like :
private void measurementsRead_Click(object sender, EventArgs e)
{
}
I would like to use the variable "store" in the second function. How can I ?
The form is declared as follows :
namespace myRfid
{
public partial class Form1 : Form
{
}
}
You should put variable onto the class level
class ...
{
private int store;
private void ManReg_Click(object sender, EventArgs e)
{
store = data[0];
}
private void measurementsRead_Click(object sender, EventArgs e)
{
// use store
}
}
You can set it as shown in below code in your class but private as it would be used within class only as shown below :-
private int store;
private void ManReg_Click(object sender, EventArgs e)
{
this.store = data[0];
}
private void measurementsRead_Click(object sender, EventArgs e)
{
this.store//// however you want to use
}
I was making a calculator using Windows Forms Application and I'm currently stumped.
Here's my application so far, it can only do addition, but any amount of numbers.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Calculator
{
public partial class Form1 : Form
{
int num;
int answer = 0;
int i;
public Form1()
{
InitializeComponent();
}
private void plusButton_Click(object sender, EventArgs e)
{
answer += System.Convert.ToInt32(textBox1.Text);
ClearTextbox();
}
private void minusButton_Click(object sender, EventArgs e)
{
//answer -= System.Convert.ToInt32(textBox1.Text);
//ClearTextbox();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void divideButton_Click(object sender, EventArgs e)
{
}
private void multiplyButton_Click(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
num = 1;
displayInt(num);
}
private void button2_Click(object sender, EventArgs e)
{
num = 2;
displayInt(num);
}
private void button3_Click(object sender, EventArgs e)
{
num = 3;
displayInt(num);
}
private void button4_Click(object sender, EventArgs e)
{
num = 4;
displayInt(num);
}
private void button5_Click(object sender, EventArgs e)
{
num = 5;
displayInt(num);
}
private void button6_Click(object sender, EventArgs e)
{
num = 6;
displayInt(num);
}
private void button7_Click(object sender, EventArgs e)
{
num = 7;
displayInt(num);
}
private void button8_Click(object sender, EventArgs e)
{
num = 8;
displayInt(num);
}
private void button9_Click(object sender, EventArgs e)
{
num = 9;
displayInt(num);
}
private void button0_Click(object sender, EventArgs e)
{
num = 0;
displayInt(num);
}
private void resetButton_Click(object sender, EventArgs e)
{
ClearTextbox();
num = 0;
answer = 0;
}
public void displayInt(int num)
{
textBox1.Text = textBox1.Text + num.ToString();
}
public void ClearTextbox()
{
textBox1.Clear();
}
public void DisplayAnswer(int answer)
{
answer += System.Convert.ToInt32(textBox1.Text); //Answer = Answer + Textbox Stuff
textBox1.Clear();
textBox1.Text = answer.ToString();
}
private void equalsButton_Click(object sender, EventArgs e)
{
DisplayAnswer(answer);
num = 0;
answer = 0;
}
}
}
I'm not sure if there's a way to wait until another number key is pressed, and then do x + y. I heard about event handlers, but it's quite a vague topic to me.
Here's a picture: http://img196.imageshack.us/img196/4127/12464e13e5154a949a9a457.png
*I want it to be able to do operations to more then 2 numbers.
Thanks!
Your code is a bit messy and it's hard to figure out your intent. Help with this code will be long in coming. I suggest following this simple calculator tutorial. It explains the concepts and steps very nicely, including button events.
You would need to have a "+"-button and a "="-button. Then your calculator has two number fields and two states:
1) Entering first number
2) Entering second number
You could have a variable saying which number is being entered.
int field = 1;
When pressing a digit button, this digit has to be appended to the corresponding field.
When pressing the "+"-button change the state to
field = 2;
Now the digits will be appended to the second field.
When you press the "="-button, convert the two numbers, which are still strings now, to int's, add them and output them to a result field. Set the state back to
field = 1;
Also a "clear"-button would clear the fields and set field = 1;
Note: As Paul Sasik says, your code is a bit messy. I would help, if you gave your buttons meaningful names like btnDigit1, btnDigit2, btnPlus, btnEquals, btnClear. When you double-click a button in the designer an event handler like btnPlus_Click will be created. This is clearer than button17_Click.