簡體   English   中英

C#如何使標簽的第一個字母可見

[英]C# How to make visible first letter of label

我已經創建了Windows窗體應用程序,並且正在使用label_1.Visible = false; 使我的標簽不可見。

我只想使標簽的第一個字母可見。

我該怎么做?

可見性是全有或全無的概念:如果標簽或與此相關的任何其他組件被標記為不可見,則所有標簽都不會出現在表單上。

如果只想顯示標簽中string的前幾個字母,請使用Substring方法分配標簽的文本。 為了labelText起作用,必須將實際文本存儲在標簽外部的某個位置,例如,在labelText字段中:

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

根據您對評論的回答,聽起來好像您對Marquee樣式的顯示感興趣。 這是一種方法,將整個字符串存儲在一個變量中,然后僅將其一部分顯示在標簽中。

在下面的示例中,我們有一個要顯示的文本字符串存儲在一個變量中。 我們添加了一個標簽來顯示文本,並且使用計時器來重復更改文本以使其看起來像在滾動。

要查看它的實際效果,請啟動一個新的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();
    }
}

從技術上講,字符串是字節數組,這意味着每個字母都可以通過索引進行訪問。

例如:

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

希望對您有用!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM