0

I have a code in WindowAfterLogin.xaml:

<TextBlock x:Name="In_Time" Foreground="#FF55534F" FontSize="71.312" FontFamily="HelveticaNeueCyr" Height="79.45" LineStackingStrategy="BlockLineHeight" Canvas.Left="2.724" LineHeight="71.312" TextAlignment="Center" TextWrapping="Wrap" Canvas.Top="69.985" Width="231.581" Text="09:00" /> 

And then I want to change the value of that TextBlock in WindowAfterLogin.xaml.cs, so I've done this :

MainWindow objMainWindow = new MainWindow(); WindowAfterLogin objAfterLogin = new WindowAfterLogin(); objMainWindow.Show(); objAfterLogin.In_Time.Text = DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString(); this.Close(); 

But, when I press F5 button (Compile), it didn't change. Where is the problem here?

8
  • try this.In_time.Text instead Commented Mar 14, 2014 at 3:06
  • where should i put that code? because it was error everywhere.. Commented Mar 14, 2014 at 3:20
  • @GrantWinney it came from this code: WindowAfterLogin objAfterLogin = new WindowAfterLogin(); Commented Mar 14, 2014 at 3:21
  • aahh, I understand @safetyOtter. But still, it didn't change. Commented Mar 14, 2014 at 3:26
  • On what event are you changing the text? Please show us the code for that event. Commented Mar 14, 2014 at 3:29

2 Answers 2

1

Are you creating a separate instance of WindowAfterLogin ? If you want to do that, you will have to Show() the new instance. Try this:

 WindowAfterLogin objAfterLogin = new WindowAfterLogin(); objAfterLoginShow(); objAfterLogin.In_Time.Text = DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString(); //Close(); //Close any other window that needs to be closed.. 

If you wanted the In_Time.Text to change for the current instance, (assuming that has been showed or is visible currrently), you can try this perhaps in the constructor or your method that Initializes the WindowAfterLogin :

In_Time.Text = DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString(); 
Sign up to request clarification or add additional context in comments.

Comments

0

As a simplification to bit answer, try to write something like this instead of your code

MainWindow objMainWindow = new MainWindow(); objMainWindow.Show(); In_Time.Text = DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString(); this.Close(); //Not sure if You need it. Try to comment this line to be able to notice textblock text changes 

Also I think You need

DateTime.Now.ToString("hh:mm") 

instead of

DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString() 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.