none
Program is not responding RRS feed

  • Question

  • Hello,

    I am currently learning how to make Windows Forms application. I am reading a book "Windows Forms programming with C#" by Erik Brown and there is this code:

    [assembly: System.Reflection.AssemblyVersion("1.3")]
    namespace MyNamespace
    {
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    public class MyForm : System.Windows.Forms.Form
    {
    Button btnLoad;
    PictureBox pboxPhoto;
    public MyForm()
    {
    this.Text = "Hello Form 1.3";
    // Create and configure the Button
    btnLoad = new Button();
    btnLoad.Text = "&Load";
    btnLoad.Left = 10;
    btnLoad.Top = 10;
    btnLoad.Click += new System.EventHandler(this.OnLoadClick);
    // Create and configure the PictureBox
    pboxPhoto = new PictureBox();
    pboxPhoto.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
    pboxPhoto.Width = this.Width / 3;
    pboxPhoto.Height = this.Height / 3;
    pboxPhoto.Left = (this.Width - pboxPhoto.Width) / 2;
    pboxPhoto.Top = (this.Height - pboxPhoto.Height) / 2;
    pboxPhoto.SizeMode = PictureBoxSizeMode.StretchImage;
    // Add our new controls to the Form
    this.Controls.Add(btnLoad);
    this.Controls.Add(pboxPhoto);
    }
    private void OnLoadClick(object sender, System.EventArgs e)
    {
    OpenFileDialog dlg = new OpenFileDialog();
    dlg.Title = "Open Photo";
    dlg.Filter = "jpg files (*.jpg)|*.jpg|All files (*.*)|*.*" ;
    if (dlg.ShowDialog() == DialogResult.OK)
    {
    pboxPhoto.Image = new Bitmap(dlg.OpenFile());
    }
    dlg.Dispose();
    }
    public static void Main()
    {
    Application.Run(new MyForm());
    }
    }
    }

    I compiled it, run the program and after clicking "load" button the app is not responding. Everything was ok when the code looked only this way:

    [assembly: System.Reflection.AssemblyVersion("1.2")]
    namespace MyNamespace
    {
    using System;
    using System.Windows.Forms;
    public class MyForm : Form
    {
    private Button btnLoad;
    private PictureBox pboxPhoto;
    public MyForm()
    {
    this.Text = "Hello Form 1.2";
    // Create and configure the Button
    btnLoad = new Button();
    btnLoad.Text = "&Load";
    btnLoad.Left = 10;
    btnLoad.Top = 10;
    // Create and configure the PictureBox
    pboxPhoto = new PictureBox();
    pboxPhoto.BorderStyle =
    System.Windows.Forms.BorderStyle.Fixed3D;
    pboxPhoto.Width = this.Width / 2;
    pboxPhoto.Height = this.Height / 2;
    pboxPhoto.Left = (this.Width - pboxPhoto.Width) / 2;
    pboxPhoto.Top = (this.Height - pboxPhoto.Height) / 2;
    // Add our new controls to the Form
    this.Controls.Add(btnLoad);
    this.Controls.Add(pboxPhoto);
    }
    public static void Main()
    {
    Application.Run(new MyForm());
    }
    }
    }

    But after adding an event I cannot click load button because, as I said before, app is not responding. Where is the problem?

    Thursday, February 20, 2020 9:01 PM

Answers

All replies