Method does not work when called

Asked

Viewed 998 times

1

I have two forms:

In the form1 there’s a ListBox with some pre-entered data and a method.
In the form2 there is a button that executes a method in the form1.

//form1
public void limparListBox()
{
    listBox1.Items.Clear();
}

//form2
private void button1_Click(object sender, EventArgs e)
{
    form1 limpar = new form1;
    limpar.limparListBox();
}

For some reason I don’t know, he just doesn’t clean up ListBox.

I want to know why and how I can solve this problem.

  • 1
  • 3

    The problem is that you create a new reference from form1, instead of using the reference for the form1 which is already created. That is, you delete the ListBox of a form1 that is not the form1 that you intend to erase.

  • @Felipeavelar, wut? What would be the way to do without "creating" a new Form1?

  • Actually, it’s not that you didn’t create the form, but you’re probably not passing the reference of form which is on the screen and therefore, looks like not running. Probably, if you give limpar.show(), another will be presented form who has the ListBox clean.

  • That would work with a form with more components than ListBox? For example, besides the ListBox, have a ComboBox and a TextBox, I mean, he’d just erase the ListBox?

  • Dude, it would work with any component that’s on form, provided that they are encapsulated with public, you can access all outside the main class of the form. If you don’t want to, just make a change method (as you did with the clean) and it will perform that method. (provided it is public). Otherwise your problem is exactly the same of that question, as I wrote in my first comment.

  • @Felipeavelar did not understand very well your settings... But I did another test, I put a message in the method limparListBox(), when I press the method through the button while the ListBox is without any item, message appears, when not, when there are items in the ListBox, the message does not appear.

Show 2 more comments

2 answers

1

This from here is incorrect, including in the builder of form1 which should contain the call to the ():

//form2
private void button1_Click(object sender, EventArgs e)
{
    form1 limpar = new form1(); // em vez de form1;
    limpar.limparListBox();
}

What you do here is call the method limparListBox() of an object instance form1 that has just been created and is probably not that instance that is already being displayed on the screen and should have been created at another time.

Another detail refers to the lack of pattern for names of classes and members when using Camelcase (or lowerCamelCase) which is the standard used in Java but not in C#, which uses Pascalcase (or Uppercamelcase). I will assume that the types were created with Visual Studio and the case pattern in the class names was maintained.

So, assuming we have two presses (Form1 and Form2) and that the Form2 is opened with a button on Form1. The code would look like this:

//Form1
private void button1_Click(object sender, EventArgs e)
{
    var form2 = new Form2();
    form2.Form1 = this; //aqui, vamos criar uma propriedade no Form2 que recebe a referência do Form1: this
    form2.Show();
}

public void LimparListBox() //aqui o método que será chamado no Form2 setado como public
{
    //
}

And the code of Form2:

//Form2
public partial class Form2 : Form
{
    public Form1 Form1 { get; set; } //essa é a propriedade que recebe a referência this do Form1

    public Form2()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        // form1 limpar = new form1(); 
        // em vez de criar uma nova instância ... 
        Form1.LimparListBox(); //... usamos aquela passada por referencia
    }
}
  • I used your tips... I did another test, I put a message in the method limparListBox(), when I press the method through the button while the ListBox is without any item, message appears, when not, when there are items in the ListBox, the message does not appear.

0

I have no experience with C#, but from what I understand the following should work:

Supposing they are in namespaces separately, add the namespace of Form1 no using form2:

using Form1;

If you happen to be in the same namespace, the above step is not necessary.

And call the public variable form1 thus:

private void button1_Click(object sender, EventArgs e)
{
    form1.limpar.limparListBox();
}

Browser other questions tagged

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