gpt4 book ai didi

c# - 在 RichTextBox 中显示按钮

转载 作者:行者123 更新时间:2023-11-30 15:57:51 24 4
gpt4 key购买 nike

如何在设置指针的字符串前显示按钮

Example

此时,按钮出现在点击发生的字符串的对面。

private void richTextBox1_MouseClick(object sender, MouseEventArgs e)
{
button2.Visible = true;

int index = richTextBox1.SelectionStart;
int line = richTextBox1.GetLineFromCharIndex(index);
button2.Visible = true;
int x = richTextBox1.Location.X - 10;
int y = 25;

for (int i = 0; i < richTextBox1.Lines.Length; i++)
{
button2.Location = new Point(280, Cursor.Position.Y - 170);
}
}

最佳答案

如果您想在 richTextBox1 的“内部”显示按钮

    private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
var pos = richTextBox1.GetPositionFromCharIndex(richTextBox1.SelectionStart);

if (pos.X > button2.Width + 4)
{
if (button2.Parent != richTextBox1)
{
button2.Parent.Controls.Remove(button2);
richTextBox1.Controls.Add(button2);
}
button2.Location = new Point(pos.X - button2.Width - 2, pos.Y);
}
else
{
if (button2.Parent == richTextBox1)
{
button2.Parent.Controls.Remove(button2);
richTextBox1.Parent.Controls.Add(button2);
}
button2.Location = new Point(richTextBox1.Left - button2.Width - 2, pos.Y + richTextBox1.Top);
}
}

如果你想在行首显示按钮:

    private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
var pos = richTextBox1.GetPositionFromCharIndex(richTextBox1.SelectionStart);
button2.Location = new Point(richTextBox1.Left - button2.Width - 2, pos.Y + richTextBox1.Top);
}

关于c# - 在 RichTextBox 中显示按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44452865/

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