silly question

  • Thread starter Thread starter CSharper
  • Start date Start date
C

CSharper

I wrote a simple WPF application, which has label and a progress bar
and button. When I click the button I change the label with a new
value and change progress bar value. When I debug the code, I can see
the value changing but not on the GUI. Could someone tell me what am I
missing?
 
CSharper said:
I wrote a simple WPF application, which has label and a progress bar
and button. When I click the button I change the label with a new
value and change progress bar value. When I debug the code, I can see
the value changing but not on the GUI. Could someone tell me what am I
missing?

Practically impossible to say without a concise-but-complete code
example that reliably reproduces the problem.

Are you sure you're actually setting the properties correctly? That is,
setting the properties representing the values you really want to
update, on the correct objects representing the GUI you want to be changed?

Pete
 
Practically impossible to say without a concise-but-complete code
example that reliably reproduces the problem.

Are you sure you're actually setting the properties correctly?  That is,
setting the properties representing the values you really want to
update, on the correct objects representing the GUI you want to be changed?

Pete

I ended up changing the code little bit more than what I explained,
regardless here is the code


private void button1_Click(object sender, RoutedEventArgs e)
{
DirectoryInfo directoryDetail = new
DirectoryInfo(driveLetter.Text);
progressBar1.Minimum = 0;
progressBar1.Maximum =
directoryDetail.GetDirectories().Length;
TreeViewItem item = new TreeViewItem();
item.Header = driveLetter.Text;
directoryTree.Items.Add(item);
label1.Content = driveLetter.Text;
BuildTree(directoryDetail, item);
}

private void BuildTree(DirectoryInfo directoryDetail,
TreeViewItem parent)
{
foreach (string dirName in
Directory.GetDirectories(directoryDetail.FullName))
{
TreeViewItem item = new TreeViewItem();
item.Header = dirName;
parent.Items.Add(item);
DirectoryInfo dInfo = new DirectoryInfo(dirName);
try
{
if (dInfo.GetDirectories().Length > 0)
{
BuildTree(dInfo, item);
}
if (parent.Header.ToString() ==
driveLetter.Text.ToString())
{
progressBar1.Value += 1;
label1.Content = dirName;
progressBar1.InvalidateVisual();
label1.InvalidateVisual();
}
}
catch
{
}
}
foreach(string fileName in
Directory.GetFiles(directoryDetail.FullName))
{
TreeViewItem fileitem = new TreeViewItem();
fileitem.Header = fileName;
parent.Items.Add(fileitem);
}
}
 
CSharper said:
I ended up changing the code little bit more than what I explained,
regardless here is the code

You may find these links useful:
http://www.yoda.arachsys.com/csharp/complete.html
http://www.yoda.arachsys.com/csharp/incomplete.html
http://sscce.org/

That said, just looking at the code you posted, it appears you have
committed the classic error of blocking your GUI thread.

If you have a lengthy processing task, you need to execute that task on
a different thread (e.g. by using the BackgroundWorker class), so that
the GUI thread can still respond to changes and update the display as
necessary.

Pete
 
CSharper said:
I wrote a simple WPF application, which has label and a progress bar
and button. When I click the button I change the label with a new
value and change progress bar value. When I debug the code, I can see
the value changing but not on the GUI. Could someone tell me what am I
missing?

I have a bigger piece of advice for you mate.

You're trying to work with WPF the wrong way.
Round peg, square hole.
Bash it enough and it'll work but it's not a GOOD way to work.

Take a look at MVVM.
Unlike a lot of patterns/methodologies you can adopt it for all but the most
trivial applications without adding overhead,
It arguably makes this sort of thing simpler.

Whenever you think of changing a property which is in your view ( screen )
think binding.
So your viewmodel would expose a progress property that the control is bound
to.

There are good examples and vids out there on the web.
 
Back
Top