【问题标题】:Labels doesnt work on Visual Studio 2010标签在 Visual Studio 2010 上不起作用
【发布时间】:2011-11-09 17:51:36
【问题描述】:

我正在使用 Visual Studio 2010 Ultimate,我尝试使用进度条和两个标签转换一些图像,但两个标签的文本都没有显示,程序只显示白色背景的标签。

附言标签背景颜色为“Control”(不透明)

我有两种形式,一种是“主”形式,一种是“ProgressDialog”形式

当我点击“保存”按钮时在主窗体中

//copy the filenames to a new array to sort them
string[] FileNames = openDialog.FileNames;
Array.Sort(FileNames);                

//create an instance of my progress dialog
ProgressDialog progress = new ProgressDialog(this, 100/FileNames.Length);
progress.ShowProgress();

//save all the files in the array
for (int i = 0; i < FileNames.Length; i++)
{    
    //call the SaveImage method 
    SaveImage(FileNames[i], 597, 412, Fill);

    //increase the progresbar and set the label text of the progress dialog to the filename
    progress.Increase(FileNames[i]);
}

progress.Close();//close progress dialog and re-enable the main form

...这里是我的“ProgressDialog”表单的代码:

Bar:进度条的名称

注释:图片路径为文本的标签名称

private Form SenderForm;// the "Main" form
private int Step; //value to increase in the progress bar

public ProgressDialog(Form sender, int step)//constructor
{
    InitializeComponent();

    SenderForm = sender;
    Step = step;
}

//disable cose button
private const int CP_NOCLOSE_BUTTON = 0x200;
protected override CreateParams CreateParams
{
    get
    {
        CreateParams myCp = base.CreateParams;
        myCp.ClassStyle = myCp.ClassStyle | CP_NOCLOSE_BUTTON;
        return myCp;
    }
}

//re-enable the "Main" form
private void ProgressDialog_FormClosed(object sender, FormClosedEventArgs e)
{
    SenderForm.Enabled = true;
    SenderForm.Activate();//activate form
} 

//increase the value of the progressbar
public void Increase( string comment=null, int step=0)
{
    //if step is 0 -> use the Step of the constructor
    if (step==0) step = Step;

    //if the comment (the current file name) is != of null
    if (comment!=null)
        Comment.Text = comment;//use the comment of the parameter

    //increase progressbar
    Bar.Value += step;            
}

//show "ProgrssDialog" form
public void ShowProgress()
{
    this.Show();
    SenderForm.Enabled = false;//disable the main form
}

【问题讨论】:

  • 我可以自信地说,标签在 Visual Studio 2010 中工作得很好。因此,问题一定出在您的程序上。代码是什么样的?

标签: c# winforms label


【解决方案1】:

这是因为主线程太忙而无法更新 ui 中的标签 您应该在线程中执行该过程以防止 UI 挂起 看看这个帖子C# question on preventing GUI from becoming sluggish when using backgroundworker/thread

【讨论】:

  • 你是对的,但我的经验告诉我当我们要向用户显示后台作业的进度时它的常见问题
  • 是的。唯一的谜团是 ProgressBar 确实 更新的原因。因为它是为这种情况而设计的,所以当您更改 Value 属性时,它会自动调用 Update()。
【解决方案2】:

我怀疑字体颜色也是白色的。尝试更改标签的颜色或背景颜色以查看是否显示文本。

【讨论】:

  • 你能在你设置标签文本的地方放一个断点来确保你确实在这样做吗?另一个想法 - 我曾经遇到过一个案例,其中两个应该不同的系统颜色实际上不是。尝试明确地将颜色更改为您知道的颜色
猜你喜欢
  • 1970-01-01
  • 2011-11-06
  • 2011-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-28
  • 1970-01-01
  • 2011-04-08
相关资源
最近更新 更多