Change button content in WPF - c#

I have:
private void button1_MouseEnter(object sender, EventArgs e)
{
for (int i = 0; i > 2; i++)
{
button1.Content = Convert.ToString(i);
System.Threading.Thread.Sleep(1000);
}
tekst.Text = "Mouse Enter";
}
When I enter on Button I see only Mouse Enter, but Content on Button don't change. Why? What I can do wrong?

Hi is your for loop correct? It should be i<2 instead of i>2
for (int i = 0; i < 2; i++)
{

Your for loop never execute because you have wrong condition, change it to following code:
for (int i = 0; i < 2; i++)
Also you should use BackgroundWorker (msdn) to update your GUI dynamicly.
private void button1_MouseEnter(object sender, MouseEventArgs e)
{
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += delegate
{
for (int i = 0; i < 2; i++)
{
this.Dispatcher.Invoke((Action)(() => { btn.Content = Convert.ToString(i); }));
System.Threading.Thread.Sleep(1000);
}
};
worker.RunWorkerCompleted += delegate { tekst.Text = "Mouse Enter"; };
worker.RunWorkerAsync();
}

Related

Incrementing Text in Circular Progress Bar

private void _btnOK_Click(object sender, EventArgs e)
{
_label1.Hide();
_label2.Hide();
_label3.Hide();
for(int i = 1; i <= 100; i++)
{
Thread.Sleep(5);
_circularprogressbar.Value = i;
_circularprogressbar.Update();
}
}
private void LoadingScreen_Load(object sender, EventArgs e)
{
_circularprogressbar.Value = 0;
_circularprogressbar.Minimum = 0;
_circularprogressbar.Maximum = 100;
}
}
}
This is my code. What i want to do is, i want to have a text inside the progress bar that shows the percentage of the progress from 1 to 100 percent.
what can i add to my code?
thank you
Here is what i would do:
private void _btnOK_Click(object sender, EventArgs e)
{
_label1.Hide();
_label2.Hide();
_label3.Hide();
for(int i = 1; i <= 100; i++)
{
_circularprogressbar.Value = i;
_percent_lable_name.Text = string.Format("{0}%", _circularprogressbar.Value);
_circularprogressbar.Update();
}
}
private void LoadingScreen_Load(object sender, EventArgs e)
{
_circularprogressbar.Value = 0;
_circularprogressbar.Minimum = 0;
_circularprogressbar.Maximum = 100;
}
}
See if that helps you!
Thanks
Techcraft7 :)
That Thread.Sleep(5) is blocking your entire UI thread. If you want to have your UI responsive, while the progress takes place, you need to make a separate thread for it. Something like this:
private void _btnOK_Click(object sender, EventArgs e)
{
_label1.Hide();
_label2.Hide();
_label3.Hide();
Task.Factory.StartNew(() =>
{
for (int i = 1; i <= 100; i++)
{
Thread.Sleep(5);
Invoke((Action)(() =>
{
_circularprogressbar.Value = i;
_circularprogressbar.Update();
}));
}
});
}
Note that you will need t use Invoke to BeginInvoke to access UI components from inside that thread.

fix progress bar in WinForms

The problem is the label gets displayed before the progress-bar is completed. How do I make the label display only after the progress-bar is fully complete?
I tried changing the max values to a higher number but it didn't work.
public partial class dload : Form
{
public dload()
{
InitializeComponent();
}
private void dload_Load(object sender, EventArgs e)
{
label1.Visible = false;
}
private void button1_Click(object sender, EventArgs e)
{
int i = 0;
progressBar1.Minimum = 0;
progressBar1.Maximum = 5000;
for (i = 0; i <= 5000; i++)
{
progressBar1.Value = i;
if (i == 5000)
{
label1.Visible = true;
}
}
}
}
Actually, Your code run very vast, it is about less than a second to set value from 0% to 100% ! But
ProgressBar has two styles for displaying the current status (Classic and Continues).
In Continues mode if the progress value went to 100% from 0% the control will show an animation, which is not display the real and accurate progress. You can set a delay via Thread.Sleep() and show your label immediately after the for loop to find out to what i happening !
The Below code will work:
private void button1_Click(object sender, EventArgs e)
{
int i = 0;
progressBar1.Minimum = 0;
progressBar1.Maximum = 5000;
for (i = 0; i <= 5000; i++)
{
Thread.Sleep(1);
progressBar1.Value = i;
}
label1.Visible = true;
}
This is an animation problem. A "hack" around it is to actually decrease the progress value by 1:
progressBar1.Minimum = 0;
progressBar1.Maximum = 5000;
for (int i = 0; i < progressBar1.Maximum; i++) {
progressBar1.Value = i;
progressBar1.Value = Math.Max(i - 1, progressBar1.Minimum);
}
label1.Visible = true;
use progressBar1.Refresh:
public partial class dload : Form
{
public dload()
{
InitializeComponent();
}
private void dload_Load(object sender, EventArgs e)
{
label1.Visible = false;
}
private void button1_Click(object sender, EventArgs e)
{
int i = 0;
progressBar1.Minimum = 0;
progressBar1.Maximum = 5000;
for (i = 0; i <= 5000; i++)
{
progressBar1.Value = i;
progressBar1.Refresh();
if (i == 5000)
{
label1.Visible = true;
}
}
}

How can i empty my progressbar after its full

How can i set my progressBar value to 0 if its full when i click the button it loads so fast i cant see anything
my code is
private void button1_Click(object sender, EventArgs e)
{
int i;
progressBar1.Minimum = 0;
progressBar1.Maximum = 100;
for (i = 0; i <= 100; i++)
{
progressBar1.Value = i;
if (progressBar1.Value == 100)
{
progressBar1.Value = 0;
}
Refresh();
}
}
Is this what you mean?
private async void button1_Click(object sender, EventArgs e)
{
int i;
progressBar1.Minimum = 0;
progressBar1.Maximum = 100;
for (i = 0; i <= 100; i++)
{
progressBar1.Value = i;
}
await Task.Delay(2000);
if (progressBar1.Value == 100)
{
progressBar1.Value = 0;
}
}
Edit this will now wait 2 seconds until it sets it to empty so you can see that the progress bar does increment to 100.
Your code does exactly what you require.
You may should add a sleep to see something.
private void button1_Click(object sender, EventArgs e)
{
if(IWantToClearTheProgressbarBeforeWork == true)
progressBar1.Value = 0;
int i;
progressBar1.Minimum = 0;
progressBar1.Maximum = 100;
for (i = 0; i <= 100; i++)
{
progressBar1.Value = i;
Thread.Sleep(100); // only for testing. Blocks your thread.
}
if(IWantToClearTheProgressbarImediatelyAfterWorkIsDone == true)
progressBar1.Value = 0;
}

Updating array of labels from backgroundworkers

I am trying to update an array of Labels which are on a form from a backgroundworker. Here is my code:
for (int i = 0; i < 6; i++)
{
if (this.InvokeRequired)
{
this.Invoke((MethodInvoker)delegate
{
arrLabel[i].Text = values[i].ToString();
});
}
else
{
arrLabel[i].Text = values[i].ToString();
}
}
This does not work, but if I try to change text property of each label instead of the array, it works. How can I fix this? Also is there a shorter/better way of updating form controls from backgroundworkers than what I am doing for every single control on my form?
Edit: here is how I defined the array:
private Label[] arrLabel = new Label[6];
and here is the function that I call to assign the array:
private void makeLabelArrays()
{
for (int i = 0; i < 6; i++)
{
arrLabel[i] = (Label)Groupbox1.Controls["label" + (i + 1).ToString()];
}
}
I'm assuming what some of your code looks like; I may be wrong.
You can use the ReportProgress() method to send two pieces of information back to the UI thread - the percentage of completeness (doesn't really apply in your case so I specified 0) and some piece of data (any object you want, just a number in this case).
Then you can get the data in the ProgressChanged event and execute code that touches the UI.
private List<Label> arrLabel = new List<Label>();
private List<string> values = new List<string>();
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
var bw = (BackgroundWorker)sender;
for (int i = 0; i < 6; i++)
bw.ReportProgress(0, i);
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
var currentIndex = Convert.ToInt32(e.UserState);
arrLabel[currentIndex].Text = values[0].ToString();
}
Make sure you enable reporting progress, as it's disabled by default.
backgroundWorker1.WorkerReportsProgress = true;
try the following
private delegate void delegateAssignText();
public void AssignText()
{
if (this.InvokeRequired)
{
Invoke(new delegateAssignText(AssignText));
return;
}
for (int i = 0; i < 6; i++)
{
arrLabel[i].Text = values[0].ToString();
}
}

I want show text in textbox continually

for(int a=0;a<10;a++)
{
txtblck =txtblk+ a.ToString();
}
In this, txtbox show all the text at the last.
I want to show the text one by one.
You can try this
for (int a = 0; a < 10; a++)
{
txtblck.Text = txtblck.Text + a.ToString();
Application.DoEvents();
System.Threading.Thread.Sleep(1000);
}
This is happening because the for loop is running so fast that you cannot actually see the text of your TextBox changing. Use the System.Threading.Thread.Sleep method to pause the loop for a while so you can watch the text changing:
for(int a = 0; a < 10; a++)
{
txtblck =txtblk + a.ToString();
System.Threading.Thread.Sleep(1000);
}
Try something like this: (I assumed you were using WPF but any timer will do)
System.Windows.Threading.DispatcherTimer timer;
int a, count;
void start() {
timer = new System.Windows.Threading.DispatcherTimer();
a = 0;
count = 10;
timer.Tick += timer_Tick;
timer.Interval = new TimeSpan(0, 0, 1);
timer.Start();
}
void timer_Tick(object sender, EventArgs e) {
updateString();
}
void updateString() {
if (a < count) {
txtblck.Text += a.toString();
a++;
}
else {
timer.Stop();
}
}

Categories