【问题标题】:Call other Form from mainForm - Components not visible of other Form从 mainForm 调用其他表单 - 其他表单不可见的组件
【发布时间】:2011-06-22 07:46:18
【问题描述】:

在我的 mainForm 中,我在其 Form_Load 中检查已安装的组件。如果没有安装,我会调用另一个表单(WaitingDialog)来显示安装状态。我将 waitingDialog 称为:

    waitDlg = new WaitingDialog(null); // Parent is set to null
    waitDlg.set("Checking....", "Components"); // Set 2 Labels
    waitDlg.Title = "Installing...";
    waitDlg.Show();

使用上面的代码,等待表单是可见的,但文本集是不可见的。标签的那部分(文字大小)是白色背景,但没有文字可见。

我必须仅在 mainForm 的 Form_Load 中执行此操作,好像组件不存在,然后 mainForm 安装它并在 waitingDlg 表单上显示 appprop 消息。

如何处理这个问题,让文本在 waitingDlg 表单中也可见?

WaitingDialog 代码:

    public partial class WaitingDialog : Form
{
    private string title;
    private string message;
    private bool cancel;
    private ParentForm myParent = null;

    public WaitingDialog()
    {
        InitializeComponent();
        Cancel = false;
        this.StartPosition = FormStartPosition.CenterScreen;
    }

    public WaitingDialog(ParentForm parent) : this()
    {
        if (parent != null)
        {
            myParent = parent;
            this.StartPosition = FormStartPosition.Manual;
        }
    }

    public WaitingDialog(string title, string message)
        : this()
    {
        label1.Text = title;
        msgLbl.Text = message;
       // Title = title;
       // Message = message;      
    }

    private void WaitingDialog_Load(object sender, EventArgs e)
    {
    }


    public string Title
    {
        get { return title; }
        set { title = value;
        label1.Text = title;
        Invalidate();
        }
    }

    public string Message
    {
        get { return message; }
        set { message = value;
        msgLbl.Text = value;
        Invalidate();
        }
    }

    public void set(string title, string message)
    {
        Title = title;
        Message = message;
    }

    public void set(string title, string message, bool showButton)
    {
        Title = title;
        Message = message;
        this.cancelBtn.Visible = showButton;
    }

    public void showCancelButton(bool showButton)
    {
        this.cancelBtn.Visible = showButton;
    }

    public bool Cancel
    {
        get { return cancel; }
        set { cancel = value; }
    }

    private void cancelBtn_Click(object sender, EventArgs e)
    {
        Console.WriteLine("CANCEL BUTTON CAUGHT");
        Cancel = true;
    }


}

// TRIED CAlling as :
waitingDlg = new WaitingDialog("Installing", "Components");
waitingDlg.Text = "Install Components";   //CAN SEE THIS IN TITLE
waitingDlg.Show();

但标签文本不可见,其背景(文本大小)为白色。

希望这会有所帮助。如果您需要更多,很乐意分享更多。

谢谢

【问题讨论】:

  • 你在 waitDlg 中的 set 方法是什么?
  • 您在创建表单时将标签和标题设置为常量值。你能在设计器中设置那些标签和标题吗?
  • 你能从 WaitingDialog 发布 som 代码吗?
  • @George,是的,George,我可以在设计师身上看到。默认值为 Text - “请稍候..”,label1.Text = “Processing” & msgLbl.Text = “label2”。我可以在 Designer 中看到它们。
  • 我现在用两个表单创建了一个 WinForms 应用程序。在第一个表单的 Load 事件中,我按照您的描述调用另一个表单。我在标签和标题栏中都得到了文本。

标签: c# winforms text label visibility


【解决方案1】:

检查 WaitingDialog 的构造函数是否调用;

InitializeComponent();

还要在表单加载中放置一个断点,并确保将要更新的标签的 Text 属性设置为 waitDialog.Title 属性。也可以使用此断点检查 waitDialog.Title 属性值。

【讨论】:

    【解决方案2】:

    当我在显示waitingDlg 时安装组件时。由于 CPU 一直很忙,因此 UI 线程无法绘制标签。我在后台线程的 ReportProgress 中显示和设置标签,并在 DoWork 中调用安装。

    这解决了问题,并按预期很好地显示了一切。

    感谢大家的时间和努力。

    谢谢

    【讨论】:

      猜你喜欢
      • 2012-03-31
      • 1970-01-01
      • 1970-01-01
      • 2017-10-24
      • 2010-12-10
      • 2014-12-21
      • 1970-01-01
      • 2012-02-19
      • 1970-01-01
      相关资源
      最近更新 更多