我試圖在我的應用程序中顯示文本。輸入是我接受使用FileUpload的文本文件。我必須將文本分成單詞,並將每個單詞分解爲相應的字符。然後,每個單詞首先以字母順序顯示,然後整個單詞顯示在一個字幕中。在asp.net和c#中顯示文本時添加延遲#
到目前爲止,我已設法接受該文件。我也設法分割內容。但我無法知道如何在顯示每個字母后添加延遲。另外,顯示每個字符後,它必須在下一個字符出現之前消失。我嘗試使用Thread.Sleep(),但它所做的只是凍結我的UI,使線程在開始時處於睡眠狀態,並一次顯示所有內容。
同上Task.Delay()。
有人告訴我使用計時器,但我是C#編程新手,不知道它們是如何工作的。到目前爲止,這是我所做的。第一個按鈕(即Button1
)按原樣顯示文件的內容。第二個按鈕(即Button2
)用於分割文本。
aspx.cs文件:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
String a = FileUpload1.FileName;
String ext = Path.GetExtension(a);
//Label1.Text = ext;
if (ext == ".txt")
{
System.IO.StreamReader reader = new System.IO.StreamReader(FileUpload1.FileContent);
string text = reader.ReadToEnd();
//Response.Write(text);
TextBox1.Text = text;
//Above section for text file
}
if (ext == ".docx" || ext == ".doc")
{
//String b = FileUpload1.PostedFile.FileName;
// Open a doc file.
string filename = Path.GetFileName(FileUpload1.FileName);
FileUpload1.SaveAs(Server.MapPath("~/") + filename);
Application application = new Application();
Document document = application.Documents.Open(Server.MapPath("~/") + filename);
// Loop through all words in the document.
int count = document.Words.Count;
for (int i = 1; i <= count; i++)
{
// Write the word.
string text = document.Words[i].Text;
//Response.Write(text);
TextBox1.Text = text;
}
// Close word file
application.Quit();
}
}
protected void Button2_Click(object sender, EventArgs e)
{
String a1 = FileUpload1.FileName;
String ext1 = Path.GetExtension(a1);
if (ext1 == ".txt")
{
System.IO.StreamReader reader = new System.IO.StreamReader(FileUpload1.FileContent);
string text = reader.ReadToEnd();
/*foreach (char c in text)
{
TextBox2.Text = c.ToString();
System.Threading.Thread.Sleep();
}*/
List<string> list = new List<string>(); //code for splitting
String[] words = text.Split();
for (int i = 0; i < words.Length; i++)
{
list.Add(words[i]);
}
foreach (string word in words)
{
Char[] letters = word.ToCharArray();
foreach (char letter in letters)
{
Response.Write("<marquee>"+letter+"</marquee>");
Response.Write("<br>");
}
Response.Write("<marquee>" + word + "</marquee>");
Response.Write("<br>");
}
}
}
,這就是你應該把代碼做延遲...在Javascript ,基本上。首先將所有數據發送給客戶端,然後使用Javascript在適當的時間顯示它。 –