I am new to coding in C#. Currently I am encountering the problem of populating a textBox depending on the amount of times button1 is clicked. I have been able to populate option one but I am not sure how to get the second option after the second click and so fourth. How would I be able to do that? Also, Would I need to add a loop to start over after the fourth click?
Code
protected void Button1_Click(object sender, EventArgs e)
{
MyTextBox.Text = "Fruits"; //1st click
MyTextBox.Text = "Vegtables"; //2nd click
MyTextBox.Text = "Grains"; //3rd click
MyTextBox.Text = "Poultry"; //4th click
}
private List<string> messages= new List<string>(){"Fruits", "Vegetables", "Grains", "Poultry"};
private int clickCount = 0;
protected void Button1_Click(object sender, EventArgs e)
{
MyTextBox.Text = messages[clickCount];
clickCount++;
if (clickCount == messages.Count)
clickCount = 0;
}
Related
So essentially, I have a button, when I press the button, the button should delete the most recent row of text embedded into the rich text box. Press it again, it should delete the next line above that etc.
I tried a using .substring, which it worked, but required me to double click the button instead of a single click..
Furthermore, when I press the add text button it should replace the deleted line. Currently, the delete button button1_click just removes all lines, but all I want the it to do is remove a line of text from the richtextbox.
Edit:
Huge apologies before, I forgot to add the code as mentioned in replies, apologies.
public partial class Form1 : Form
{
string[] texts = new string[10];
int i = 0;
public Form1()
{
InitializeComponent();
}
private void btn_Click(object sender, EventArgs e)
{
if (TextBox.Text == "")
{
texts[i] = texts[i];
}
else
{
texts[i] += TextBox.Text + "\r\n";
richText.Text += texts[i];
++i;
TextBox.Text = "";
}
}
private void richText_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
//i dont know need to do research or im too dumb
richText.Text = "";
for (int i = 0; i < texts.Length; i++)
{
texts[i] = "";
}
i = 0;
//https://stackoverflow.com/questions/22587505/delete-the-last-line-of-rich-text-box
//works, but double click instead of one click
}
}
}
To clarify, the left button adds text from the textbox and the right button is basically the function aforementioned.
enter image description here
Thanks to anyone who helps!
Is there a way to change the state of the radio button depending on the value entered by the user in one of the text boxes. I’ve tried this:
protected void Btn_click(object sender, EventArgs e) {
int TextBoxValue = int.Parse(TextBox.Text);
if(TextBoxValue < 18) {
RadioButton.Enabled = false;
} else {
RadioButton.Enabled = true;
}
}
but when I click a button nothing happens.
I have three TextBoxe1,TextBoxe2 and TextBoxe3 and one main TextBox4 and Button1 when its clicked it will insert TextBox4's value into the clicked (the chosen/selected/clicked one) TextBox. This code Populates all the TextBoxes with the same value.
private void button1_Click(object sender, EventArgs e)
{
TextBox[] array = new TextBox[3] { textBox1, textBox2, textBox3 };
for (int i = 0; i < 3; i++)
{
if (array[i].Focus())
{
array[i].Text = textBox4.Text;
}
}
}
But I want it to take the TextBox4's value and insert into the TextBox2 that I have Clicked on. Like this illu.:
It's better to change the way that you set the value for those TextBox controls and think about another UI, but anyway, if you like to keep it as is, I'll share an idea to satisfy the requirement which you described in the question.
Define a field in form, TextBox selectedTextBox;, then handle Enter event of those 3 TextBox controls and in the handler set selectedTextBox = (TextBox)sender. Then in Click event handler of the button, check if selectedTextBox is not null, then set selectedTextBox.Text = textBox4.Text;:
TextBox selectedTextBox;
public Form1()
{
InitializeComponent();
textBox1.Enter += TextBox_Enter;
textBox2.Click += TextBox_Enter;
textBox3.Click += TextBox_Enter;
button1.Click += button1_Click;
}
void TextBox_Enter(object sender, EventArgs e)
{
selectedTextBox = (TextBox)sender;
}
void button1_Click(object sender, EventArgs e)
{
if(selectedTextBox!=null)
selectedTextBox.Text = textBox4.Text;
}
Make sure you don't attach event handler twice, so to attach event handler, use code editor or designer, not both of them.
Register the Click event of the 3 target TextBoxes to the same handler:
public Form1()
{
InitializeComponent();
textBox1.Click += TransportValueEvent_Click;
textBox2.Click += TransportValueEvent_Click;
textBox3.Click += TransportValueEvent_Click;
}
Inside the handler get the sender (which will be the TextBox that you have clicked) as TextBox and write the value:
private void TransportValueEvent_Click(object sender, EventArgs e)
{
(sender as TextBox).Text = textBox4.Text;
}
Now you don't need the button anymore. The value will be written to the correct TextBox as soon as you click it.
May be you want to avoid deletion if the textBox4 is empty, then you can update the value only if:
private void TransportValueEvent_Click(object sender, EventArgs e)
{
if (!string.IsNullOrWhiteSpace(textBox4.Text))
{
(sender as TextBox).Text = textBox4.Text;
}
}
I am writing a simple calculator script for my C# programming class. It will of course have buttons 0-9 that will update the output textbox to add the number of whatever button is clicked. My problem right now that is I would rather not have to have 10 different click events in my script. I would rather have a loop that cycles through the buttons that will add the same click event to each one and then decide what number to add to the output based on the button.
So right now, I have a click event for the "1" button which is this...
private void btnNum1_Click(object sender, EventArgs e)
{
txtOutput.Text = Convert.ToString(txtOutput.Text + "1");
}
This works fine, but, again, I would rather not have to do this 10 times. How can I create a loop that prevents this?
The button names are btnNum1, btnNum2, btnNum3, etc.
Assuming the button text is just "1", "2" etc you could do this:
private void btnNum_Click(object sender, EventArgs e)
{
var button = sender as Button
txtOutput.Text += button.Content.ToString();
}
Then just apply this event to all the buttons.
Also note you don't need Convert.ToString() as what you are trying to convert is already a string. Using += also cleans up your code a bit.
You could do this to wire-up all of the events in one go:
for (var n = 0; n <= 9; n++)
{
var btn =
this
.Controls
.Find("btnNum" + n.ToString(), false)
.Cast<Button>()
.First();
var digit = n;
btn.Click += (s, e) =>
{
txtOutput.Text = digit.ToString();
};
}
You could enumerate the children controls of your Form/Control, look the type of controls which are type of Button and the name StartWith 'btnNum', with each of these buttons, add a Click event address to btnNum_Click().
Say if all your buttons are contained in a Panel named 'pnlButtons', you could loop all the children like this.
foreach (var control in pnlButtons.Controls)
{
if(control.GetType() == typeof(Button))
{
var button = control as Button;
if (button .Name.StartWith('btnNum'))
{
button.Click += btnNum_Click;
}
}
}
You can use the "Tag" property of the Button control and make an array of Buttons to subscribe to the same event. See sample below:
void InitializeButtons()
{
Button btnNum1 = new Button();
btnNum1.Text = "1";
btnNum1.Tag = 1;
//Button 2..8 goes here
Button btnNum9 = new Button();
btnNum9.Text = "9";
btnNum9.Tag = 9;
Button[] buttons = new Button[]{
btnNum1, btnNum2, btnNum3, btnNum4, btnNum5, btnNum6, btnNum7, btnNum8, btnNum9
};
for (int i = 0; i < buttons.Length; i++)
{
buttons[i].Click += Button_Click;
}
}
void Button_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
int value = (int)button.Tag;
//Do something with value
}
Assuming WinForms, you can recursively search for buttons that start with "btnNum" and wire them up to a common handler like this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Load += Form1_Load;
}
void Form1_Load(object sender, EventArgs e)
{
FindButtons(this);
}
private void FindButtons(Control ctl)
{
foreach(Control ctrl in ctl.Controls)
{
if (ctrl.Name.StartsWith("btnNum") && (ctrl is Button))
{
Button btn = (Button)ctrl;
btn.Click += btn_Click;
}
else if(ctrl.HasChildren)
{
FindButtons(ctrl);
}
}
}
private void btn_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
txtOutput.Text = Convert.ToString(txtOutput.Text + btn.Text);
}
}
I have problem displaying usercontrol,I have a usercontrol which has a panel called rowpanel which has textbox and combobox,Now ,when I Click button_1,I want the usercontrol to be displayed on each click,it is like adding a row on each click,I just don`t know how to loop it,I tried using indexing...
CODE
private void button1_Click(object sender, EventArgs e)
{
AddRow add = new AddRow();
show_pnl.Controls.Add(add);
}
AddRow is usercontrol ...this is a windows application,can I get some help please,
The reason is because they are overlapping each other. To fix it increment the top &/or left as shown here:
private const int gap = 20;
private int count = 0;
private void button1_Click(object sender, EventArgs e)
{
var add = new UserControl1();
add.Top = count * (add.Height + gap);
show_pnl.Controls.Add(add);
count++;
}