WinForms自定义UserControl标签文本显示不全问题求助
WinForms自定义UserControl文本截断问题
我在WinForms中开发了名为DownloadProgressInfo的自定义UserControl,相关代码如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace UC { public partial class DownloadProgressInfo : UserControl { public Label lblSizeValue { get; private set; } public Label lblDownloadedValue { get; private set; } public Label lblImagesLeftValue { get; private set; } public Label lblSpeedValue { get; private set; } public DownloadProgressInfo() { InitializeComponent(); var flp = new FlowLayoutPanel { Width = 216, Height = 117, Location = new Point(10, 10), Font = new Font("Microsoft Sans Serif", 14) }; Controls.Add(flp); // Put the labels to the dictionary for better processing. var labels = new Dictionary<string, string> { { "Size:", "0.000 MB" }, { "Downloaded:", "0.000 MB" }, { "Images left:", "0" }, { "Speed:", "0.000 MB/s" }, }; // Go through each label foreach (var line in labels) { // Each line consists of two labels: Text + Value // Each label has its own color. var keyLabel = new Label() { Text = line.Key, ForeColor = Color.Green }; var valueLabel = new Label() { Text = line.Value, ForeColor = Color.Red }; // Save the value label to the appropriate property switch (line.Key) { case "Size:": lblSizeValue = valueLabel; break; case "Downloaded:": lblDownloadedValue = valueLabel; break; case "Images left:": lblImagesLeftValue = valueLabel; break; case "Speed:": lblSpeedValue = valueLabel; break; } flp.Controls.Add(keyLabel); flp.Controls.Add(valueLabel); // Break after each line flp.SetFlowBreak(valueLabel, true); } } public void UpdateDownloadProgress(double totalBytes, double downloadedBytes, int imagesLeft, double speed) { string[] sizes = { "B", "KB", "MB", "GB" }; string totalSizeText = $"{totalBytes:N0} bytes"; string downloadedSizeText = $"{downloadedBytes:N0} bytes"; int totalSizeIndex = 0; while (totalBytes >= 1024 && totalSizeIndex < sizes.Length - 1) { totalBytes /= 1024; totalSizeIndex++; } totalSizeText = $"{totalBytes:N2} {sizes[totalSizeIndex]}"; int downloadedSizeIndex = 0; while (downloadedBytes >= 1024 && downloadedSizeIndex < sizes.Length - 1) { downloadedBytes /= 1024; downloadedSizeIndex++; } downloadedSizeText = $"{downloadedBytes:N2} {sizes[downloadedSizeIndex]}"; // Update the labels with the new values lblSizeValue.Text = totalSizeText; lblDownloadedValue.Text = downloadedSizeText; lblImagesLeftValue.Text = imagesLeft.ToString(); lblSpeedValue.Text = $"{speed:F3} MB/s"; } } }
将该UserControl拖入Form1设计器后,“Downloaded:”标签仅显示“Download”,“Images left:”标签仅显示“Images”,无法看到完整文本。调整控件大小后问题仍未解决。
解决办法
1. 开启Label的自动尺寸适应
创建Label时显式设置AutoSize = true,让Label根据文本内容自动调整宽度,避免截断:
var keyLabel = new Label() { Text = line.Key, ForeColor = Color.Green, AutoSize = true }; var valueLabel = new Label() { Text = line.Value, ForeColor = Color.Red, AutoSize = true };
2. 调整FlowLayoutPanel宽度(可选)
如果开启AutoSize后,内部控件总宽度超过了FlowLayoutPanel的设定宽度,需要适当调大其Width值,确保有足够空间容纳文本:
var flp = new FlowLayoutPanel { Width = 300, // 增大宽度,适配长文本 Height = 117, Location = new Point(10, 10), Font = new Font("Microsoft Sans Serif", 14) };
3. 确保UserControl自身尺寸足够
在UserControl构造函数末尾,设置自身自动适应尺寸,或者手动指定足够的大小:
this.AutoSize = true; // 或者手动设置: // this.Size = new Size(flp.Width + 20, flp.Height + 20);
内容的提问来源于stack exchange,提问作者Daniel Lip
相关产品推荐
相关产品推荐

