When i click on a button,text will appear in textbox1 but then i want it to change focus to another textbox(textbox2) and when i click the same button again,display the same text but in textbox2.
private void btna_Click(object sender, EventArgs e)
{
textBox1.Text = ("A");
if (textBox1.Text.Length > 0)
{
textBox2.Focus();
}
If you want to alternate between different TextBoxes in your click event to determine which one to update, you can track them in a private variable, and then just switch which one you're using based on the current value, for example:
private TextBox textBoxToUpdate = null;
private void button1_Click(object sender, EventArgs e)
{
// Swap the textBoxToUpdate between textBox1 and textBox2 each time
textBoxToUpdate = (textBoxToUpdate == textBox1) ? textBox2 : textBox1;
textBoxToUpdate.Text = "A";
}
Another way to do this if you're updating multiple controls is to store them in a list and increment an integer that defines the index to the next item.
// holds the text boxes we want to rotate between
private List<TextBox> textBoxesToUpdate = new List<TextBox>();
private void Form1_Load(object sender, System.EventArgs e)
{
// add some text boxes to our list
textBoxesToUpdate.Add(textBox1);
textBoxesToUpdate.Add(textBox2);
textBoxesToUpdate.Add(textBox3);
textBoxesToUpdate.Add(textBox4);
}
// stores the index of the next textBox to update
private int textBoxToUpdateIndex = 0;
private void button1_Click(object sender, EventArgs e)
{
textBoxesToUpdate[textBoxToUpdateIndex].Text = "A";
// increment the index but set it back to zero if it's equal to the count
if(++textBoxToUpdateIndex == textBoxesToUpdate.Count) textBoxToUpdateIndex = 0;
}
Related
I have a GUI that is using a text field for a user int input.
a combo box that has drop downs for calculations (sum, add, div, mult. etc.)
then I click a button to calculate.
the Results show in bottom text field.
I am having trouble getting the combobox to string, so that I can make the right calculation.
The first part also needs the combobox to be selected on "Initialize" to add the value to the result text field. After that is added, all combobox choices after will utilize the initialized value in the results field and the user input field for any function calls.
namespace GUI
{
public partial class Form1 : Form
{
static void Main()
{
ApplicationConfiguration.Initialize();
Application.Run(new Form1());
}
public Form1()
{
InitializeComponent();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string path = #"C:\Users\calculations.txt";
StreamReader sr = new StreamReader(path);
string x = sr.ReadToEnd();
string[] y = x.Split('\n');
for (int i = 0; i < y.Length; i++)
{
comboBox1.Items.Add(y[i]);
}
}
//Button to calculate Resualt
private void button1_Click(object sender, EventArgs e)
{
string x = comboBox1.SelectedItem.ToString();
int b = int.Parse(textBox2.Text);
if(x == "Initialize")
textBox1.Text = (b).ToString();
else if (textBox1 == null)
Console.WriteLine("Please Initialize value");
if(x == "Sum")
textBox1.Text = (b + b).ToString();
}
//Result text box
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
//User number input box
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
}
}
Values in calculation.txt
Initialize
Sum
Subtract
Product
Power
Log
I'm trying to have a button click event add an int value to a textbox and then have another button click event subtract from the same textbox that I just added the int value to. The problem I'm having is that the subtract button enters a negative(-1) value to the textbox and if I click on the add button again it goes right back to the int value I had before clicking the subtract button. I just want the add button to add and the subtract button to subtract the in value. I'm very new to c# and I've tried a everything I've found online and I'm losing faith.
I've tried if statements, and some other methods I found online and nothing works.
This is the code I have now. The add button works fine, but the subtract button doesn't do what I want.
private int a = 0;
private void btnAdd_Click(object sender, EventArgs e)
{
a++;
txtSummary.Text = a.ToString();
}
private void comboBoxValues_SelectedIndexChanged(object sender, EventArgs e)
{
}
private int b = -0;
private void btnSubtract_Click(object sender, EventArgs e)
{
b--;
txtSummary.Text = b.ToString();
}
This will convert whatever is in your TextBox to an Integer, then add or subtract 1 to that value:
private void Form1_Load(object sender, EventArgs e)
{
txtSummary.Text = "0";
}
private void btnAdd_Click(object sender, EventArgs e)
{
AddToTextBox(1);
}
private void btnSubtract_Click(object sender, EventArgs e)
{
AddToTextBox(-1);
}
private void AddToTextBox(int changeBy)
{
int value;
if(int.TryParse(txtSummary.Text, out value))
{
value = value + changeBy;
txtSummary.Text = value.ToString();
}
else
{
MessageBox.Show("Invalid Integer in TextBox!");
}
}
I have a certain number of Textboxes, I need to track the last two focused Texboxes. This is the approch that I attempted.
private Control _focusedControl;
private Control _lastfocusedControl;
private void PCp1txt_LostFocus(object sender, System.EventArgs e)
{
_lastEnteredControl = (Control)sender;
}
private void PCp1txt_LostFocus(object sender, System.EventArgs e)
{
_lastEnteredControl = (Control)sender;
}
private void PCp2txt_GotFocus(object sender, EventArgs e)
{
_focusedControl = (Control)sender;
}
private void PCp2txt_GotFocus(object sender, EventArgs e)
{
_focusedControl = (Control)sender;
}
This is not working because when I press the button the contenent of the _lastfocusedControl will be the same as _focusedControl because another control was focused by clicking that button.
You can handle Enter event of all those TextBox controls using a single handler and in the handler and keep track of last n focused TextBox controls:
const int n = 2;
TextBox[] textBoxes = new TextBox[n];
private void textBox_Enter(object sender, EventArgs e)
{
var destination = new TextBox[n];
Array.Copy(textBoxes, 1, destination, 0, textBoxes.Length - 1);
textBoxes = destination;
textBoxes[textBoxes.Length - 1] = (TextBox)sender;
}
In above example, we are shifting the array to left, then assign the sender to the last element. This way the array always contains the last n focused TextBox controls for you.
I would suggest, to make it simpple, do something like when your textbox get focused, put the name of it into an array, for example, and after that, list the last two name added of the array created.
The title was properly written wrong but i will explain currently i have 2 sets of three buttons linked to a label. when i press a button it will place a number in the label as a result what i want to do is after the first button is pressed and then a second is clicked i want the two scores to add together to make a total result in the label eg if i press "three of a kind" which equals 3 points then press "four of a kind" which equals 6 points i want a result of 9 in the label and then if i press "five of a kind" which equals 12 point the label would then read 18 ect can i get any help please there isnt any code to put in due to not finding anything i will put in how my buttons are linked to my labels.
Result
public class Result
{
}
private void button2_Click(object sender, EventArgs e)
{
String add = (Convert.ToInt32(3)).ToString();
label6.Text = Convert.ToString(add);
}
private void button5_Click(object sender, EventArgs e)
{
String add = (Convert.ToInt32(3)).ToString();
label7.Text = Convert.ToString(add);
}
private void button3_Click(object sender, EventArgs e)
{
String add = (Convert.ToInt32(5)).ToString();
label6.Text = Convert.ToString(add);
}
private void button6_Click(object sender, EventArgs e)
{
String add = (Convert.ToInt32(5)).ToString();
label7.Text = Convert.ToString(add);
}
private void button4_Click(object sender, EventArgs e)
{
String add = (Convert.ToInt32(12)).ToString();
label6.Text = Convert.ToString(add);
}
private void button7_Click(object sender, EventArgs e)
{
String add = (Convert.ToInt32(12)).ToString();
label7.Text = Convert.ToString(add);
}
I didn't quite understand the question, but I would suggest you to read about WPF and the MVVM mechanism to sync the data between the UI and the data.
What you're doing is writing a lot of code-behind code that is duplicated over and over and is doing a lot of unnecessary work (you convert a number to Int, then convert the result to string and then convert it again to string)
This function can be used to do the work property and efficiently once:
private void UpdateLabelText(Label label, int number) {
label.Text = number.ToString();
}
And use this method whenever you need to update the text in your label.
You have to cast the value of label.Text to int then add the value and then recast to string:
private void button6_Click(object sender, EventArgs e)
{
AddToLabel(label7, 12);
}
void AddToLabel(Label label, int value)
{
var n = int.Parse(label.Text); // convert the actual value of label.Text to int
var add = n + value; // add the increment
label.Text = add.ToString(); // assign to label.Text
}
I am studying C# now, so i am making some kind of exercises to get used to C# syntax and learn better. I decided to make a calculator looking alike the normal windows calculator.
I created only one button "1" and one textbox.
I want to make this button write 1 in textbox when I press it and also make an int variable equal to the number in the textbook, to make the calculation later. So I can't either change "int a"'s value or change text in the textbox, it always shows 01, because a always equals to 0.
How can I make the program, both show the correct numbers and change the value of a correctly?
For example how can i make the program show eleven in the textbox when i press button twice and also change "int a"'s value to 11?
public partial class Form1 : Form
{
int a;
string Sa;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Sa = a.ToString() + "1";
textBox1.Text = Sa;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text += "1";
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
a = Int32.Parse(textBox1.Text);
}
just it.. Change text on textBox every button click, and change variable a every textBox changed.
The value could then be set using
a = int.Parse(Sa);
textBox1.Text = Sa.TrimStart('0');
Although if you'd like to be more efficient about it,
a = a * 10 + 1;
not have Sa at all,
textBox1.Text = a.ToString();
If you run into integer overflows, you should use BigInteger.
You have several options:
Make int a nullable int. That way you can check if int is already set
int? a;
if ( a.HasValue )
{
}
else
{
}
Check if the Text property of textBox1 is empty (which means you don't have to append a to it)
if ( textBox1.Text == string.Empty)
{
}
else
{
}
public void btnOne_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnOne.Text;
}
private void btnTwo_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnTwo.Text;
}
// etc
for any button you want to append text to the text box set the click property to btn_Click then pt this code inside the method
private void btn_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
// This will assign btn with the properties of the button clicked
txt_display.Text = txt_display.Text + btn.Text;
// this will append to the textbox with whatever text value the button holds
}