C#如何使标签的第一个字母可见
我已经创建了一个Windows窗体应用程序,我正在使用C#如何使标签的第一个字母可见,c#,winforms,C#,Winforms,我已经创建了一个Windows窗体应用程序,我正在使用label_1.Visible=false使我的标签不可见 我只想让标签的第一个字母可见 我该怎么做呢?字符串从技术上讲是字节数组,这意味着每个字母都可以通过索引访问 例如: string x = "cat"; char y = x[0]; // y now has a value of 'c'! 对用于标签的字符串执行此操作,并将结果用于标签。我还想补充一点,您需要设置label_1.Visible=true否则将不会显示任何内容 将上述
label_1.Visible=false代码>使我的标签不可见
我只想让标签的第一个字母可见
我该怎么做呢?字符串从技术上讲是字节数组,这意味着每个字母都可以通过索引访问
例如:
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();
希望这对你有用 可见性是全有或全无的概念:如果标签或任何其他组件被标记为不可见,则所有标签或组件都不会出现在表单上
如果只想在标签中显示字符串的前几个字母
,请使用子字符串
方法指定标签的文本。为了使其正常工作,实际文本必须存储在标签之外的某个位置-例如,在labelText
字段中:
private string labelText = "Quick brown fox";
...
label_1.Text = labelText.Substring(0, 1); // Only the first character is shown
根据您对一条评论的回答,听起来您好像对字幕式显示感兴趣。这里有一种方法,将整个字符串存储在一个变量中,然后仅在标签中显示部分字符串
在下面的示例中,我们在变量中存储了一个要显示的文本字符串。我们添加了一个标签来显示文本,并使用一个计时器来反复更改文本,使其显示为滚动
要查看它的运行情况,请启动一个新的Windows窗体应用程序项目,并用以下代码替换部分窗体类:
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();
}
}
label_1.Text=label_1.Text[0]代码>足够好了?你想喝点马基酒吗?一个字母一个字母地显示等等?@Guman,是的,我想要this@RandRandom您的代码不起作用:/@NoobGuy123只是一个一般的想法,而不是一个功能齐全的代码,但现在您可以使用label_1.Text=label_1.Text[0].ToString()代码>