我想要一个固定大小的框来显示一些文本,并在其中有一个链接,可以在右下角点击进行编辑。单击此编辑链接会显示一组要填写的字段。
我尝试了LinkLabel,它可以解决问题,但是,当我更改文本时,框的大小会发生变化。我将自动调整大小设置为false,较长的文本强制将该链接置于框外。短文将链接放在远处。
我可以想象并计算链接的位置并将其插入适当的位置(如果需要可以添加新行),但我想知道是否有更简单的方法来执行此操作。
有没有更好的控制来做这个或另一种方式呢?
编辑: 填写的框连接在一起并替换linklabel中的文本。编辑链接当前被附加到此,并且设置了LinkArea(最后4个字符)。
答案 0 :(得分:1)
您需要构建一个复合布局,例如使用Panel
Label/TextBox
(Dock = Fill)和LinkLabel
(Dock = Bottom,TextAlign = MiddleRight),就像这样
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Samples
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var text = "I want a box of a fixed size to show some text and have a link in it that is clickable in the bottom right corner for editing.";
int textSize = 50;
var form = new Form { Padding = new Padding(8) };
var panel = new Panel { Parent = form, BorderStyle = BorderStyle.FixedSingle, Padding = new Padding(4) };
var label = new Label { Dock = DockStyle.Fill, Parent = panel, AutoSize = false, Text = text, Height = textSize };
var link = new LinkLabel { Dock = DockStyle.Bottom, Parent = panel, AutoSize = false, TextAlign = ContentAlignment.MiddleRight, Text = "Edit" };
panel.Location = form.DisplayRectangle.Location;
panel.Width = form.DisplayRectangle.Width;
panel.Height = panel.Padding.Vertical + link.Height + label.Height;
Application.Run(form);
}
}
}
结果:
答案 1 :(得分:0)
我找到了另一种方式。
我使用一个文本框并在其角落放置一个链接标签。
textbox是只读和禁用的,因此它的作用类似于标签,背景颜色设置为白色,以覆盖禁用/只读的颜色。
编辑链接现在保持