I have created a Windows Forms application and I am using 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. In order for this to work, the actual text must be stored somewhere outside the label - say, in labelText
field:
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. 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:
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;
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!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.