How to make buttons to enter specific values? - c#

public partial class Form1 : Form
{
public static string a = "a"; public static string b = "b"; public static string c = "c";
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = a;
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = b;
}
private void button3_Click(object sender, EventArgs e)
{
textBox1.Text = c;
}
private void button4_Click(object sender, EventArgs e)
{
a = null;
b = null;
c = null;
}
}
I want to make a simple keyboard for a chat.
I started it with a small sample program in which there are only 3 buttons; Button a, Button b, Button c for a,b,c, respectively.
When I run the Program,I press Button a for a & then Button b for b (Now I want the Output in the form ab) but it first shows a and then on pressing button b it erases a and shows b.
I want to make more buttons like these to make a keyboard.
Basically,I want to print the letters stored in the buttons sequentially,but it erases the first one and then prints the next one..

The easiest way to create an on-screen keyboard is to use the buttons texts, except in special keys like backspace, enter, clear etc`.
That way all your text buttons click events can be handled with a single method:
private void KeyButton_Click(object sender, EventArgs e)
{
textBox1.Text += ((Button)sender).Text;
}
private void ClearButton_Click(object sender, EventArgs e)
{
textBox1.Text = string.Empty;
}
private void BackspaceButton_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text.SubString(0, textBox1.Text.Length-1);
}

It erases the value because you are using = operator. Try to use +=
textBox1.Text += c;
textBox1.Text = textBox1.Text + c;
Also you can get a text value from the Button's Text property.
And have only one Button.Click event handler, for each button.
private void button_Click(object sender, EventArgs e)
{
var button = sender as Button;
textBox1.Text = textBox1 + button.Text;
}

As I told you in the comments before you posted the code you need to concatenate (a.k.a append) the character to the text box.
If you have a text box named textBox1 doing this:
textBox1.Text = 'a'
will replace any text already written in the text box with the character 'a'
What you need to do is use the += operator:
textBox1.Text += a;
textBox1.Text = b;
textBox1.Text = c;

Related

Clicking the same button but different action - c#

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;
}

How to add into a singular number when pressing different buttons

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
}

How do I keep words from being replaced by each button?

I am creating an application for class called Sentence Builder, it's supposed to allow the user to click the buttons provided to build a sentence on a label. I have had no success on getting a word generated by a button to stay on the label after I click another button. When I click a button it displays the word on the button onto the label. Then when I click another button, the word on that button appears on the label but it takes the place of the previous word that was already there. I need it to stay on the label so the user can create a sentence on the label by pressing multiple buttons. This is my code for the application.
namespace C3_7_Sentence_Builder
{
public partial class sentencebuilderForm : Form
{
public sentencebuilderForm()
{
InitializeComponent();
}
private void resetButton_Click(object sender, EventArgs e)
{
sentenceoutputLabel.Text = "";
}
private void exitButton_Click(object sender, EventArgs e)
{
this.Close();
}
private void AButton_Click(object sender, EventArgs e)
{
string output;
output = AButton.Text;
sentenceoutputLabel.Text = output;
}
private void a_Button_Click(object sender, EventArgs e)
{
string output;
output = a_Button.Text;
sentenceoutputLabel.Text = output;
}
private void anButton_Click(object sender, EventArgs e)
{
string output;
output = anButton.Text;
sentenceoutputLabel.Text = output;
}
private void TheButton_Click(object sender, EventArgs e)
{
string output;
output = TheButton.Text;
sentenceoutputLabel.Text = output;
}
private void the_Button_Click(object sender, EventArgs e)
{
string output;
output = the_Button.Text;
sentenceoutputLabel.Text = output;
}
}
}
you need to use +=
sentenceoutputLabel.Text += output;
what that does is it appends the string instead of overwrites it.
Aside from my comment, I figured I would post as an answer because you can remove all the individual events and suscribe all your buttons to the following to do the same thing.
private void sentence_button_clicked(object sender, EventArgs e)
{
var button = sender as Button;
if(button != null)
sentenceoutputLabel.Text += button.Text;
}
The only button that does need to reassign the text, instead of appending would be the Reset button.
You are always replacing all text of the label with all text of your choice. You need the text of the label to stay and only add the new text:
sentenceoutputLabel.Text = sentenceoutputLabel.Text + output;
You may want to add some spaces:
sentenceoutputLabel.Text = sentenceoutputLabel.Text + " " + output;

Add to int each time a button is clicked and show in textbox c#

I am very new to programming and I am trying to do that every time you click a button, it adds one to the value of an int and shows it in a textbox. My code is:
private void button1_Click(object sender, EventArgs e)
{
int a = 100;
a++;
txtBox1.Text = a.ToString();
}
So when I click the button it shows in the text box 101, but when I click it again, I want the textbox to show 102 and 103 etc etc. Any ideas? I assume it's very easy and using some variation of a loop but I have tried a few things and nothing seems to work. Any tips would be greatly appreciated! Thanks.
You have to store your value outside of Method Body.
private int a = 100;
private void button1_Click(object sender, EventArgs e)
{
a++;
txtBox1.Text = a.ToString();
}
What you did in your program is anytime you clicked the button, new Integer a was declared with value of 100, then you are increasing it by 1 and that's why you always seen '101'.
In your code you delcare a and assign a value to it over and over again every time you click on the button.
You should declare the variable outside of button1_Click method:
class Window1
{
int a = 100;
....
private void button1_Click(object sender, EventArgs e)
{
a++;
txtBox1.Text = a.ToString();
}
}
You need to declare a as a member of the class containing your method:
private int _a = 100;
private void button1_Click(object sender, EventArgs e)
{
_a++;
txtBox1.Text = _a.ToString();
}
If you don't do that, you will have a new instance every time the button is clicked, so you will always see 101 in your text box.
It is possible not to create global fields and store count of clicks inside the textbox.
This is especially convenient if you have several buttons.
private void button1_Click(object sender, EventArgs e)
{
if (txtBox1.Tag is int)
{
int a = (int)txtBox1.Tag;
a++;
txtBox1.Tag = a;
txtBox1.Text = a.ToString();
}
else
{
txtBox1.Tag = 100;
txtBox1.Text = 100;
}
}
int a = 100;
txtBox1.Text = a.ToString();
......
private void button1_Click(object sender, EventArgs e)
{
a++;
txtBox1.Text = a.ToString();
}
Placing int a = 100; inside the button1_Click(object sender, EventArgs e) will set a to 100 when each time function executing. If you need to have a counter place it outside from the function(Then it will initialize only once.) and increment it when executing the function.
Solution
int a = 100;
private void button1_Click(object sender, EventArgs e)
{
a++;
txtBox1.Text = a.ToString();
}
static int a = 100;
private void button1_Click(object sender, EventArgs e)
{
a++;
txtBox1.Text = a.ToString();
}
if you want to optimization your code than firstly set the textbox property text = 100 and write only one line code in button click event
private void button1_Click(object sender, EventArgs e)
{
txtBox1.Text = (Convert.ToInt32(txtBox1.Text) + 1).ToString();
}
as you know C# complie the code line by line and you have only one line code than it's give faster perfomance.

C# Calculator typing by pressing buttons

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
}

Categories