I have a form application in C#. I need to take radiobutton values in textbox.i know the methods are private, i change public but it didnt work. i need to take value1,value2,value3 and value4 with button press. Can anybody tell me a way for getting values in textboxs....
private void groupBox1_Enter(object sender, EventArgs e)
{
double value1;
if (radioButton1.Checked)
value1 = 0.9;
else if (radioButton2.Checked)
value1 = 0.8;
else if (radioButton3.Checked)
value1 = 0.7;
else if (radioButton4.Checked)
value1 = 0.3;
else if (radioButton5.Checked)
value1 = 0.5;
else
MessageBox.Show("Oda Tipi girilmedi.");
}
private void groupBox2_Enter(object sender, EventArgs e)
{
double value2;
if (radioButton6.Checked)
value2 = 1;
else if (radioButton7.Checked)
value2 = 0.8;
else if (radioButton8.Checked)
value2 = 0.6;
else
MessageBox.Show("İzolasyon Tipi girilmedi.");
}
private void groupBox3_Enter(object sender, EventArgs e)
{
double value3;
if (radioButton9.Checked)
value3 = 0.9;
else if (radioButton10.Checked)
value3 = 1;
else
MessageBox.Show("Cam Tipi girilmedi.");
}
private void groupBox4_Enter(object sender, EventArgs e)
{
double value4;
if (radioButton11.Checked)
value4 = 1;
else if (radioButton12.Checked)
value4 = 0.9;
else
MessageBox.Show("Formül katsayısı girilmedi.");
}
private void button1_Click(object sender, EventArgs e)
{
textBox5.Text=Convert.ToString(value1*value2*value3*value4*(Convert.ToDouble(textBox2.Text))*(Convert.ToDouble(textBox3.Text))*(Convert.ToDouble(textBox4.Text)));
}
You can either move the variables (value1 etc.) to the class scope or put everything in the button1_Click event handler like #SteveDanner has suggested or you may write a more generic solution. It can be very easily extendend if you create more options (RadioButtons).
// Store values for each RadioButton in a dictionary.
private Dictionary<RadioButton, double> values =
new Dictionary<RadioButton, double>();
private Dictionary<GroupBox, string> messages =
new Dictionary<GroupBox, string>();
public Form1()
{
InitializeComponent();
// Associate values with radio buttons.
values[radioButton1] = 0.9;
// repeat the same for others...
// Associate values messages with group boxes.
messages[groupBox1] = "Oda Tipi girilmedi.";
// repeat the same for others...
}
#region GroupBox.Enter event handlers.
private void groupBox1_Enter(object sender, EventArgs e)
{
RadioButton radioButton = GetSelectedRadioButton(sender as GroupBox);
if (radioButton == null)
{
MessageBox.Show(messages[sender as GroupBox]);
}
}
// Here you can either repeat the same for other group boxes
// or simply assign this event hander to all of them.
// It will get the right message for each group.
#endregion
// Gets the selected radio button from the specified group.
private void RadioButton GetSelectedRadioButton(GroupBox groupBox)
{
RadioButton radioButton =
groupBox
.Controls
.OfType<RadioButton>()
.Where(rb => rb.Checked)
.FirstOrDefault();
return radioButton;
}
// Gets selected value from the specified group.
private double GetSelectedValue(GroupBox groupBox)
{
RadioButton radioButton = GetSelectedRadioButton(groupBox);
if (radioButton == null)
{
// Nothing selected yet.
return double.NaN;
}
else
{
// Get the value from the dictinary.
return values[radioButton];
}
}
private void button1_Click(object sender, EventArgs e)
{
// Get the selected values.
double value1 = GetSelectedValue(groupBox1);
double value2 = GetSelectedValue(groupBox2);
double value3 = GetSelectedValue(groupBox3);
double value4 = GetSelectedValue(groupBox4);
// Check other values in the same way.
if (double.IsNaN(value1))
{
MessageBox.Show(message[groupBox1]);
}
textBox5.Text = Convert.ToString(
value1
* value2
* value3
* value4
* (Convert.ToDouble(textBox2.Text))
* (Convert.ToDouble(textBox3.Text))
* (Convert.ToDouble(textBox4.Text)));
}
The problem is that your values being created from the RadioButtons are local variables to the method handlers. You need to remove your groupBox_Enter handlers and just handle the button1_Click event like so:
private void button1_Click(object sender, EventArgs e)
{
double value1;
if (radioButton1.Checked)
value1 = 0.9;
else if (radioButton2.Checked)
value1 = 0.8;
else if (radioButton3.Checked)
value1 = 0.7;
else if (radioButton4.Checked)
value1 = 0.3;
else if (radioButton5.Checked)
value1 = 0.5;
else
{
MessageBox.Show("Oda Tipi girilmedi.");
return; //not sure if this is what you want here?
}
double value2;
if (radioButton6.Checked)
value2 = 1;
else if (radioButton7.Checked)
value2 = 0.8;
else if (radioButton8.Checked)
value2 = 0.6;
else
{
MessageBox.Show("Izolasyon Tipi girilmedi.");
return; //not sure if this is what you want here?
}
double value3;
if (radioButton9.Checked)
value3 = 0.9;
else if (radioButton10.Checked)
value3 = 1;
else
{
MessageBox.Show("Cam Tipi girilmedi.");
return; //not sure if this is what you want here?
}
double value4;
if (radioButton11.Checked)
value4 = 1;
else if (radioButton12.Checked)
value4 = 0.9;
else
{
MessageBox.Show("Formül katsayisi girilmedi.");
return; //not sure if this is what you want here?
}
textBox5.Text=Convert.ToString(value1*value2*value3*value4*(Convert.ToDouble(textBox2.Text))*(Convert.ToDouble(textBox3.Text))*(Convert.ToDouble(textBox4.Text)));
}
Related
I am new to C# and classes. I was testing on making a datagridview where the user can right click and a contextmenustrip pops. Now my question is the following.
I made my code to add the text to the textbox and increase the textbox if it is not empty.With help from this post.
private void TsmItem_Click(object sender, EventArgs e)
{
int rowindex = dgvResults.CurrentCell.RowIndex;
int columnindex = dgvResults.CurrentCell.ColumnIndex;
double resultText;
if (string.IsNullOrEmpty(Textbox.Text))
{
Textbox.Text = dgvResults.Rows[rowindex].Cells[columnindex].Value.ToString();
}
else
{
string selectedCell;
selectedCell = dgvResults.Rows[rowindex].Cells[columnindex].Value.ToString();
ResultText = Convert.ToDouble(Textbox.Text) + Convert.ToDouble(selectedCell);
Textbox.Text = Convert.ToString(ResultText);
}
}
Is there a way to only have this code only 1 time and call it every time I need it. So that I only have to change the textboxes.Text? Otherwise I need to copy and paste this code more than 10 times.
What I tried
private void TsmItem_Click(object sender, EventArgs e)
{
Textbox.Text = RightMouseClick(Textbox.Text);
}
private void RightMouseClick(string txtResult)
{
int rowindex = dgvResults.CurrentCell.RowIndex;
int columnindex = dgvResults.CurrentCell.ColumnIndex;
if (string.IsNullOrEmpty(txtResult))
{
txtResult = dgvResults.Rows[rowindex].Cells[columnindex].Value.ToString();
}
else
{
string selectedCell;
double resultText;
selectedCell = dgvResults.Rows[rowindex].Cells[columnindex].Value.ToString();
resultaat = Convert.ToDouble(txtResult) + Convert.ToDouble(selectedCell);
txtResult = Convert.ToString(resultText);
}
}
Fix
private string RightMouseClick(TextBox txtResult)
{
int rowindex = dgvResults.CurrentCell.RowIndex;
int columnindex = dgvResults.CurrentCell.ColumnIndex;
if (string.IsNullOrEmpty(txtResult.Text))
{
return txtResult.Text = dgvResults.Rows[rowindex].Cells[columnindex].Value.ToString();
}
else
{
string selectedCell;
double resultaat;
selectedCell = dgvResults.Rows[rowindex].Cells[columnindex].Value.ToString();
resultaat = Convert.ToDouble(txtResult.Text) + Convert.ToDouble(selectedCell);
return txtResult.Text = Convert.ToString(resultaat);
}
}
You can use the textbox as a parameter
private void RightMouseClick(TextBox txtBox)
{
int rowindex = dgvResults.CurrentCell.RowIndex;
int columnindex = dgvResults.CurrentCell.ColumnIndex;
if (string.IsNullOrEmpty(txtResult))
{
txtBox.Text = dgvResults.Rows[rowindex].Cells[columnindex].Value.ToString();
}
else
{
string selectedCell;
double resultText;
selectedCell = dgvResults.Rows[rowindex].Cells[columnindex].Value.ToString();
resultaat = Convert.ToDouble(txtBox.Text) + Convert.ToDouble(selectedCell);
txtBox.Text = Convert.ToString(resultText);
}
}
You should ensure that you also checking to make sure that the textbox isn't NULL
So I have an invoice total form that should calculate the percentage discount among other things. I'm adding a second form to the code that allows you to change the sales tax I got it to populate the form and actually work with no errors but I can't get it to move the data from the textbox on frmSalesTax to the txtSalesTax.Text in frmInvoiceTotal.
frmInvoiceTotal Code:
public frmInvoiceTotal()
{
InitializeComponent();
}
frmSalesTax percent = new frmSalesTax();
private void btnCalculate_Click(object sender, EventArgs e)
{
decimal productTotal = Convert.ToDecimal(txtProductTotal.Text);
decimal salesTax = (7.75m/100m) * productTotal;
decimal discountPercent = .0m;
if (productTotal < 100)
discountPercent = .0m;
else if (productTotal >= 100 && productTotal < 250)
discountPercent = .1m;
else if (productTotal >= 250)
discountPercent = .25m;
decimal discountAmount = (productTotal + salesTax) * discountPercent;
decimal subtotal = productTotal - discountAmount;
decimal invoiceTotal = (subtotal + salesTax) - discountAmount;
txtSubtotal.Text = subtotal.ToString("c");
txtSalesTax.Text = salesTax.ToString("c");
txtDiscountPercent.Text = discountPercent.ToString("p1");
txtDiscountAmount.Text = discountAmount.ToString("c");
txtTotal.Text = invoiceTotal.ToString("c");
txtProductTotal.Focus();
}
private void btnChange_Click(object sender, EventArgs e)
{
percent.salesTax = txtSalesTax.Text;
switch (percent.ShowDialog())
{
case DialogResult.OK:
txtSalesTax.Text = percent.salesTax;
break;
}
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
invoiceTotal GUI:
frmSalesTax Code:
public partial class frmSalesTax : Form
{
public string salesTax
{
get;
set;
}
public frmSalesTax()
{
InitializeComponent();
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnOk_Click(object sender, EventArgs e)
{
this.salesTax = txtPercent.Text;
txtPercent.Text = "";
Hide();
}
I know I'm missing something but I can't figure out what it is.
salesTax GUI:
You've got the right idea making a property on frmSalesTax to communicate...but you're not really using it.
In your frmInvoiceTotal, you need to send the current value to frmSalesTax.salesTax and then handle the results of the percent dialog returning DialogResult.OK:
private void btnChange_Click(object sender, EventArgs e)
{
percent.salesTax = txtSalesTax.Text; //--> send current value to frmSalesTax
switch ( percent.ShowDialog() ) //--> ShowDialog will return the DialogResult of the pressed button
{
case DialogResult.OK:
txtSalesTax.Text = percent.salesTax; //--> update with new value from frmSalesTax
break;
}
}
...and, in your frmSalesTax, you need to put the txtPercent.Text into the salesTax property when the user clicks the OK button:
private void btnOk_Click(object sender, EventArgs e)
{
this.salesTax = txtPercent.Text; //--> frmInvoiceTotal will read this after the OK button is clicked
txtPercent.Text = "";
Hide();
}
Important: you have to make sure the frmSalesTax buttons have their DialogResult set, so that frmInvoiceTotal.btnOk_Click knows that it's okay to get the value:
Edit
The property (in frmSalesTax) needs to not be based on the form's text values...because you're setting that to "" when the form hides. This is what you want for the property:
public string salesTax
{
get;
set;
}
This would go with the other changes I mentioned earlier.
Edit 2
It's easy to get frustrated. There are a lot of moving pieces, and I can understand how the eyes can cross. Here's the crux of the issue - your calculation is trashing things on you;-)
These lines in the btnCalculate_Click:
decimal salesTax = (7.75m/100m) * productTotal;
decimal discountPercent = .0m;
//...
txtSalesTax.Text = salesTax.ToString("c");
txtDiscountPercent.Text = discountPercent.ToString("p1");
...should be the initial values and come in the form's initialization code:
public frmInvoiceTotal()
{
InitializeComponent();
decimal salesTax = (7.75m/100m) * productTotal;
decimal discountPercent = .0m;
txtSalesTax.Text = salesTax.ToString("c"); //--> the initial value
txtDiscountPercent.Text = discountPercent.ToString("p1");
}
...and then, the calculation should not re-populate txtSalesTax.Text or txtDiscountPercent.Text. The txtSalesTax.Text might be update from showing frmSalesTax, and I'm guessing you're gonna make another form to override discount percent at some point.
private void btnCalculate_Click(object sender, EventArgs e)
{
decimal productTotal = Convert.ToDecimal(txtProductTotal.Text);
decimal salesTax = Convert.ToDecimal(salesTax.Text) * productTotal; //--> if it got changed in frmSalesTax
decimal discountPercent = .0m;
if (productTotal < 100)
discountPercent = .0m;
else if (productTotal >= 100 && productTotal < 250)
discountPercent = .1m;
else if (productTotal >= 250)
discountPercent = .25m;
decimal discountAmount = (productTotal + salesTax) * discountPercent;
decimal subtotal = productTotal - discountAmount;
decimal invoiceTotal = (subtotal + salesTax) - discountAmount;
txtSubtotal.Text = subtotal.ToString("c");
//txtSalesTax.Text = salesTax.ToString("c"); //--> don't do this...it steps on what came from frmSalesTax
//txtDiscountPercent.Text = discountPercent.ToString("p1"); //--> when you add another form to override this
txtDiscountAmount.Text = discountAmount.ToString("c");
txtTotal.Text = invoiceTotal.ToString("c");
txtProductTotal.Focus();
}
I bet this get you a lot closer :-)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am trying to make a simple visual calculator and if you press 1 on it it currently just adds the number to it that would mean that 1+1 is 2. I want it to be 11. How do I do this? How will my code look after I did it?
{
double value1 = 0;
double value2 = 0;
double finalResult = 0;
double output = 0;
bool second = false;
bool plus = false;
bool minus = false;
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
if (second == false)
{
value1 = value1 + 1;
output = value1 * 10 + 1;
textresult.Text = output.ToString();
}
else if (second == true)
{
value2 = value2 + 1;
output = value2;
textresult.Text = output.ToString();
}
}
private void button2_Click(object sender, RoutedEventArgs e)
{
if (second == false)
{
value1 = value1 + 2;
output = value1;
textresult.Text = output.ToString();
}
else if (second == true)
{
value2 = value2 + 2;
output = value2;
textresult.Text = output.ToString();
}
}
private void button3_Click(object sender, RoutedEventArgs e)
{
if (second == false)
{
value1 = value1 + 3;
output = value1;
textresult.Text = output.ToString();
}
else if (second == true)
{
value2 = value2 + 3;
output = value2;
textresult.Text = output.ToString();
}
}
private void button4_Click(object sender, RoutedEventArgs e)
{
if (second == false)
{
value1 = value1 + 4;
output = value1;
textresult.Text = output.ToString();
}
else if (second == true)
{
value2 = value2 + 4;
output = value2;
textresult.Text = output.ToString();
}
}
private void button5_Click(object sender, RoutedEventArgs e)
{
if (second == false)
{
value1 = value1 + 5;
output = value1;
textresult.Text = output.ToString();
}
else if (second == true)
{
value2 = value2 + 5;
output = value2;
textresult.Text = output.ToString();
}
}
private void button6_Click(object sender, RoutedEventArgs e)
{
if (second == false)
{
value1 = value1 + 6;
output = value1;
textresult.Text = output.ToString();
}
else if (second == true)
{
value2 = value2 + 6;
output = value2;
textresult.Text = output.ToString();
}
}
private void button7_Click(object sender, RoutedEventArgs e)
{
if (second == false)
{
value1 = value1 + 7;
output = value1;
textresult.Text = output.ToString();
}
else if (second == true)
{
value2 = value2 + 7;
output = value2;
textresult.Text = output.ToString();
}
}
private void button8_Click(object sender, RoutedEventArgs e)
{
if (second == false)
{
value1 = value1 + 8;
output = value1;
textresult.Text = output.ToString();
}
else if (second == true)
{
value2 = value2 + 8;
output = value2;
textresult.Text = output.ToString();
}
}
private void button9_Click(object sender, RoutedEventArgs e)
{
if (second == false)
{
value1 = value1 + 9;
output = value1;
textresult.Text = output.ToString();
}
else if (second == true)
{
value2 = value2 + 9;
output = value2;
textresult.Text = output.ToString();
}
}
private void buttonClear_Click(object sender, RoutedEventArgs e)
{
//remember to add all the other values :)
value1 = 0;
value2 = 0;
finalResult = 0;
output = 0;
second = false;
plus = false;
minus = false;
textresult.Text = output.ToString();
}
private void buttonPlus_Click(object sender, RoutedEventArgs e)
{
output = 0;
textresult.Text = output.ToString();
second = true;
plus = true;
}
private void buttonMinus_Click(object sender, RoutedEventArgs e)
{
output = 0;
textresult.Text = output.ToString();
second = true;
minus = true;
}
private void buttonIs_Click(object sender, RoutedEventArgs e)
{
if (plus == true)
{
finalResult = value1 + value2;
output = finalResult;
textresult.Text = output.ToString();
}
else if (minus == true)
{
finalResult = value1 - value2;
output = finalResult;
textresult.Text = output.ToString();
}
}
}
You can convert int - string with this.
int i = 1;
string s = Convert.ToString(i);
Just wanted to share a some ideas/recommendations to simplify your code
enum Operators
{
NewOperation = -1,
NotSet = 0,
Plus = 1,
Minus = 2
//...
};
int value1, value2;
const string RESET_VALUE = "0";
Operators operation;
public MainWindow() { InitializeComponent(); }
private void MainWindow_Load(object sender, EventArgs e) { textresult.Text = RESET_VALUE; }
private void button1_Click(object sender, EventArgs e) { AppendNumber(1); }
private void button2_Click(object sender, EventArgs e) { AppendNumber(2); }
private void button3_Click(object sender, EventArgs e) { AppendNumber(3); }
private void button4_Click(object sender, EventArgs e) { AppendNumber(4); }
private void button5_Click(object sender, EventArgs e) { AppendNumber(5); }
private void button6_Click(object sender, EventArgs e) { AppendNumber(6); }
private void button7_Click(object sender, EventArgs e) { AppendNumber(7); }
private void button8_Click(object sender, EventArgs e) { AppendNumber(8); }
private void button9_Click(object sender, EventArgs e) { AppendNumber(9); }
private void button0_Click(object sender, EventArgs e) { AppendNumber(0); }
private void AppendNumber(int selected_number)
{
if (operation == Operators.NewOperation)
{
operation = Operators.NotSet;
textresult.Text = $"{selected_number}";
}
else if (textresult.Text.StartsWith("0"))
{
textresult.Text = $"{selected_number}";
}
else
{
textresult.Text = $"{textresult.Text}{selected_number}";
}
if (operation == Operators.NotSet)
value1 = int.Parse(textresult.Text);
else
value2 = int.Parse(textresult.Text);
}
private void buttonPlus_Click(object sender, EventArgs e)
{
operation = Operators.Plus;
textresult.Text = RESET_VALUE;
}
private void buttonMinus_Click(object sender, EventArgs e)
{
operation = Operators.Minus;
textresult.Text = RESET_VALUE;
}
private void buttonIs_Click(object sender, EventArgs e)
{
switch (operation)
{
case Operators.Minus: textresult.Text = $"{(value1 - value2)}"; break;
case Operators.Plus: textresult.Text = $"{(value1 + value2)}"; break;
}
operation = Operators.NewOperation;
value1 = int.Parse(textresult.Text); //save result for next operation
value2 = 0;
}
private void buttonClear_Click(object sender, EventArgs e)
{
operation = Operators.NotSet;
textresult.Text = RESET_VALUE;
value1 = value2 = 0;
}
I'm creating a program that allows the user to choose 2 different options, 1 for the dormitories and 1 for the meal plan. I'm using radio buttons for both, but I can only choose 1 button at a time, and I want to be able to choose both at the same time
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
int totalCharge = 0;
totalCharge = Dorm() + MealPlan();
Form1 TChargeForm = new Form1();
TChargeForm.label1.Text = totalCharge.ToString("c");
TChargeForm.ShowDialog();
}
private void button3_Click(object sender, EventArgs e)
{
radioButton1.Checked = false;
radioButton2.Checked = false;
radioButton3.Checked = false;
radioButton4.Checked = false;
radioButton5.Checked = false;
radioButton6.Checked = false;
radioButton7.Checked = false;
}
private int Dorm()
{
int total = 0;
if (radioButton1.Checked)
{
total += 1500;
return total;
}
if (radioButton2.Checked)
{
total += 1600;
return total;
}
if (radioButton3.Checked)
{
total += 1500;
return total;
}
if (radioButton4.Checked)
{
total += 1500;
return total;
}
else
{
return total;
}
}
private int MealPlan()
{
int total = 0;
if (radioButton5.Checked)
{
total += 600;
return total;
}
if (radioButton6.Checked)
{
total += 1200;
return total;
}
if (radioButton7.Checked)
{
total += 1700;
return total;
}
else
{
return total;
}
}
}
Try using the GroupName property for each type of button: dorimitories, mealplan
Groupname attribute allows you to create logical groups of selections for radio buttons.
Add this attribute to every radio button or at the group box level.
private void submitbutton_Click(object sender, EventArgs e)
{
if (fishingrodcomboBox.SelectedIndex == 2)
{
fishingrodcomboBox.SelectedIndex = 2;
decimal two = 18m;
decimal price = two * fishingrodnumericUpDown.Value;
totalfishingrodtextBox.Text = price.ToString("C");
}
else
{
//Do something
}
}
Textbox doesnt display the amount and how i do code it such that when i select something from the combo box i can each a different value?