C# WPF - How to auto refresh the UI after run it - c#

I created in the XAML a simple Label called TbTimer
I made the following code:
class Level2
{
public Level2()
{
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += timer_Tick;
}
public int counter;
public void timer_Tick(object sender, EventArgs e)
{
counter++;
}
public DispatcherTimer timer;
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
lvl2 = new Level2();
}
private void MenuItemMedium_Click(object sender, RoutedEventArgs e)
{
lvl2.timer.Start();
TbTimer.Content = lvl2.counter.ToString();
}
}
Then I have another button, and I call TimerUpdater when that button is clicked.
When I run the program and I click the button, I can see that the content of the TextBlock shows the number 1... and it does not continue to run the numbers - when I click the button again after 5 seconds it shows the number 6.
So I guess the timer is running fine behind the scenes, but the content of the TextBlock is updated only when I click the button.
What should I do to make the TextBlock content update the seconds without clicking the button? Hope my explanation and question is clear.

With your modified code, the answer changes completely. I apologize for the dramatic change in content. The "simplest" way to accomplish this would be to add an event for the counter updating and have your UI subscribe to it. Something like:
class Level2
{
public event Action<int> CounterUpdated;
...
public void timer_Tick(object sender, EventArgs e)
{
counter++;
if (CounterUpdated != null)
CounterUpdated(counter);
}
}
public class MainWindow
{
public MainWindow()
{
InitializeComponent();
lvl2 = new Level2();
lvl2.CounterUpdated += UpdateCounterText;
}
private void MenuItemMedium_Click(object sender, RoutedEventArgs e)
{
lvl2.timer.Start();
}
private void UpdateCounterText(int newCounterValue)
{
TbTimer.Content = newCounterValue.ToString();
}
}
Incidentally, this ends up being similar to how the binding system is set up. If you just bound your textbox to the counter variable, it would be much cleaner and easier to use. To do that, you would change your XAML to:
<TextBox Name="TbTimer" Text="{Binding Counter}"/>
and assign the DataContext:
public class MainWindow
{
public MainWindow()
{
InitializeComponent();
lvl2 = new Level2();
DataContext = lvl2;
}
private void MenuItemMedium_Click(object sender, RoutedEventArgs e)
{
lvl2.timer.Start();
}
}
Level2 now needs to implement INotifyPropertyChanged, and you have to make counter a property (so it can be bound to):
class Level2 : INotifyPropertyChanged
{
//Notify Property Changed Implementation from MSDN:
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private int counter = 0;
public int Counter
{
get { return counter; }
set
{
counter = value;
NotifyPropertyChanged();
}
}
...
public void timer_Tick(object sender, EventArgs e)
{
Counter++;
}
}
The binding system will now update the text box automatically when the timer ticks (and increments the Counter property. This is the way it should be done in WPF, so feel free to ask any questions that come up when implementing it.
For reference, this is the implemenation of INofityPropertyChanged I used: http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx

Related

How to get results in real time from a UserControl?

I am building a Windows Forms App, where I created a UserControl (called MyControl).
This UserControl can generate and return a value using a BackgroundWorker.
My question is, how can my form1 show in real-time the value produced by the UserControl when the calculation is completed?
public partial class MyControl : UserControl
{
BackgroundWorker bgWorker;
private string _num;
public string Num
{
get
{
return _num;
}
set
{
_num = value;
}
}
public MyControl()
{
InitializeComponent();
bgWorker = new BackgroundWorker();
bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgWorker_RunWorkerCompleted);
}
void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
this.BeginInvoke((MethodInvoker)delegate
{
//Done
MessageBox.Show(e.Result.ToString());
//↑↑↑↑↑ HERE
//I want to return to form1
// like to rewrite: form1.textbox1.text = e.Result.ToString();
});
btnStartAsyncOperation.Enabled = true;
btnCancel.Enabled = false;
}
void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
string Num = A_huge_calculation();
e.Result = Num;
}
private void RunCalculate(string Num)
{
if (bgWorker.IsBusy != true)
{
bgWorker.RunWorkerAsync(Num);
}
}
private void btnStartAsyncOperation_Click(object sender, EventArgs e)
{
RunCalculate(_num);
}
}
}
Why use BackgroundWorker? Because it will do an unpredictable thing(like generate the MD5 and SHA1 checksum for any file), and I don't wanna the main form fell asleep for end user.
Besides form2 will addition this UserControl like form1, and assign unique control to show the value.
form2.cs
public partial class Form2 : Form
{
MyControl mycontrol;
private void Form2_Load(object sender, EventArgs e)
{
mycontrol.SentTO = 'richtextBox1';
//It can be assign any container name to receive the value when completed.
//when trigger the execute button inside the MyControl
}
private void button1_Click(object sender, EventArgs e)
{
mycontrol = new MyControl();
mycontrol.Num = "xxxxxx";
//run mycontrol
//mycontrol.run();
richtextBox2.Text = mycontrol.value; //Here, when the MyControl calculation is completed.
}
}
I also have been thinking to set a timer listen to this user control get value every 1000ms.
form1.cs
private void timer1_Tick(object sender, EventArgs e)
{
if( ! string.IsNullOrEmpty(mycontrol.value))
{
richtextBox2.Text = mycontrol.value; //here is when the MyControl calculation is completed.
timer1.Stop();
}
}
The old ideal was to access the public value of MyControl by a timer,
when MyControl completed, it will return a non-null value, and the timer stop MyControl detection.
But it will derivative why the MyControl can know what is the timer name in form1?
Or form1 be set two or more MyControls, needs more timers to detect every MyControl.

Mousebuttonpressed event Issues C#

I have an issue with a custom event i have created. I have made a Usercontrol that looks the following:
public partial class UCListView : UserControl {
public UCListView() {
InitializeComponent();
}
public event EventHandler SubmitClick;
public event EventHandler MouseButtonUpEvent;
private void SubmitButton_OnClick(object sender, RoutedEventArgs e) {
if (SubmitClick != null)
SubmitClick(this, e);
}
private void MouseButtonUp(object sender, RoutedEventArgs e) {
if (MouseButtonUpEvent != null) {
MouseButtonUpEvent(this, e);
}
}
}
Here is the MouseButtonUp event i have.
The following is where i listen to the event:
public partial class RoundsteelWindow : WindowControls {
private UCListView uc;
public RoundsteelWindow() {
InitializeComponent();
uc = new UCListView();
uc.SubmitClick += new EventHandler(ButtonPressed);
uc.MouseButtonUpEvent += new EventHandler(MousePressed);
stkTest.Children.Add(uc);
base.Test<RoundSteel>(uc, "Roundsteel");
}
}
Here is the WindowControls, where the MousePressed method can be seen. This is the same as the code snippet beneath this code. Really don't see the issue:
public abstract class WindowControls : Window {
public IMaterialWith14Elements _ReturnObject { get; set; }
public double amount { get; set; }
private UCListView _uc;
public void Test<T>(UCListView uc, string type) where T: IMaterialWith14Elements, new() {
_uc = uc;
List<T> test = MaterialLogic.GetList(type) as List<T>;
foreach (T material in test) {
uc.listView.Items.Add(material.Name);
}
}
private string str;
public void MousePressed(object sender, EventArgs eventArgs) {
var item = (sender as ListView).SelectedItem;
if (item != null) {
_ReturnObject = _uc.listView.SelectedItems as FlatSteel ;
str = item.ToString();
_uc.amountText.IsEnabled = true;
}
}
public void ButtonPressed(object sender, EventArgs e) {
if (!string.IsNullOrEmpty(_uc.amountText.Text)) {
amount = _uc.amountText.Text.customParseToDouble();
this.Close();
}
else {
MessageBox.Show("Indtast venligst en værdi.");
}
}
}
Now the problem is the following: With the following code it is working, but this class is not using the windowcontrols. It is called by another class which handles all of the buttons.
private void flatsteelListView_PreviewMouseLeftButtonUp(object sender, RoutedEventArgs e) {
var item = (sender as ListView).SelectedItem;
if (item != null) {
_returnObject = flatsteelListView.SelectedItems as FlatSteel;
str = item.ToString();
amountTextbox.IsEnabled = true;
FindObject(str);
}
}
The first picture shows the working window. This is where there is not used a usercontrol. Actually this is a previous issue i have worked with and got help with here on stackoverflow Help for thisissue.
The second picture is showing the next window using the usercontrol that has been created. The button event works and closes the window. Here comes then the issue, when the listview item is pressed. It is doing the same thing as on the first picture(where it works), but it is giving me a null reference, which doesn't make any sense to me. I have also checked the object sender to see if there was a difference between the sender of these two different windows.
I simply can't figure out why this is not working.
greetings darophi
Your sender is an object of UCListView class which is inherited from UserControl and you are trying to use it like ListView. So as result of operation (sender as ListView) you get null because sender is not an instance of ListView class and not inherits it.

Calling a method of a parent page from UserController

I've followed this question and tried to build my solution. The problem is that 'UserControlButtonClicked' appears to be null! So 'UserControlButtonClicked(this, EventArgs.Empty)' inside the if, doesn't run, and the method 'addStepContent' in the parent page is never called.
UserControl 'StepsBar'
public sealed partial class StepsBar : UserControl
{
public event EventHandler UserControlAddStepContent;
[...]
public StepsBar()
{
this.InitializeComponent();
Image step_1 = new Image();
ButtonInfo step_1Info = new ButtonInfo();
step_1Info.Add((int)stepNumber.one, (int)stepStatus.normal);
step_1.Tag = step_1Info;
step_1.Source = setBackground((int)stepStatus.normal);
step_1.Tapped += stepTapped;
[...]
}
public void stepTapped(Object sender, RoutedEventArgs e)
{
[...]
if (step != null)
{
[...]
firePageEvent();
}
}
public void firePageEvent()
{
if (UserControlAddStepContent != null)
{
UserControlAddStepContent(this, EventArgs.Empty);
}
}
Parent Page
public Violation()
{
this.InitializeComponent();
StepsBar stepsBar = new StepsBar();
stepsBar.UserControlAddStepContent += new EventHandler(addStepContent);
}
private void addStepContent(object sender, EventArgs e)
{
CheckBox check_1 = new CheckBox();
check_1.Content = "Check me!";
bodyStackPanel.Children.Add(check_1);
}
This assumes that you want to use an existing delegate rather than make your own and you aren't passing anything specific to the parent page by event args.
In the user control's code-behind (adapt as necessary if not using code-behind or C#):
public partial class MyUserControl : System.Web.UI.UserControl
{
public event EventHandler UserControlButtonClicked;
private void OnUserControlButtonClick()
{
if (UserControlButtonClicked != null)
{
UserControlButtonClicked(this, EventArgs.Empty);
}
}
protected void TheButton_Click(object sender, EventArgs e)
{
// .... do stuff then fire off the event
OnUserControlButtonClick();
}
// .... other code for the user control beyond this point
}
In the page itself you subscribe to the event with something like this:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// hook up event handler for exposed user control event
MyUserControl.UserControlButtonClicked += new
EventHandler(MyUserControl_UserControlButtonClicked);
}
private void MyUserControl_UserControlButtonClicked(object sender, EventArgs e)
{
// ... do something when event is fired
}
}
Solved. The problem was this, on the parent page.
StepsBar stepsBar = new StepsBar();
stepsBar.UserControlAddStepContent += new EventHandler(addStepContent);
The istance of StepsBar was not added to the page. D'OH!
So here's what I've done:
stepsBar.UserControlAddStepContent += new EventHandler(addStepContent);
and on the xaml of the parent page:
<local:StepsBar x:Name="stepsBar"/>

No data is set when wpf window is loaded?

I have a issue with passing information from one wpf window to another. For some reason when main window is loaded nothing is set in the label, I need to be able to keep the data in a string to use for anything (label not important but shows what I mean)?
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public string MyData { get; set; }
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
label1.Content = MyData;
}
public partial class LoginWindow : Window
{
public LoginWindow()
{
InitializeComponent();
}
private void button2_Click(object sender, RoutedEventArgs e)
{
string mytext = "blabla";
MainWindow fromloginwindow = new MainWindow();
fromloginwindow.Mydata = mytext;
}
Or am I doing this the wrong way round?
EDIT:
Please do not go on a tangent about the label its unimportant I need to be able to get and set a string for use anywhere in the MainWindow. Also the string "mytext" is also irrelevant as obviously I will not be setting the string this way.
It sounds like you are running into an event lifecycle issue; the calls to the Loaded event happen pretty quickly and thus, the chance to set the text has passed. Instead, what you should do is either:
1) Bind the Property to the Label in the XAML
public event PropertyChangedEventHandler PropertyChanged;
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
protected string _myData = string.Empty;
public string MyData
{
get { return _myData; }
set { _myData = value; NotifyPropertyChanged("MyData"); }
}
protected void NotifyPropertyChanged(string propName)
{
var methods = PropertyChanged;
if(methods != null)
methods(this, new PropertyChangedEventArgs(propName));
}
<Label Content="{Binding MyData}" />
2) Set the control text via another method (or inside the property declaration):
public void SetLabel(string text)
{
label1.Content = text;
}
protected void button2_Click(object sender, RoutedEventArgs e)
{
MainWindow x = new MainWindow();
x.SetLabel("blabla");
}
The Loaded event occurs before you set MyData, change the code like this:
public partial class MainWindow : Window
{
public MainWindow(string data)
{
MyData = data
InitializeComponent();
}
Have you tried passing the value to the second window through the window's constructor?
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public MainWindow(string data)
: this()
{
label1.Content = data;
}
}
public partial class LoginWindow : Window
{
public LoginWindow()
{
InitializeComponent();
}
private void button2_Click(object sender, RoutedEventArgs e)
{
string mytext = "blabla";
MainWindow fromloginwindow = new MainWindow(mytext);
}
}

ObservableCollection CollectionChanged event seems not to be firing – why?

What’s wrong with this code? Clicking button1 doesn’t cause the messageBox to appear.
public partial class Form1 : Form
{
public ObservableCollection<string> aCollection2 = new ObservableCollection<string>();
myClass mc = new myClass();
public Form1()
{
InitializeComponent();
aCollection2.Add("a");
aCollection2.Add("b");
}
private void button1_Click(object sender, EventArgs e)
{
mc.myCollection = aCollection2;
}
private void button2_Click(object sender, EventArgs e)
{
mc.myCollection.Clear();
}
}
With myClass defined:
class myClass
{
public ObservableCollection<string> myCollection = new ObservableCollection<string>();
public myClass()
{
myCollection.CollectionChanged += Changed;
}
void Changed(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
MessageBox.Show(myCollection.Count.ToString());
}
}
EDIT:
When I add a 3rd button with:
private void button3_Click(object sender, EventArgs e)
{
mc.myCollection.Add("a");
}
It does show the messageBox. And so does button2. But after clicking button1 – none will fire anymore. How come?
You added an event handler to the original ObservableCollection instance from your field initializer.
You never added an event handler to the new ObservableCollection instance from the form.
Since the original ObservableCollection never changes, your handler never runs.
This is one of the many reasons why collection properties should be read only (and they should be properties, not fields)

Categories

Resources