Need help deleting rows/lines in richtextbox c# win forms - c#

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!

Related

How to display input from textbox in a multiple lined textbox?

I need to make a program that takes the input of a textbox (tb_1) and puts it in the second textbox (tb_2, multilined) when a button is pressed. It also needs to paste the code a specific amount of times, depending on the numericupdown digit. I have come this far, what do i do next? Thanks in advance.
(Note: it should probably be done within a class.)
{
public Form1()
{
InitializeComponent();
}
private void tb_1_TextChanged(object sender, EventArgs e)
{
}
private void btn_1_Click(object sender, EventArgs e)
{
string tekst = tb_1.Text;
tb_2.Text = tekst + Environment.NewLine;
tb_1.Clear();
tb_2.Text = tekst + Environment.NewLine;
}
}
}

How do I add click events to all buttons that have names starting with a certain string in C#?

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 to click on a MessageBox Button several times to come out of the Message Box

Quit Button Click Event.
`void buttn2_Click(object sender, EventArgs e) //QUIT BUTTON CLICK EVENT.
{
if (MessageBox.Show("LEAVE CURRENT GAME?", "QUIT CONFIRMATION", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
this.Controls.Remove(buttn); .. PLAY AGAIN BUTTON.
this.Controls.Remove(buttn2);
for (int i = 0; i <= gencount; i++)
{
this.Controls.Remove(panel[i]);
this.Controls.Remove(label200[i]);
this.Controls.Remove(label100[i]);
this.Controls.Remove(Tbox[i]);
}
this.Controls.Remove(AttemptsRem);
this.Controls.Remove(AttemptNum);
this.Controls.Remove(TimeRem);
this.Controls.Remove(Min);
this.Controls.Remove(Sec);
this.Controls.Remove(misc);
this.ReftoForm2.Show(); To go back to the starting form
}
else
buttn.Focus();
}
Form1 activate Event.
private void Form1_Activated(object sender, EventArgs e)
{
if (ui_formCowsAndBulls.rdbSinglePlayer.Checked == true)//Static variable
{
//GetAllTheWords(); .. Am still working on getting a the 4 letter words
//GetDistinctElements(); .. randomly out of a list.
textBox1.PasswordChar = '*';
textBox1.Focus();
foreach (string val in distinctWords)
{
if (val == "ABLE") .. For single player,the guess word is ABLE.
textBox1.Text = val;
}
}
else
{
textBox1.Text = string.Empty;
textBox1.Enabled = true;
textBox1.Focus();
}
//textBox1.Focus();
}
PLAY AGAIN CLICK EVENT
private void buttn_Click(object sender, EventArgs e) //PLAY AGAIN CLICK EVENT.
{
for (int i = 0; i <= gencount; i++)
{
this.Controls.Remove(panel[i]);
this.Controls.Remove(label200[i]);
this.Controls.Remove(label100[i]);
this.Controls.Remove(Tbox[i]);
}
this.Controls.Remove(AttemptsRem);
this.Controls.Remove(AttemptNum);
textBox1.Text = string.Empty;
textBox1.Enabled = true;
textBox1.Focus();
incrpanel = 0; gencount = 0; count = 10;
this.Controls.Remove(TimeRem);
this.Controls.Remove(Min);
this.Controls.Remove(Sec);
this.Controls.Remove(misc);
this.textBox1.PasswordChar = '*';
this.Controls.Remove(buttn);
this.Controls.Remove(buttn2);
}
My question is I dont come out of the message boxes when I click message box button NO.I come out of the message box the first time I play the game,But If i play the game a second time,it takes me two clicks to come out of the Message box.If I play the game a third time,it takes me 3 clicks on either the Yes or NO button to come out of the Message Box.I hope you folks can help me.I had posted the same question before but without the code.Hope the code helps.
I think you may be subscribing to the Click-event:
button1.Click += new EventHandler(buttn2_Click);
in a place in the code where it's called more than once, so when the button is clicked the MessageBox will be shown, and when the buttn2_Click event handler (-your code that you posted) is finished - it will run again, showing another MessageBox, for as many times as the subscription (the "...+=..." above) was done.

Populating textBox with value depending on button click count

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

why does the CLEAR button seem to disable my listbox label output?

When I run my program with a listbox everything works after selecting the items and pressing enter, but when I press the clear button and I select the items again and press enter nothing happens. I've tried the following for the clear button and they clear my label text and the selected listbox but I can no longer produce another output when I try pressing enter button again after selecting the items.
public partial class frmLabSix : Form
{
public string strCakes;
public int cakeCost;
public frmLabSix()
{
InitializeComponent();
}
private void lstCakes_SelectedIndexChanged(object sender, EventArgs e)
{
for (int index = 0; index < lstCakes.SelectedItems.Count; index++)
{
strCakes += Environment.NewLine + lstCakes.SelectedItems[index].ToString();
if (lstCakes.SelectedIndices[index] == 0) cakeCost += 18;
if (lstCakes.SelectedIndices[index] == 1) cakeCost += 25;
if (lstCakes.SelectedIndices[index] == 2) cakeCost += 40;
if (lstCakes.SelectedIndices[index] == 3) cakeCost += 30;
}
}
private void lblOrdered_Click(object sender, EventArgs e)
{
}
private void btnEnter_Click(object sender, EventArgs e)
{
double tax = 1.13;
lblOrdered.Text = "You have ordered: " + strCakes + '\n' + "Total Cost: " + (tax * cakeCost).ToString("C");
lblOrdered.Visible = true;
}
private void btnClear_Click(object sender, EventArgs e)
{
lstCakes.SelectedItems.Clear();
lblOrdered.Visible = false;
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
}
Can someone tell me why this is?
This line of code hides your label.
lblOrdered.Visible = false;
You make your label invisible on clear button click. Do you reset its visibility after?
I don't know, what happens in other part of your code, but it should probably be like this:
// if the label is not visible, the next line won't make it visible implicitly
lblOrdered.Text = ...
//you should set label's visibility explicitly
if (!lblOrdered.Visible)
lblOrdered.Visible = true;
Setting label's text doesn't make it visible. If you hide it with your own code, you should make label visible explicitly as well.

Categories

Resources