wpf 窗体中显示当前系统时间
2014-07-18 12:46
1016 查看
先看一下效果:
这其实是我放置了两个TextBlock,上面显示当前的日期,下面显示时间。
接下来展示一下代码:
在XAML中:
<StackPanel Width="205"
Margin="0,0,57,0"
HorizontalAlignment="Right">
<TextBlock Height="Auto"
Margin="10,5,0,0"
Name="tbDateText"
Foreground="White"
FontWeight="Bold"
FontFamily="Arial"
FontSize="15" />
<TextBlock Height="Auto"
Margin="10,5,0,0"
Name="tbTimeText"
Foreground="#ffa51f"
FontWeight="Bold"
FontFamily="Calibri"
FontSize="23" />
</StackPanel>
在主窗体的cs中代码为:
/// <summary>
/// 定义一个定时器
/// </summary>
private DispatcherTimer ShowTimer;
public MainWindow()
{
InitializeComponent();
ShowTime(); //在这里窗体加载的时候不执行文本框赋值,窗体上不会及时的把时间显示出来,而是等待了片刻才显示了出来
ShowTimer = new System.Windows.Threading.DispatcherTimer();
ShowTimer.Tick += new EventHandler(ShowCurTimer);//起个Timer一直获取当前时间
ShowTimer.Interval = new TimeSpan(0, 0, 0, 1, 0);
ShowTimer.Start();
}
public void ShowCurTimer(object sender, EventArgs e)
{
ShowTime();
}
//ShowTime方法
private void ShowTime()
{
//获得年月日
this.tbDateText.Text = DateTime.Now.ToString("yyyy/MM/dd"); //yyyy/MM/dd
//获得时分秒
this.tbTimeText.Text = DateTime.Now.ToString("HH:mm:ss");
}
这其实是我放置了两个TextBlock,上面显示当前的日期,下面显示时间。
接下来展示一下代码:
在XAML中:
<StackPanel Width="205"
Margin="0,0,57,0"
HorizontalAlignment="Right">
<TextBlock Height="Auto"
Margin="10,5,0,0"
Name="tbDateText"
Foreground="White"
FontWeight="Bold"
FontFamily="Arial"
FontSize="15" />
<TextBlock Height="Auto"
Margin="10,5,0,0"
Name="tbTimeText"
Foreground="#ffa51f"
FontWeight="Bold"
FontFamily="Calibri"
FontSize="23" />
</StackPanel>
在主窗体的cs中代码为:
/// <summary>
/// 定义一个定时器
/// </summary>
private DispatcherTimer ShowTimer;
public MainWindow()
{
InitializeComponent();
ShowTime(); //在这里窗体加载的时候不执行文本框赋值,窗体上不会及时的把时间显示出来,而是等待了片刻才显示了出来
ShowTimer = new System.Windows.Threading.DispatcherTimer();
ShowTimer.Tick += new EventHandler(ShowCurTimer);//起个Timer一直获取当前时间
ShowTimer.Interval = new TimeSpan(0, 0, 0, 1, 0);
ShowTimer.Start();
}
public void ShowCurTimer(object sender, EventArgs e)
{
ShowTime();
}
//ShowTime方法
private void ShowTime()
{
//获得年月日
this.tbDateText.Text = DateTime.Now.ToString("yyyy/MM/dd"); //yyyy/MM/dd
//获得时分秒
this.tbTimeText.Text = DateTime.Now.ToString("HH:mm:ss");
}
相关文章推荐
- WPF中添加个简单的显示当前系统时间的示例
- C#--第11周实验--任务1(建立Windows窗体应用程序)--设计一个窗体--单击按钮时,在标签上显示当前系统时间
- 在该窗体上,放置一个按钮、一个标签。单击按钮时,在标签上显示当前系统时间。
- WPF 显示当前系统时间
- C#--第11周实验--任务1(建立Windows窗体应用程序)--设计一个窗体--单击按钮时,在标签上显示当前系统时间 .
- 设计一个如下图所示窗体:该窗体自动位于屏幕中央;大小不可调;最小化、最大化按钮不可用;窗体标题为“烟台大学”。在该窗体上,放置一个按钮、一个标签。单击按钮时,在标签上显示当前系统时间。
- WPF中添加个简单的显示当前系统时间的示例
- 显示当前系统时间
- MFC状态栏的编程(以显示系统当前时间为例)
- 使用JavaScript显示当前系统时间
- 显示当前时间的函数,当初是为了里面的check函数,我还以为是系统自带的(目前没找到奥这个函数,但是以前学过,不重要就先不看了)
- 在状态栏中显示当前系统时间
- js显示当前系统时间若干代码
- c++实现显示系统当前时间
- PHP日期时间函数date & time:如何显示系统当前时间与时钟同步
- 显示当前系统时间(Linux C++)
- 用JS显示当前系统时间
- c# winForm 将窗体状态栏StatusStrip 分成左中右三部分 右边显示当前时间
- javascript 显示当前系统时间代码
- c# winForm 将窗体状态栏StatusStrip 分成左中右三部分 右边显示当前时间