C# 按钮“;清晰的;在文本框上,行仅显示一次
我设法在一个文本框行上找到一个清晰的按钮,代码很少。这将在单击时删除相应的行及其本身。但是按钮只出现一次。它应该随着每一次点击每一行而生成。有人能帮我吗?非常感谢 是WinForm VS2010吗#C# 按钮“;清晰的;在文本框上,行仅显示一次,c#,winforms,C#,Winforms,我设法在一个文本框行上找到一个清晰的按钮,代码很少。这将在单击时删除相应的行及其本身。但是按钮只出现一次。它应该随着每一次点击每一行而生成。有人能帮我吗?非常感谢 是WinForm VS2010吗# 第二个按钮看起来像这样。您将所有X按钮放在同一个位置,在文本框的顶部。实现一些增加按钮Y位置的逻辑。可以是这样的: //... your code here ... btn1.ForeColor = Color.Red; //increase Y // for each control that
第二个按钮看起来像这样。您将所有
X
按钮放在同一个位置,在文本框的顶部。实现一些增加按钮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\u单击(对象发送方,事件参数e)
{
//删除右行
text1.Text=text1.Lines[text1.Controls.IndexOf((控制)发送方)]。删除(0);
//移除按钮
text1.Controls.Remove(text1.Controls.OfType().Last());
}
您将所有的X
按钮放在同一个位置,在文本框的顶部。实现一些增加按钮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\u单击(对象发送方,事件参数e)
{
//删除右行
text1.Text=text1.Lines[text1.Controls.IndexOf((控制)发送方)]。删除(0);
//移除按钮
text1.Controls.Remove(text1.Controls.OfType().Last());
}
@jadolo,您的代码是正确的,它每次都会添加新按钮,但每个按钮都将位于相同的位置,因此您将看不到这些所有按钮,添加按钮的“分配位置”属性,这样所有按钮都会正确显示
表格1.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);
}
}
}
设计:
输出:
我希望这将对您有所帮助。@jadolo,您的代码是正确的,它每次都会添加新按钮,但每个按钮都将位于相同的位置,因此您将看不到这些所有按钮,添加按钮的assign location属性,这样所有按钮都会正确显示
表格1.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);
}
}
}
设计:
输出:
我希望这将对您有所帮助。下面是经过测试的工作代码。该按钮将与默认的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--;
}
下面是测试工作代码。该按钮将与默认的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--;
}
你能把你得到的输出作为图片发布吗?有点难理解你的要求看起来是这样的。如果按下文本框行中的清除“X”按钮,则会使用该按钮删除整行。但他只出现过一次。这应该出现在每一行上,以便每一行都可以单独删除。我想到了Listview或者DataGridView。文本框对于这种事情是完全无用的,你现在应该已经注意到了。你能把你得到的输出作为图片发布吗?有点难理解你的要求看起来是这样的。如果按下文本框行中的清除“X”按钮,则会使用该按钮删除整行。但他只出现过一次。这应该出现在每一行上,以便每一行都可以单独删除。我想到了Listview或者DataGridView。TextBox对于这种事情是完全无用的,你们现在应该已经注意到了。当点击按钮时,他正在清除所有的控件。对吗?我来自WPF后台,不确定WF controls VisualTree。@WPFUser您是正确的,OP应该删除该行他应该只删除带有一个按钮的一行。所以每一行都可以单独删除。@oRole no。。。因为在一个场景中,他有5行,点击第二行,这一行将被删除,按钮将被删除。但是,随着线的移动,较低的线将上升。现在我们有4行和按钮,比如X(空)X X。所以,第二行没有X,但第五行有一行,没有文本。嘿,伙计们,太好了,非常感谢!!点击按钮时,他正在清除所有控件。对吗?我来自WPF后台,不确定WF controls VisualTree。@WPFUser您是正确的,OP应该删除该行他应该只删除带有一个按钮的一行。所以每一行都可以单独删除。@oRole no。。。因为在一个场景中,他有5行,点击第二行,这一行将被删除,按钮将被删除。但是,随着线的移动,较低的线将上升。现在我们有4行和按钮,比如X(空)X X。所以,第二行没有X,但第五行有一行,没有文本。嘿,伙计们,太好了,非常感谢@L_J和Hiren Patel
,感谢您提供进一步的解决方案。我仍然有一个问题,文本被删除在错误的地方。每个清除按钮应删除同一行上的文本。另一个问题是删除时产生的空行。它们应该被推迟,所以,这些都是要获取的行。@JaDoLo我编辑了代码。现在它应该可以像你描述的那样工作了。如果不看到代码,就很难说什么是修复。我怀疑的一件事可能是从btn.Name
中提取的按钮的id
。如果您更改了btn.Name
,您应该已经更改了var Id=int.Parse(btn.Name.Replace(“btn”,”)代码>这里也是。因此,为了避免这种情况,我编辑了一些代码,并将id
保存到控件的Tag
属性中。我唯一需要更改的是private int Btn2Y=13代码>而不是18
@JaDoLo什么是e