1

I would like to use some public methods from another forms. I'd like to know if there is a way to do this, I tried to put the method and the form in public, but when I try to call it into another form the method, it just dosnt appear anything.

here is my main form:

namespace GUI
{
    public partial class frmPrincipal : Form
    {
        public frmPrincipal()
        {
            InitializeComponent();
        }

        private void categoriaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmCadastroCategoria f = new frmCadastroCategoria();
            f.ShowDialog();
            f.Dispose();
        }

        private void categoriaToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            frmConsultaCategoria f = new frmConsultaCategoria();
            f.ShowDialog();
            f.Dispose();
        }

and this is the form that I try to call

    public void LocalizarCategoria()
    {
        frmConsultaCategoria f = new frmConsultaCategoria();
        f.ShowDialog();
        if (f.codigo != 0)
        {
            DALConexao cx = new DALConexao(DadosDaConexao.StringDeConexao);
            BLLCategoria bll = new BLLCategoria(cx);
            ModeloCategoria modelo = bll.CarregaModeloCategoria(f.codigo);
            txtCodigo.Text = modelo.CatCod.ToString();
            txtNome.Text = modelo.CatNome;
            alteraBotoes(3);
        }
        else
        {
            this.LimpaTela();
            this.alteraBotoes(1);
        }
        f.Dispose();
    }

I was trying to do something like:

    {
      LocalizarCategoria()
    }

but I can't, it just dosn't find the method.

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
Andre Aquiles
  • 394
  • 1
  • 17
  • 2
    "I would like to use some public methods from another forms." - Don't do that because it will make your forms dependent despite they may not be. If a method is in another form then it belongs there. What you probably want to do is moving the method to another place (such as a base class). – Quality Catalyst May 12 '16 at 23:10
  • 1
    I agree with @Quality Catalyst's comment, and recommend you accept his answer over mine - the base class idea is a far better solution in this case. – CoolBots May 12 '16 at 23:20
  • It is not entirely clear what it is you're trying to do. But there are two obvious possibilities, and both are well-addressed by previously-asked questions: https://stackoverflow.com/questions/1665533/communicate-between-two-windows-forms-in-c-sharp and https://stackoverflow.com/questions/25394337/how-to-use-the-same-method-with-two-different-forms (if the answer posted by @Quality addresses your concern, then your question is a duplicate of the second of those links). – Peter Duniho May 13 '16 at 00:57

3 Answers3

4

Don't call the method because it will make your forms dependent, when they don't have to be. If a method is in another form, then it belongs there for a reason.

What you probably want to do, is to move the method to another place (such as a base class).

Example:

public partial class frmPrincipal : MyFormBase // inherit from your own base
{
    ...
}

public partial class frmMyOtherForm : MyFormBase // inherit from your own base
{
    ...
}

public class MyFormBase : Form  // your own base with the to be shared method
{
    protected void LocalizarCategoria() // protected might be enough
    {
        ...
    }
}
fluter
  • 13,238
  • 8
  • 62
  • 100
Quality Catalyst
  • 6,531
  • 8
  • 38
  • 62
  • The method the OP has posted uses two fields which are almost certainly specific to the form in which the method is contained, as well as calls two other methods within the same class, either of which could easily also require members of the same form. The OP will not be able to simply move his code into a base class, without moving a lot of other members as well (and frankly, the VS Designer does not handle subclassed `Form` objects very well, so doing so would introduce a host of other headaches). – Peter Duniho May 13 '16 at 01:00
  • Yes, correct, and this explains why it should be taken out of the form into a more appropriate place. Designs like this cause headaches long-term with even heavier re-factoring necessary so better design and implement well in the first place so you can avoid rework later. – Quality Catalyst May 13 '16 at 05:37
  • Well, I would have to use like 20 methods into another 20 forms, I'd say it wouldn't be efficient, so I tried to think outside the box and made another solution based on my needs. But since this looks like the best solution i'll point this one as the best answer. Thanks for your efforts and your shared knowledge. – Andre Aquiles May 14 '16 at 03:26
0

LocalizarCategoria is an instance method (because it's not static). So you need an instance of the other form, and then you can call it like this:

var myOtherForm = new frmOtherForm();
myOtherForm.LocalizarCategoria();

Alternatively, if LocalizarCategoria doesn't need to access any instance fields, properties or methods, then you can make it static:

public static void LocalizarCategoria()
{
    // etc..

And then call it by referencing the other form's class (instead of a specific instance):

frmOtherForm.LocalizarCategoria();
Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
0

You need to create an instance of the Form object you want to use:

var form = new FormThatHasTheMethod_LocalizarCategoria();
form.LocalizarCategoria();
CoolBots
  • 4,770
  • 2
  • 16
  • 30
  • Workable solution, but makes the forms dependent on each other. – Quality Catalyst May 12 '16 at 23:14
  • 1
    I agree, this is not a good practice in general; however, I am presuming OP has a need for this situation... I'd personally look for a better solution, perhaps a static class that both forms can use, or base class (as you have suggested), etc. – CoolBots May 12 '16 at 23:16