[英]C# How to make visible first letter of label
I have created a Windows Forms application and I am using label_1.Visible = false;
我已经创建了Windows窗体应用程序,并且正在使用
label_1.Visible = false;
to make my label invisible. 使我的标签不可见。
I want to only make the first letter of the label visible. 我只想使标签的第一个字母可见。
How can I do it? 我该怎么做?
Visibility is all-or-nothing concept: if a label, or any other component for that matter, is marked invisible, none of it is going to appear on the form. 可见性是全有或全无的概念:如果标签或与此相关的任何其他组件被标记为不可见,则所有标签都不会出现在表单上。
If you want to show only the first few letters of a string
in a label, use Substring
method to assign label's text. 如果只想显示标签中
string
的前几个字母,请使用Substring
方法分配标签的文本。 In order for this to work, the actual text must be stored somewhere outside the label - say, in labelText
field: 为了
labelText
起作用,必须将实际文本存储在标签外部的某个位置,例如,在labelText
字段中:
private string labelText = "Quick brown fox";
...
label_1.Text = labelText.Substring(0, 1); // Only the first character is shown
Based on your answer to a comment, it sounded like you were interested in a Marquee-style display. 根据您对评论的回答,听起来好像您对Marquee样式的显示感兴趣。 Here's one way to do that, by storing the whole string in one variable, and then only displaying parts of it in a label.
这是一种方法,将整个字符串存储在一个变量中,然后仅将其一部分显示在标签中。
In the example below, we have a string of text to display stored in a variable. 在下面的示例中,我们有一个要显示的文本字符串存储在一个变量中。 We add a label to display the text, and a timer is used to repeatedly change the text to make it appear that it's scrolling.
我们添加了一个标签来显示文本,并且使用计时器来重复更改文本以使其看起来像在滚动。
To see it in action, start a new Windows Forms Application project and replace the partial form class with the following code: 要查看它的实际效果,请启动一个新的Windows Forms Application项目,并将部分表单类替换为以下代码:
public partial class Form1 : Form
{
// Some text to display in a scrolling label
private const string MarqueeText =
"Hello, this is a long string of text that I will show only a few characters at a time. ";
private const int NumCharsToDisplay = 10; // The number of characters to display
private int marqueeStart; // The start position of our text
private Label lblMarquee; // The label that will show the text
private void Form1_Load(object sender, EventArgs e)
{
// Add a label for displaying the marquee
lblMarquee = new Label
{
Width = 12 * NumCharsToDisplay,
Font = new Font(FontFamily.GenericMonospace, 12),
Location = new Point {X = 0, Y = 0},
Visible = true
};
Controls.Add(lblMarquee);
// Add a timer to control our marquee and start it
var timer = new System.Windows.Forms.Timer {Interval = 100};
timer.Tick += Timer_Tick;
timer.Start();
}
private void Timer_Tick(object sender, EventArgs e)
{
// Figure out the length of text to display.
// If we're near the end of the string, then we display the last few characters
// And the balance of characters are taken from the beginning of the string.
var startLength = Math.Min(NumCharsToDisplay, MarqueeText.Length - marqueeStart);
var endLength = NumCharsToDisplay - startLength;
lblMarquee.Text = MarqueeText.Substring(marqueeStart, startLength);
if (endLength > 0) lblMarquee.Text += MarqueeText.Substring(0, endLength);
// Increment our start position
marqueeStart++;
// If we're at the end of the string, start back at the beginning
if (marqueeStart > MarqueeText.Length) marqueeStart = 0;
}
public Form1()
{
InitializeComponent();
}
}
Strings are technically byte arrays, meaning each letter can be accessed with an index. 从技术上讲,字符串是字节数组,这意味着每个字母都可以通过索引进行访问。
For example: 例如:
string x = "cat";
char y = x[0];
// y now has a value of 'c'!
Perform this on the string being used for your label and use the result for your label instead. 在用于标签的字符串上执行此操作,然后将结果用于标签。 I want to also add that you need to set
label_1.Visible = true;
我还要补充一点,您需要设置
label_1.Visible = true;
otherwise nothing will appear at all. 否则,什么都不会出现。
Applying the above to your code, you should arrive at something like this: 将以上内容应用到您的代码中,您应该得到如下所示的内容:
label_1.Visible = true;
label_1.text = label_1.text[0].ToString();
Hope that works for you! 希望对您有用!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.