Form.Shown调用不显示PictureBox图像

时间:2021-10-19 00:24:28

I have a form containing a TableLayoutPanel which has 1 row and 2 columns.

我有一个包含TableLayoutPanel的表单,其中包含1行和2列。

Column 1 contains a panel which contains a picture box. Column 2 is a text box.

第1列包含一个包含图片框的面板。第2列是一个文本框。

I would like to display the form and then add text to the textbox one character at a time. Everything works perfectly except the picturebox image isn't displayed until the textbox has finished populating.

我想显示表单,然后一次将一个字符添加到文本框中。一切都很完美,除了在文本框完成填充之前不显示图片框图像。

class Program
{
    static void Main(string[] args)
    {
        MainForm mainForm = new MainForm();

        FormShown Shown = new FormShown(mainForm);

        mainForm.Shown += new EventHandler(Shown.mainForm_Shown);

        mainForm.ShowDialog();
    }
}

class FormShown
{
    MainForm mainForm;

    public FormShown(MainForm aMainForm)
    {
        mainForm = aMainForm;
    }

    public void f1_Shown(object sender, EventArgs e)
    {
        mainForm.AddText("hello!");
    }
}

public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();

        PictureBox.Image = MyApp.Properties.Resources.MyImage;
    }

    public void AddText(string text)
    {
        foreach (char c in text)
        {
            TextBox.Text += c;
            TextBox.Refresh();
            System.Threading.Thread.Sleep(100);
            TextBox.SelectionStart = TextBox.Text.Length;
        }
    }
}   

I'd hoped that setting the image in code from resources would be quick enough, and I expected the picturebox to be loaded by the Form.Shown event - am I wrong to think this?

我希望用资源在代码中设置图像会很快,我希望通过Form.Shown事件加载图片框 - 我想错了吗?

I've tried setting the image in the design view as opposed to in code, but with the same results.

我尝试在设计视图中设置图像而不是代码,但结果相同。

Is there a different event I should be using? I believe Shown is the last to be called.

我应该使用不同的事件吗?我相信Shown是最后一个被称为。

Thanks!

谢谢!

1 个解决方案

#1


1  

If you know the Windows Form Event life cycle then you can easily understand what problem exactly you are getting. Basically image and other controls will be drawn when form is being painted and the Paint event will be raised at the end. So, that means application will not draw any control or graphic until all process will not done.

如果您了解Windows窗体事件生命周期,那么您可以轻松了解您正在获得的问题。基本上,在绘制表单时将绘制图像和其他控件,并在结束时引发Paint事件。因此,这意味着在所有进程都没有完成之前,应用程序不会绘制任何控件或图形。

You should call the AddText() method in different process.

您应该在不同的进程中调用AddText()方法。

#1


1  

If you know the Windows Form Event life cycle then you can easily understand what problem exactly you are getting. Basically image and other controls will be drawn when form is being painted and the Paint event will be raised at the end. So, that means application will not draw any control or graphic until all process will not done.

如果您了解Windows窗体事件生命周期,那么您可以轻松了解您正在获得的问题。基本上,在绘制表单时将绘制图像和其他控件,并在结束时引发Paint事件。因此,这意味着在所有进程都没有完成之前,应用程序不会绘制任何控件或图形。

You should call the AddText() method in different process.

您应该在不同的进程中调用AddText()方法。