Button "clear" on a TextBox line appears only once

button app
button company
button computing
button html
button pronunciation
button css
button synonym
button game

I managed to get a clear button on a TextBox line with little code. This deletes the corresponding line and itself when clicking. But the button appears only once. It should be generated with each click on each additional line. Can someone help me there? Many Thanks.

Is WinForm VS2010 C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Daten_zu_TextBox_Test
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Button btn1 = new Button();
        btn1.Name = "btn";
        btn1.Click += new EventHandler(btn1_Click);
        btn1.Size = new Size(18, 18);
        btn1.Text = "X";
        btn1.ForeColor = Color.Red;
        textBox1.Controls.Add(btn1);

        string sent = ("\t" + "Testline1");

        textBox1.AppendText(sent);
        textBox1.AppendText(Environment.NewLine);
    }

    void btn1_Click(object sender, EventArgs e)
    {
        textBox1.Controls.Clear();
        textBox1.Text = textBox1.Text.Remove(1, textBox1.Lines[0].Length + 0 );
        textBox1.Text = textBox1.Text.Remove(0, textBox1.Lines[0].Length + 0);
    }

  }
}

private int ButtonCount2 = 0;
private int Btn2Y = 18;
private void button2_Click(object sender, EventArgs e)
{
    Button btn2 = new Button();
    btn2.Name = "btn2" + ButtonCount2;
    btn2.Click += new EventHandler(btn2_Click);
    btn2.Size = new Size(18, 18);
    btn2.Location = new Point(1, Btn2Y * ButtonCount2);

    ButtonCount2++;

    btn2.Text = "X";
    btn2.ForeColor = Color.Red;
    textBox2.Controls.Add(btn2);

    string sent = ("\t" + "Testline" + ButtonCount2);

    textBox2.AppendText(sent);
    textBox2.AppendText(Environment.NewLine);
}
void btn2_Click(object sender, EventArgs e)
{
    var bt2 = sender as Button;
    var bt2Id = textBox2.Controls.IndexOf(bt2);
    textBox2.Controls.Remove(bt2);
    var lines = textBox2.Text.Replace("\r\n", "\n").Split('\n').ToList();
    lines.RemoveAt(bt2Id);
    textBox2.Text = string.Join("\r\n", lines);

    foreach (Button btn2 in textBox2.Controls)
    {
        var Id2 = int.Parse(btn2.Name.Replace("btn2", ""));

        if (Id2 > bt2Id)
        {
            var b2 = textBox2.Controls.Find(btn2.Name, false)[0];
            var loc2 = btn2.Location;
            b2.Location = new Point(loc2.X, loc2.Y - Btn2Y);
         }
    }
    ButtonCount2--;
}

The second button looks like this.


Below is tested working codes. The button will coincide with text for the textbox font size being the default 8.

private int ButtonCount = 0;
private int BtnY = 13;

private void button1_Click(object sender, EventArgs e)
{
    Button btn1 = new Button();
    btn1.Name = "btn" + ButtonCount;
    btn1.Click += new EventHandler(btn1_Click);
    btn1.Size = new Size(18, 18);
    btn1.Location = new Point(1, BtnY * ButtonCount);
    btn1.Tag = ButtonCount;  // the last edit

    ButtonCount++;

    btn1.Text = "X";
    btn1.ForeColor = Color.Red;
    textBox1.Controls.Add(btn1);

    string sent = ("\t" + "Testline" + ButtonCount);

    textBox1.AppendText(sent);
    textBox1.AppendText(Environment.NewLine);
}

void btn1_Click(object sender, EventArgs e)
{
    var bt = sender as Button;
    var btId = textBox1.Controls.IndexOf(bt);
    textBox1.Controls.Remove(bt);
    var lines = textBox1.Text.Replace("\r\n", "\n").Split('\n').ToList();
    lines.RemoveAt(btId);
    textBox1.Text = string.Join("\r\n", lines);

    foreach (Button btn in textBox1.Controls)
    {
        //var Id = int.Parse(btn.Name.Replace("btn", "")); // the last edit
        var Id = (int)btn.Tag;  // the last edit

        if (Id > btId)
        {
            var b = textBox1.Controls.Find(btn.Name, false)[0];
            var loc = btn.Location;
            b.Location = new Point(loc.X, loc.Y - BtnY);
        }
    }
    ButtonCount--;
}

Button, Inc. (Company), Inside a <button> element you can put content, like text or images. This is the difference between this element and buttons created with the <input> element. Button definition is - a small knob or disk secured to an article (as of clothing) and used as a fastener by passing it through a buttonhole or loop. How to use button in a sentence.


You're putting all the X buttons on the same place, on top of textbox. Implement some logic that increases Y location of your button. That can be something like this:

//... your code here ...
btn1.ForeColor = Color.Red;
//increase Y
// for each control that is inside of textBox, 
// lower your newly created button by count * 18 
// so that btn1.Top will be 0, 18, 36 etc...
btn1.Top = textBox1.Controls.Count * 18; 
textBox1.Controls.Add(btn1); 

also, as WPFUser noticed, on click on X button, you're removing all the X buttons. I'm guessing you should remove last, bottom one

EDIT 2. Each button should remove its corresponding line, like this (didn't test it :))

void btn1_Click(object sender, EventArgs e)
{
    //remove right line
    text1.Text = text1.Lines[text1.Controls.IndexOf((Control)sender)].Remove(0);
    //remove button
    text1.Controls.Remove(text1.Controls.OfType<Button>().Last());
}

Button, April fastened the buttons of her overcoat to keep out the wind. A mechanical device meant to be pressed with a finger in order to open or close an electric circuit or  Define button. button synonyms, button pronunciation, button translation, English dictionary definition of button. n. 1. a. A generally disk-shaped fastener used to


@jadolo ,Your code is correct ,It add new button every time but every button will located at same location so those all button will not visible to you, Add assign location property of button like this,so all button are displayed correctly.

Form1.cs

using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {

        int i = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {


        }


        void btn1_Click(object sender, EventArgs e)
        {
            textBox1.Controls.Clear();
            textBox1.Text = textBox1.Text.Remove(1, textBox1.Lines[0].Length + 0);
            textBox1.Text = textBox1.Text.Remove(0, textBox1.Lines[0].Length + 0);
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            Button btn1 = new Button();
            btn1.Name = "btn"+i++;
            btn1.Click += new EventHandler(button1_Click_1);
            btn1.Size = new Size(18,18 );
            btn1.Text = "X";
            btn1.Location = new Point(0, i);
            i += 18;
            btn1.ForeColor = Color.Red;
            this.textBox1.Controls.Add(btn1);


            string sent = ("\t" + "Testline1");

            textBox1.AppendText(sent);
            textBox1.AppendText(Environment.NewLine);


        }
    }
}

Design :

Output :

I hope this will help you.

Button (Computing), The HTML button element represents a clickable button, used to submit forms or anywhere in a document for accessible, standard button  noun a small disk, knob, or the like for sewing or otherwise attaching to an article, as of clothing, serving as a fastening when passed through a buttonhole or loop. anything resembling a button, especially in being small and round, as any of various candies, ornaments, tags, identification badges, reflectors, markers, etc.


Button, In modern clothing and fashion design, a button is a small fastener, now most commonly made of plastic but also may be made of metal, wood, or seashell, that​  In modern clothing and fashion design, a button is a small fastener, now most commonly made of plastic but also may be made of metal, wood, or seashell, that joins two pieces of fabric together. In archaeology, a button can be a significant artifact.


Button, button meaning: 1. a small, usually circular object used to fasten something, for example a shirt or coat: 2. a…. Learn more. GANSSIA 1 Inch Buttons 25mm Sewing Flatback Button Black Colored Pack of 50. 600 Pieces Set Includes 60 Mini Black Top Hats, Plastic Magician Hats, 360 Tiny Black Buttons, 60 Carrot Noses Buttons and 120 Snowman Hands for Christmas Crafting and Sewing, Party Supplies. Dshengoo 600-700 Pieces Resin Buttons Assorted Colors and Shapes Buttons for


HTML button tag, Button is the mobile commerce optimization platform that enables companies to drive revenue through commerce in their mobile apps and websites. Through  Shop sewing buttons online at JOANN. Find great button selections, including novelty sewing buttons and sewing buttons in a variety of styles and types.