Hi,
I have a calculator.cs (it is an userControl). I'm using it in the file iskonto.cs named which is windows form. When the calculator's textbox changed, i want to use the textbox value in iskonto.cs... Please help me with this issue. Here is my code;
calculator.cs
iskonto indirim = new iskonto();
private void txtResult_TextChanged(object sender, EventArgs e)
{
indirim.iskontoGoster(txtResult.Text.ToString());
}
iskonto.cs
public void iskontoGoster(string textBoxValue)
{
if (textBoxValue != null || textBoxValue != "")
{
button1.Text = "% " + textBoxValue + " iskonto yap. Sonuç = ";
button1.BackColor = System.Drawing.Color.Navy;
button1.ForeColor = System.Drawing.Color.Yellow;
}
}
I have a calculator.cs (it is an userControl). I'm using it in the file iskonto.cs named which is windows form. When the calculator's textbox changed, i want to use the textbox value in iskonto.cs... Please help me with this issue. Here is my code;
calculator.cs
iskonto indirim = new iskonto();
private void txtResult_TextChanged(object sender, EventArgs e)
{
indirim.iskontoGoster(txtResult.Text.ToString());
}
iskonto.cs
public void iskontoGoster(string textBoxValue)
{
if (textBoxValue != null || textBoxValue != "")
{
button1.Text = "% " + textBoxValue + " iskonto yap. Sonuç = ";
button1.BackColor = System.Drawing.Color.Navy;
button1.ForeColor = System.Drawing.Color.Yellow;
}
}
Nilanka DharmadasaPosted Nov 11, 2009, 5:57 AM
Now I got to know your problem. Keep your calculator class as it is. Then change iskonto class as given below.
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 WindowsFormsApplication1
{
public partial class iskonto : Form
{
public iskonto()
{
InitializeComponent();
}
private void CalValueChanged(string textBoxValue)
{
if (textBoxValue != null || textBoxValue != "")
{
button1.Text = "% " + textBoxValue + " iskonto yap. Sonuç = ";
button1.BackColor = System.Drawing.Color.Navy;
button1.ForeColor = System.Drawing.Color.Yellow;
}
}
}
}
Then go to designer of 'iskonto ' class. Then drag a 'calculator' control from the toolbox to 'iskonto ' form. Then in 'calculator' control added (calculator1 or something) if you go to properties > events list, you will see an event called 'MyTextChanged'. Then set 'CalValueChanged' for that.
That's all you ahve to do. Run the application and check and let me know.
Nilanka DharmadasaPosted Nov 11, 2009, 6:09 AM
finally, do not forget to mark my aswer as accepted, if it helped you. :)
billypostmanPosted Nov 11, 2009, 6:06 AM
Thank you very very much, now it's working great... You are my hero. :)
Then go to designer of 'iskonto ' class. Then drag a 'calculator' control from the toolbox to 'iskonto ' form. Then in 'calculator' control added (calculator1 or something) if you go to properties > events list, you will see an event called 'MyTextChanged'. Then set 'CalValueChanged' for that.
This is the best suggestion I've ever read :)
I marked your answer, thanks a lot again.
billypostmanPosted Nov 11, 2009, 5:12 AM
calculator.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dynamicGridFilter;
namespace WindowsFormsApplication1
{
public partial class calculator : UserControl
{
private int opMain = 0; /// Stores the value of the operation (1-4)
private double mainNum1 = 0; ///Stores the value of the first number
private double mainNum2 = 0; ///Stores the value of the second number
private bool isSecond = false; //Boolean value used to determine if input is the second or first number.
private bool isDone = false; ///Boolean value used to determine if the equals key was pressed.
private bool isDecimal = false; //Boolean value used to determine if there is a decimal in the number.
public calculator()
{
InitializeComponent();
}
public void setText(String textset)
{
if (textset.Equals("clear")) //If the user hits the clear button
{
txtResult.Text = ""; //Clear the text and reset the boolean variables.
isDone = false;
isSecond = false;
isDecimal = false;
}
else
{
if (isSecond) //Determine if the number being entered is the begining of the second number. If it is:
{
txtResult.Text = textset; //Start the text over and set the first # to what the user enters
isSecond = false; //So Calculator knows to continue the # rather than making a new one.
isDecimal = false;
}
else
{
if (isDone) //isDone lets the program know that the user just hit "=" and if they press another # to start a new number.
{
txtResult.Text = textset;
isDone = false; //Set isDone to false so that the number just started is added on to and a new # is not started.
}
else
txtResult.Text += textset; //Simply add on to the existing #.
}
}
}
public void Calc(double num1, double num2, int op)
{
double answer = 0;//Initialize answer to 0;
switch (op) //Determine which operation to perform depending on the value of "op"
{
case 1:
answer = num1 + num2;
break;
case 2:
answer = num1 - num2;
break;
case 3:
answer = num1 * num2;
break;
case 4:
answer = num1 / num2;
break;
}
setText(answer.ToString()); //Show the answer in the textbox;
}
private void setOperator(int operation)
{
if (txtResult.Text.Length > 0)//Make sure that the user entered a number
{
opMain = operation; //Store the operation
mainNum1 = double.Parse(txtResult.Text); //Store the value of the first number
isSecond = true; //Let the program know to begin the second number
isDone = false; //If a operator button is pressed before a new number, let the program know to start a new number.
}
}
private void setDecimal()
{
if (!isDecimal)//Check for existing decimal
{
setText(","); //Add decimal
isDecimal = true; //Let program know decimal has been added
}
}
private void doEquals()
{
mainNum2 = double.Parse(txtResult.Text); //Set the value of the second number
setText("clear"); //Clear the textbox
Calc(mainNum1, mainNum2, opMain); //Call the Calc method
isDone = true; //Set isDone to true so that if another # is pressed, the program will begin a new number
}
private void clearButton_Click(object sender, EventArgs e)
{
isSecond = false;
setText("clear");
}
private void divideButton_Click(object sender, EventArgs e)
{
setOperator(4);
}
private void multiplyButton_Click(object sender, EventArgs e)
{
setOperator(3);
}
private void minusButton_Click(object sender, EventArgs e)
{
setOperator(2);
}
private void plusButton_Click(object sender, EventArgs e)
{
setOperator(1);
}
private void equalButton_Click(object sender, EventArgs e)
{
doEquals();
}
private void button0_Click(object sender, EventArgs e)
{
setText("0");
}
private void button1_Click(object sender, EventArgs e)
{
setText("1");
}
private void button2_Click(object sender, EventArgs e)
{
setText("2");
}
private void button3_Click(object sender, EventArgs e)
{
setText("3");
}
private void button4_Click(object sender, EventArgs e)
{
setText("4");
}
private void button5_Click(object sender, EventArgs e)
{
setText("5");
}
private void button6_Click(object sender, EventArgs e)
{
setText("6");
}
private void button7_Click(object sender, EventArgs e)
{
setText("7");
}
private void button8_Click(object sender, EventArgs e)
{
setText("8");
}
private void button9_Click(object sender, EventArgs e)
{
setText("9");
}
private void button000_Click(object sender, EventArgs e)
{
setText("000");
}
private void kommaButton_Click(object sender, EventArgs e)
{
setDecimal();
}
public delegate void CalTextChanged(string Result);
public event CalTextChanged MyTextChanged;
private void txtResult_TextChanged(object sender, EventArgs e)
{
if (this.MyTextChanged != null)
this.MyTextChanged(txtResult.Text);
}
}
}
iskonto.cs
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 WindowsFormsApplication1
{
public partial class iskonto : Form
{
calculator mycal = new calculator();
public iskonto()
{
InitializeComponent();
mycal.MyTextChanged += new calculator.CalTextChanged(CalValueChanged);
}
private void CalValueChanged(string textBoxValue)
{
if (textBoxValue != null || textBoxValue != "")
{
button1.Text = "% " + textBoxValue + " iskonto yap. Sonuç = ";
button1.BackColor = System.Drawing.Color.Navy;
button1.ForeColor = System.Drawing.Color.Yellow;
}
}
}
}
Edit: Should I do something in the calculator.cs [Design] file (properties window, events tab ? ) for the CalTextChanged event.
Nilanka DharmadasaPosted Nov 11, 2009, 5:07 AM
public class calcualtor {
public delegate void CalTextChanged(string Result);
public event CalTextChanged MyTextChanged;
private void txtResult_TextChanged(object sender, EventArgs e)
{
if (this.MyTextChanged != null)
this.MyTextChanged(txtResult.Text);
}
}
Nilanka DharmadasaPosted Nov 11, 2009, 4:51 AM
This line,
mycal.MyTextChanged += new calculator.CalTextChanged(CalValueChanged);
should be executed only once. That's y I asked you to put it like this.
public iskonto()
{
InitializeComponent();
mycal.MyTextChanged += new calculator.CalTextChanged(CalValueChanged);
}
It only binds the event. After that that line's duty is over. When the text is changed that line should not be executed. So there is nothing wrong in the above code.
Did you try with that code? Change the text in calulator text box and see whether button text is changed. If you still get an error, the reason should be somewhere else. Paste your complete code here. Then i can look into it.
billypostmanPosted Nov 11, 2009, 3:47 AM
When I put that line into the constructor. It works only once. After changing textBox value, it has to stop in that line. But it's not stopping at the breakpoint. That's why it's null, so the if statement is not working.
Edit for niki d: Now I recognized, you said put the breakpoint in the blue line. I also put there, but it's not entering the if statement. Because this.MyTextChanged comes null.
Dear maggogg d,
Your solution is same as niki's solution. Form load works only once. It also don't stop at the breakpoint.
Thank you very much for your answers...
Nilanka DharmadasaPosted Nov 11, 2009, 2:36 AM
Hi friend,
You only have to change the position of red color line as i mentioned in the previous post. Put a break point to blue color line and check whether that line is hit when you change the text value.
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 WindowsFormsApplication1
{
public partial class iskonto : Form
{
calculator mycal = new calculator();
public iskonto()
{
InitializeComponent();
mycal.MyTextChanged += new calculator.CalTextChanged(CalValueChanged);
}
private void CalValueChanged(string textBoxValue)
{
if (textBoxValue != null || textBoxValue != "")
{
button1.Text = "% " + textBoxValue + " iskonto yap. Sonuç = ";
button1.BackColor = System.Drawing.Color.Navy;
button1.ForeColor = System.Drawing.Color.Yellow;
}
}
}
}
calculator.cs
public delegate void CalTextChanged(string Result);
public event CalTextChanged MyTextChanged;
private void txtResult_TextChanged(object sender, EventArgs e)
{
if (this.MyTextChanged != null)
this.MyTextChanged(txtResult.Text);
}
naura paxPosted Nov 11, 2009, 2:10 AM
just put this line : mycal.MyTextChanged += new calculator.CalTextChanged(CalValueChanged);
in form_load event.
Nilanka DharmadasaPosted Nov 11, 2009, 2:04 AM
Don't put that line outside the method. Put it here.
public iskonto()
{
InitializeComponent();
mycal.MyTextChanged += new calculator.CalTextChanged(CalValueChanged);
}
Now try and let me know.
billypostmanPosted Nov 11, 2009, 1:35 AM
Thank you for your patience. But here is the problem; I can not use that line outside of method.
mycal.MyTextChanged += new calculator.CalTextChanged(CalValueChanged);
In this line, I put a breakpoint. When I change the textbox value. It's not stopping at the breakpoint. It never sees that line. Here's my code...
iskonto.cs
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 WindowsFormsApplication1
{
public partial class iskonto : Form
{
calculator mycal = new calculator();
public iskonto()
{
InitializeComponent();
}
mycal.MyTextChanged += new calculator.CalTextChanged(CalValueChanged);
private void CalValueChanged(string textBoxValue)
{
if (textBoxValue != null || textBoxValue != "")
{
button1.Text = "% " + textBoxValue + " iskonto yap. Sonuç = ";
button1.BackColor = System.Drawing.Color.Navy;
button1.ForeColor = System.Drawing.Color.Yellow;
}
}
}
}
That bold line has 3 errors.
Error 1 Invalid token '+=' in class, struct, or interface member declaration
Error 2 Invalid token '(' in class, struct, or interface member declaration
Error 3 Invalid token ')' in class, struct, or interface member declaration
calculator.cs
public delegate void CalTextChanged(string Result);
public event CalTextChanged MyTextChanged;
private void txtResult_TextChanged(object sender, EventArgs e)
{
if (this.MyTextChanged != null) this.MyTextChanged(txtResult.Text);
}
Nilanka DharmadasaPosted Nov 10, 2009, 10:49 PM
Did you write all these in your iskonto class? If you wrote the code which is in red, it cannot be null.
calculator mycal = new calculator();
//I hope you create an instance of this class inside iskonto class.
mycal.MyTextChanged += new calculator.CalTextChanged(CalValueChanged);
I guess, I cudnt change this part. That might be the reaoson. change your code as follows.
private void CalValueChanged(string pResult)
{
if (pResult != null || pResult != "")
{
button1.Text = "% " + pResult + " iskonto yap. Sonuç = ";
button1.BackColor = System.Drawing.Color.Navy;
button1.ForeColor = System.Drawing.Color.Yellow;
}
}
I hope this will solve your problem.
If this helped you please do not forget to accept this answer..
billypostmanPosted Nov 10, 2009, 9:20 AM
private void txtResult_TextChanged(object sender, EventArgs e)
{
if (this.MyTextChanged != null) this.MyTextChanged(txtResult.Text);
}
MyTextChanged always comes null. That's why; it can not enter the if statement.
Why could it be? I made TextChanged event; txtResult_TextChanged from the calculator.cs [Design]
Nilanka DharmadasaPosted Nov 10, 2009, 8:56 AM
As I see, the best way to do this is using delegates. It's not a diffult thing. Simply use my code.
Ignore my previou reply. One line is missing in that Use this.
calculator.cs
public delegate void CalTextChanged(string Result);
public event CalTextChanged MyTextChanged;
private void txtResult_TextChanged(object sender, EventArgs e)
{
if (this.MyTextChanged != null) this.MyTextChanged(txtResult.Text);
}
iskonto.cs
----------
calculator mycal = new calculator();
//I hope you create an instance of this class inside iskonto class.
mycal.MyTextChanged += new calculator.CalTextChanged(CalValueChanged);
private void CalValueChanged(string pResult)
{
if (textBoxValue != null || textBoxValue != "")
{
button1.Text = "% " + pResult + " iskonto yap. Sonuç = ";
button1.BackColor = System.Drawing.Color.Navy;
button1.ForeColor = System.Drawing.Color.Yellow;
}
}
If you have any problem let me know pls.
Nilanka DharmadasaPosted Nov 10, 2009, 8:54 AM
As I see, the best way to do this is using delegates. It's not a diffult thing. Simply use my code.
calculator.cs
public delegate void CalTextChanged(string Result);
public event CalTextChanged MyTextChanged;
private void txtResult_TextChanged(object sender, EventArgs e)
{
if (this.MyTextChanged != null) this.MyTextChanged(txtResult.Text);
}
iskonto.cs
----------
calculator mycal = new calculator();
//I hope you create an instance of this class inside iskonto class.
mycal.MyTextChanged += new calculator.CalTextChanged(DocumentList_DocCountChanged);
private void CalValueChanged(string pResult)
{
if (textBoxValue != null || textBoxValue != "")
{
button1.Text = "% " + pResult + " iskonto yap. Sonuç = ";
button1.BackColor = System.Drawing.Color.Navy;
button1.ForeColor = System.Drawing.Color.Yellow;
}
}
If you have any problem let me know pls.
billypostmanPosted Nov 10, 2009, 8:37 AM
Thanks a lot for your quick answer. I hope you know the solution. :)
Nilanka DharmadasaPosted Nov 10, 2009, 8:31 AM
1. You want to just get the current value of the text box in Calculatar class
Or
2. You want to get the current value of the text box as it changed (Whenever the value of text box is changed you want to get the changed value)
From above two, what is your requirement ? Depending upon your need, the code varies.