C#如何使标签的第一个字母可见

3

我创建了一个Windows Forms应用程序,并使用label_1.Visible = false;将标签隐藏。

我想仅使标签的第一个字母可见。

如何实现?


1
"label_1.Text = label_1.Text[0];" 这样行吗? - Rand Random
@Guman,是的,我想要这个。 - NoobGuy123
@RandRandom,你的代码不起作用 :/ - NoobGuy123
@NoobGuy123 我们需要更多的信息。为什么它不起作用?它现在做了什么,你不想让它这样做?有什么改变吗? - Capn Jack
@NoobGuy123 你还有这个问题吗? - Rand Random
显示剩余5条评论
3个回答

4

可见性是一个全有或全无的概念:如果标签,或者其他任何组件被标记为不可见,则它们都不会出现在表单中。

如果您想在标签中仅显示字符串的前几个字母,请使用Substring方法来分配标签的文本。为了使其起作用,实际文本必须存储在标签之外的某个地方——比如labelText字段中:

private string labelText = "Quick brown fox";
...
label_1.Text = labelText.Substring(0, 1); // Only the first character is shown

1

根据您对评论的回答,听起来您对跑马灯式的显示很感兴趣。以下是一种方法,通过将整个字符串存储在一个变量中,然后仅在标签中显示部分字符串来实现。

在下面的示例中,我们有一个要显示的文本字符串存储在一个变量中。我们添加一个标签来显示文本,并使用计时器重复更改文本,使其看起来像是在滚动。

要查看它的实际效果,请启动一个新的 Windows Forms 应用程序项目,并将部分类表替换为以下代码:

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();
    }
}

0

字符串在技术上是字节数组,这意味着每个字母都可以通过索引访问。

例如:

string x = "cat";
char y = x[0];
// y now has a value of 'c'!

对用于标签的字符串执行此操作,并使用结果替换标签。我还想补充一点,您需要设置label_1.Visible = true;否则将不会显示任何内容。

将上述内容应用于您的代码,您应该得到类似以下的结果:

label_1.Visible = true;
label_1.text = label_1.text[0].ToString();

希望这对你有用!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接