Pass image to another form via the click of the c # button (Visual Studio)

0

I'm doing a parking system, in which the need arises when the user registers, in the main form appears an icon of a car over his seat (which is a label), that is, when he clicks on the 'sign up' button in the other form, the main form has to appear a car icon on top of the vacancy of it!

I tried the following way, in the click of the button of the registration form I instantiated a constructor of the main form, and in the constructor of the main form, I passed the image that should go to the label, however it did not work! p>

// Click the register button, the form register, first I create the object after instantiating the form1

private void button1_Click(object sender, EventArgs e)
    {
        Cadastro cadObj = new Cadastro(Convert.ToString(horas), txt_placa.Text, txt_cor.Text, txt_modelo.Text);

        var formulario1 = new Form1(Properties.Resources.vermelho);


        btn_registrar.Enabled = false;

    }

// The main form constructor that is called in the click of the button to register

 public Form1(System.Drawing.Image imagem)
    {
        InitializeComponent();
        box15.Image = imagem;
    }

I just need that when I click on register the icon of a car go to the label, to indicate that there is someone already there in the vacancy!

    
asked by anonymous 06.12.2016 / 00:08

3 answers

0

Try this. No form

private void button1_Click(object sender, EventArgs e)
{
        Cadastro cadObj = new Cadastro(Convert.ToString(horas), txt_placa.Text, txt_cor.Text, txt_modelo.Text);
        cadObj.showDialog();

        //caso a imagem esteja vindo do form cadastro
        this.box15.Image = cadObj.imagem;

        //ou
        this.box15.Image = Properties.Resources.vermelho;

        btn_registrar.Enabled = false;

    }
    
06.12.2016 / 14:30
0

Your code is missing the form call:

private void button1_Click(object sender, EventArgs e)
{
    Cadastro cadObj = new Cadastro(Convert.ToString(horas), txt_placa.Text, txt_cor.Text, txt_modelo.Text);

    var formulario1 = new Form1(Properties.Resources.vermelho);
    //chama o form com a imagem
    formulario1.ShowDialog();
    btn_registrar.Enabled = false;

}
    
06.12.2016 / 14:16
0
  • Verify that the button1_Click function is actually being called.

  • Make breakpoints in your code to debug it, only then will you find the error. One help is to use the shortcut F11 to debug the code line by line, from the breakpoint. (make a breakpoint and test the button1_Click function.

  • Because your button1_Click function is also a Listener (in this case, for the click event), make sure the listener was actually added in the Click event in the designer file of the form to which it belongs. Something like:

    this.Click + = button1_Click;

07.12.2016 / 13:01