Use Relative Form in Usercontrol

Asked

Viewed 55 times

0

I’m trying to create a Usercontrol so you can call different Forms, however changing only the controls he make the call.

Shows the error that the "Form" is non-existent.

  public partial class campos_busca : UserControl
    {
        public Form Formulario
        {
            get; set;
        }
        public campos_busca()
        {
            InitializeComponent();
        }

        private void Campos_busca_Load(object sender, EventArgs e)
        {

        }

        private void TextBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Formulario f = new Formulario();
        }
  • The "Form" field is variable, e.g.: Define "Form Form = Frm_products" When the Doubleclick event triggers it would be like: Frm_products f = new Frm_products(); f. Showdialog(); When to set "Form Formulario = Frm_employee": Frm_employee f = new Frm_employee(); f. Showdialog();

2 answers

1

It’s a little complex to answer the question without understanding the whole context. But what I suggest is that the creation part of the form is on behalf of those who use the UserControl.

Something like:

var meuCampo = new campos_busca(); //--- Você pode omitir isso aqui
meuCampo.Formulario = new FrmFuncionarios(); //---- Essa parte você coloca na criação do formulário que contém o seu UserControl, por exemplo.

Within your UserControl you can work with Form normally. But remember to check if it has already been created.

In my case I work with form inheritance and implement the methods of creating these forms as virtual methods that should always be implemented in inheritance. Like I told you, the context could be different.

I hope I’ve helped

1


You are trying to instantiate a class that does not exist, so the compiler will indicate that it does not exist (obviously). When you try to invoke the new Formulario, he will search for the class Formulario, which for the time being does not exist.

If you’re trying to instantiate the Form who is in Formulario, assign an instance to the property value. (Example of this at the end)


If you want your UserForm call another Form, remove the new when attempting to instantiate an instantiated object.

 public partial class campos_busca : UserControl
    {
        public Form Formulario
        {
            get; set;
        }
        public campos_busca()
        {
            InitializeComponent();
        }

        private void Campos_busca_Load(object sender, EventArgs e)
        {

        }

        private void TextBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            // Formulario f = new Formulario();  < ~~ você está tentando instanciar a propriedade Formulario
            Formulario.ShowDialog();  // < ~~ está a chamar o ShowDialog de sua propriedade Formulario
        }
    }

And to specify that you will call a particular form, associate it in your property:

campos_busca cbusca = new campos_busca();
cbusca.Formulario = new Frm_produtos();

When calling the event MouseDoubleClick within the cbusca, will call the ShowDialog of Frm_produtos instantiated.

Browser other questions tagged

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