TextBox行上的“清除”按钮仅出现一次

时间:2018-08-01 10:21:21

标签: c# winforms

我设法用很少的代码在TextBox行上得到一个清晰的按钮。单击时将删除相应的行及其本身。但是该按钮仅出现一次。每次单击附加行时都应生成该文件。有人可以帮我吗?非常感谢。

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

  }
}

enter image description here

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

第二个按钮如下所示。

3 个答案:

答案 0 :(得分:3)

您要将所有X按钮放在同一位置的textbox上方。实现一些增加按钮Y位置的逻辑。可能是这样的:

//... 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); 
另外,正如WPFUser注意到的那样,单击X按钮时,您还将删除所有X按钮。我猜你应该删除最后一个,底部的

编辑2.每个按钮都应删除其对应的行,就像这样(未测试过:))

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

答案 1 :(得分:2)

@jadolo,您的代码正确,它每次都会添加新按钮,但是每个按钮都位于同一位置,因此所有按钮对您都不可见,这样添加按钮的assign location属性,这样所有按钮都可以正确显示。

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 :

我希望这会对您有所帮助。

答案 2 :(得分:1)

下面是经过测试的工作代码。该按钮将与文本重合,文本框字体大小为默认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--;
}