标签在我的表单上没有更新,而其值更新,C#winform [英] Label not updating on my form whilst its value updates, C# winform
问题描述
我是C#的新手,但现在我必须编写一个软件来报告当前登录Outlook的用户。要做到这一点,我使用以下代码:
I am pretty new with C# but now I have to write a software that reports the user currently logged in Outlook. To do that I'm using the following code:
public string AccountLogged
{
get
{
try
{
return (Marshal.GetActiveObject("Outlook.Application") as Outlook.Application).Session.Accounts[1].DisplayName;
}
catch (Exception)
{
return string.Empty;
}
}
}
然后在Application.Idle事件中,我更新了几个标签(其中一些显示来自outlook的数据和其他来自其他来源的数据)但只有一个没有立即更新表格:
then in the Application.Idle event, I update several labels (some of them shown data from outlook and some other from othe sources) but only one is not updating on the form immediately:
lbUserAccount.Text = ComVar.otlConnector.AccountLogged;
在调试时调查变量所有值都是正确的;甚至打开和关闭Outlook几次数据都正确更新。
问题是表单上的标签只有在我分配值string时才会立即更新。否则它只有在我点击表单时才会更新。
我的尝试:
- >而不是从Application.Idle更新此标签我尝试了System.Windows.Forms.Timer
- >无效
- >刷新
- >更新
Looking into the vars while debugging all the values are correct; even opening and closing Outlook several times data are updated correctly.
The problem is that the label on the form updates immediately only when I assign the value string.Empty otherwise it does only if I click on the form.
What I have tried:
-> Instead of updating this label from Application.Idle I tried System.Windows.Forms.Timer
-> Invalidate
-> Refresh
-> Update
推荐答案
刷新表单上的标签只需使用Control.Invoke方法,如:
to refresh the label on your Form just use Control.Invoke Method like :
this.lbUserAccount.BeginInvoke((MethodInvoker) delegate() {this.lbUserAccount.Text = ComVar.otlConnector.AccountLogged; });
the follwoing解决方案是我的代码的一部分,可以很好地满足我的需求
the follwoing solution is part of my code that working fine for my needs
using Outlook = Microsoft.Office.Interop.Outlook;
.
.
.
public string AccountLogged
{
get
{
return GetDefaultOutlookAccount();
}
}
private string GetDefaultOutlookAccount()
{
Outlook.NameSpace session = null;
Outlook.Application outlookApp = GetOutlookApplicationObject(out session);
if (session == null)
return null;
string account = string.Empty;
try
{
Outlook.AddressEntry addrEntry = session.CurrentUser.AddressEntry;
//Exchange server
if (addrEntry.Type == "EX")
{
Outlook.ExchangeUser manager = session.CurrentUser.AddressEntry.GetExchangeUser();
if (manager != null)
account = manager.PrimarySmtpAddress;
}
else
{
account = session.CurrentUser.Address;
}
}
catch (Exception ex)
{
//do something or
//throw new Exception(ex);
}
return account.ToLower();
}
private Outlook.Application GetOutlookApplicationObject(out Outlook.NameSpace session)
{
Outlook.Application app = null;
session = null;
// Check whether there is an Outlook process running.
if (Process.GetProcessesByName("OUTLOOK").Length > 0)
{
try
{
//If so, use the GetActiveObject method to obtain the process and cast it to an Application object.
app = (Outlook.Application)Marshal.GetActiveObject("Outlook.Application");
session = app.Session;
}
catch (Exception ex)
{
//do something or
//throw new Exception(ex);
}
}
//if Outlook process not running or exception thrown
//create a new instance of Outlook and log on to the default profile.
if (app == null)
{
try
{
app = new Outlook.Application();
session = app.GetNamespace("MAPI");
session.Logon("", "", Missing.Value, Missing.Value);
}
catch (Exception ex)
{
//do something or
//throw new Exception(ex);
}
}
//now resolve current user
if (app != null && app.Session != null && session.CurrentUser != null)
session.CurrentUser.Resolve();
return app;
}
这篇关于标签在我的表单上没有更新,而其值更新,C#winform的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!