gpt4 book ai didi

c# - TextBox 行上的按钮 "clear"仅出现一次

转载 作者:太空宇宙 更新时间:2023-11-03 18:53:18 25 4
gpt4 key购买 nike

我设法用很少的代码在 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--;
}

第二个按钮看起来像这样。

最佳答案

您将所有 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());
}

关于c# - TextBox 行上的按钮 "clear"仅出现一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51630904/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com