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

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;

Related

How can i store a textboxt text for an array and restore it later?

What i want to do is: I have a "+" button, that creates a new tab every click, and one textbox. I want to do every tab create a text array for the same textbox (1 textbox, having different values according to the selected tabcontrol tab.) i have a maximum value of 5 tabs that i set, how can i do that?
i searched alot on how to create it, but didnt found a specific one for what i need
code of tabcontrols
private void label1_Click(object sender, EventArgs e)
{
if(tabControl1.TabPages.Count != 5)
{
page++;
string title = "Script " + page.ToString();
TabPage tabipage = new TabPage(title);
tabControl1.TabPages.Add(tabipage);
}
else
{
MessageBox.Show("You cant add more tabs!");
}
}
also, doesnt need to be exactly an array, just need to save and restore values between tabs
This is a pretty open ended question, and you'll have to forgive me if I misunderstood. What I'm guessing is you have a tab control, and OUTSIDE the tab control you have a single textbox that should change based on which tab is selected? If so you could do something similar to the below:
class tabWithScript : TabPage
{
public tabWithScript(string title) : base(title)
{ }
public string myscript { get; set; }
}
private void button1_Click(object sender, EventArgs e)
{
if (tabControl1.TabPages.Count < 5)
{
string title = "Script " + (tabControl1.TabPages.Count +1 ).ToString();
tabWithScript tabipage = new tabWithScript(title);
tabipage.myscript = $"Oh man, this is my script {tabControl1.TabPages.Count +1}";
tabControl1.TabPages.Add(tabipage);
}
else
{
MessageBox.Show("You cant add more tabs!");
}
}
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
tabWithScript myTab = (tabWithScript)tabControl1.SelectedTab;
textBox1.Text = myTab.myscript;
}
catch (Exception ex) { }
}
}
Instead of using the default TabPage object you can create your own class that inherits TabPage, then store whatever information you want in there. (You'd likely have to delete the default TabPages the tabcontrol creates).
Alternatively, you can just create a string array and index it appropriately e.g.
string[] myList = new string[5];
private void button1_Click(object sender, EventArgs e)
{
if (tabControl1.TabPages.Count < 5)
{
string title = "Script " + (tabControl1.TabPages.Count +1 ).ToString();
TabPage tabipage = new TabPage(title);
myList[tabControl1.TabPages.Count] = $"Oh man, this is my script {tabControl1.TabPages.Count +1}";
tabControl1.TabPages.Add(tabipage);
}
else
{
MessageBox.Show("You cant add more tabs!");
}
}
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Text = myList[tabControl1.SelectedIndex];
}
`
An alternative of #Devon Page's solution is using the Tag property, existing in almost all controls, to store value.
private void label1_Click(object sender, EventArgs e)
{
if (tabControl1.TabPages.Count < 5)
{
page++;
string title = "Script " + page.ToString();
TabPage tabipage = new TabPage(title);
tabipage.Tag = title; //Store whatever you want.
tabControl1.TabPages.Add(tabipage);
}
else
{
MessageBox.Show("You cant add more tabs!");
}
}
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Text = tabControl1.SelectedTab.Tag.ToString(); // Cast to restore it
}

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 to make buttons to enter specific values?

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;

how to reset the focus to last entered textbox in windows application

I have two TextBoxes and a button control in the form. When the button is clicked the name of the last entered TextBox should be displayed in a MessageBox. At the same time I need to reset the focus to last entered TextBox.
string str=string.Empty;
bool foc;
In button click I wrote the following code
if (MessageBox.Show("You want to reset or continue", "control",
MessageBoxButtons.OKCancel) == DialogResult.Cancel)
{
if (foc== true)
{
textBox1.Focus();
}
else
{
textBox2.Focus();
}
}
When I clicks on cancel button the focus should be into textbox which is entered at last
private void textBox1_Enter(object sender, EventArgs e)
{
str = textBox1.Name;
foc= textBox1.Focus();
}
private void textBox2_Enter(object sender, EventArgs e)
{
str= textBox2.Name;
foc= false;
}
Other than the above lines of code is there any other possibility to focus into the textbox, but when number of textboxes increases how i need to write the conditions.
If I am having textbox,combobox,listbox,checkbox or any other controls in the form then how to find in which control the user enterd at last and set focus to that control by using any function instead of writing in every control Enter event
You can handle Leave event of the TextBoxes to store the Last TextBox Control.
Try this:
this.btnSubmit.Click += new System.EventHandler(this.Submit_Click);
this.btnCancel.Click += new System.EventHandler(this.Cancel_Click);
this.textBox1.Leave += new System.EventHandler(this.textBox1_Leave);
this.textBox2.Leave += new System.EventHandler(this.textBox2_Leave);
TextBox txtLast = new TextBox();
private void textBox1_Leave(object sender, EventArgs e)
{
txtLast = (TextBox)sender;
}
private void textBox2_Leave(object sender, EventArgs e)
{
txtLast = (TextBox)sender;
}
private void Submit_Click(object sender, EventArgs e)
{
MessageBox.Show(txtLast.Text);
}
private void Cancel_Click(object sender, EventArgs e)
{
txtLast.Focus();
}
public bool textBox1WasLastFocused = false, textBox2WasLastFocused = false; // Global Declaration
void textBox2_GotFocus(object sender, EventArgs e)
{
textBox2WasLastFocused = true;
textBox1WasLastFocused = false;
}
void textBox1_GotFocus(object sender, EventArgs e)
{
textBox1WasLastFocused = true;
textBox2WasLastFocused = false;
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1WasLastFocused)
MessageBox.Show("textbox1 was ladst focused !");
else if(textBox2WasLastFocused)
MessageBox.Show("textbox2 was ladst focused !");
}

TextBox for Live Search in Windows Forms

How to create a Textbox which displays "search" in grey color when it is empty and standard behavior when user starts typing text into it?
Do it via TextBox Events Enter and Leave and Attributes:
private void textBox1_Leave(object sender, EventArgs e)
{
if(textBox1.Text.Trim().Length == 0)
{
textBox1.Text = "Search";
textBox1.ForeColor = Color.LightGray;
}
}
private void textBox1_Enter(object sender, EventArgs e)
{
textBox1.Text = string.Empty;
}
See this thread at MSDN for a possible solution: http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/93a67793-6426-4d4f-be9d-a9b79725efc8

Categories