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
}
}
possible duplicate of How to make child form change values in parent form C#?
– Felipe Avelar
The problem is that you create a new reference from
form1
, instead of using the reference for theform1
which is already created. That is, you delete theListBox
of aform1
that is not theform1
that you intend to erase.– Felipe Avelar
@Felipeavelar, wut? What would be the way to do without "creating" a new Form1?
– ptkato
Actually, it’s not that you didn’t create the
form
, but you’re probably not passing the reference ofform
which is on the screen and therefore, looks like not running. Probably, if you givelimpar.show()
, another will be presentedform
who has theListBox
clean.– Felipe Avelar
That would work with a
form
with more components thanListBox
? For example, besides theListBox
, have aComboBox
and aTextBox
, I mean, he’d just erase theListBox
?– ptkato
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 theform
. 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 ispublic
). Otherwise your problem is exactly the same of that question, as I wrote in my first comment.– Felipe Avelar
@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 theListBox
is without any item, message appears, when not, when there are items in theListBox
, the message does not appear.– ptkato