Move image to another form by clicking the c# button (Visual Studio)

Asked

Viewed 617 times

0

I’m making a parking system, in which arose the need for when the user register, in the main form appear to an icon of a car on top of his vacancy (which is a label), ie when he click the 'register' buttonif' in the other form, the main form has to appear a car icon on top of its wave!

i tried it as follows, at the click of the sign-up form button I instated 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!

//Click the sign up button, the registration form, first I create the object then I instate 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;

    }

//Constructor of the main form that is called at the click of the sign up button

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

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

  • Why didn’t it work?

  • Also do not know, I believe that maybe because I am instantiating a new object and I actually have to do this in the current object that is created when the form opens finally.. I’m not sure, but I’ve changed my mind, figure out a way to do everything in one form, work with Forms in visual studio is an ass!!!

  • Okay, I still don’t understand what the problem is. " It didn’t work" is very abstract, we need to know what happens, what the expected behavior and what the problem is. About your last sentence, it’s just because you can’t work, it’s all very simple.

  • it was expected that the label that is in the other form(main), received the parameter(photo) passed when clicking the button to register, and that when it receives the parameter that this label assigns this photo to its delimited area, however when I do this, nothing happens, not error, and it doesn’t work either! I don’t know if I’ve been able to clear up my problem, but I hope so!

  • "but when I do, nothing happens" -> By that you mean that the form does not open? Or that it does not show the icon as you want?

3 answers

0

  • Check if the function button1_Click is really being called.

  • Break points in your code to debug it, only then you will find the error. One help is to use the F11 shortcut to debug code line by line from the breakpoint. (break point and test function button1_Click.

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

    this. Click += button1_Click;

0

Your code is missing to call the 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);

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

}
  • Could you be more specific? I should be where?

  • 1

    improved the response

0

Try this. In the 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;

    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.