c#


Windows Forms show string letter by letter


So in my visual C# project I have a TextBox, where a system response gets shown, and I want it to be displayed adding characters one by one, not the whole string at the same time. I want to create some kind of typing illusion.
For now I have:
for (int i = 0; i <= response.Length; i++)
{
textBox2.Text = response.Substring(0, i);
Thread.Sleep(100);
}
But this one waits the whole given time and then gives out the whole string.
What am I doing wrong?
Use asynchrony:
for (int i = 0; i <= response.Length; i++)
{
textBox2.Text = response.Substring(0, i);
await Task.Delay(100);
}
Don't forget to add the async keyword to scope.
This is just sample (written with WPF) how can you achieve this. You can easily adopt it for winforms.
var form = new Window();
var tb = new TextBox();
form.Content = tb;
form.Show();
var str = "alk;lfkdsfj;slfhjs;idjhf;lksdjf;klsdjf;'lkjds;lfksd";
Task.Run(() =>
{
foreach (var c in str.ToCharArray())
{
Thread.Sleep(100);
form.Dispatcher.Invoke(() =>
{
tb.Text += c;
});
}
});
UPD.
For windows forms it can be a tricky and more complicated that async/await answer. But, anyway, I can show how to do this without async stuff
void Main()
{
var form = new MyForm();
form.Show();
}
class MyForm : Form
{
private TextBox tb;
public MyForm()
{
tb = new TextBox();
tb.Width = 300;
this.Controls.Add(tb);
var btn = new Button();
btn.Text = "go";
btn.Width = 300;
btn.Location = new System.Drawing.Point(0, 50);
this.Controls.Add(btn);
btn.Click += (sender, args) =>
{
tb.Text = string.Empty;
var str = "alk;lfkdsfj;slfhjs;idjhf;lksdjf;klsdjf;'lkjds;lfksd";
SetText d = SetTextToTb;
Task.Run(() =>
{
foreach (var c in str.ToCharArray())
{
Thread.Sleep(100);
tb.Invoke(d, c);
}
});
};
}
public delegate void SetText(char text);
void SetTextToTb(char text)
{
tb.Text += text;
}
}

Related Links

Implementing IDisposable in a class exposing Begin/End methods
How to cancel node selection in nodemouseclick event
Implementing a collection initializer for my List wrapper in C# [duplicate]
How to scan a directory with wildcard with a specific subdirectory
Display Position of an Item in a List
C# shared folder access gives Logon failure exception
How to set System.Drawing.Image to System.ui.WebControls.Image in datalist template item
Could not load file or assembly CrystalDecisions.VSDesigner Version=10.5.3700.0
How to add certificate rule through command line or programmatically?
Not reading the content of files in wpf [closed]
Binding Visibility Converter in WPF C#
Setting alias name for the gridview field
Force GC to use another thread-context
C# code to get maximum dpi of installed or selected printer
Eventhandler not removed with new database
how to create a custom list c#

Categories

HOME
drupal
max
logging
oop
ide
domain-driven-design
packet
casting
dronekit
intellisense
python-requests
latex
ms-application-insights
terminal
uml-designer
travis-ci-cli
sitemap
segmentation-fault
wkhtmltopdf
phpexcel
minimax
mongoimport
logstash-configuration
jinja2
console-application
nsoperation
powerpivot
beacon
draw2d
legend-properties
xna
combinations
fibonacci
connectiq
mattermost
screen-scraping
suitescript
scalatest
mongoose-im
cloudformation
mex
r-factor
exacttarget
tikz
eclipse-jdt
voyager
kofax
console.readline
rtsp
word-wrap
instances
latency
ngrok
linkurious
ninja-forms
geminabox
lucee
cloudconvert
sharpdevelop
playframework-1.x
xamarin-test-cloud
overhead
spring-cloud-aws
robotc
computed-properties
turbolinks
qtruby
bytearray
angularfire
aws-kms
pebble-js
containstable
dynamics-nav-2013
github-for-mac
tvp
javapackager
transbase
edgar
dnvm
polarion
esb-toolkit-2.1
tidesdk
gmaps4jsf
accpac
api-eveonline
system.net
cocos2d-x-2.x
step
amzi-prolog
code-smell
slimscroll
data-layers
.net-reflector
jfilechooser
randomaccessfile
8-puzzle
zend-db-table
infinity.js
removeall
vline
cryptarithmetic-puzzle
correctness
hypertable
pinchzoom
stl-algorithm
virtual-printer
httppostedfilebase
llblgen
conditional-operator
text-manipulation
numerical-computing
lts
script-tag
bootstrap-protocol
unauthorized
moores-law

Resources

Encrypt Message