The cataloger wrote the program, wrote everything directly in the body of the event handlers. But the teacher cursed, said that everything should be beautiful, and said to remake using classes. He wants handlers to access classes. The delete button does not work, although the program is running. Please tell me what I'm doing wrong. Here is the code: The class itself

public static class Delete { public static void del() { ListBox listBox1 = new ListBox(); TextBox textBox1 = new TextBox(); listBox1.Items.Remove(listBox1.SelectedItem); textBox1.Text = ""; textBox1.Clear(); } } 

And here is the place where he goes to appeal

 public void deleteButton_Click(object sender, EventArgs e) { // удаляет выбранный элемент из списка Delete.del(); } 
  • Most likely you misunderstood the words of your teacher. Can you show the full code? - user227049
  • What the fuck is in the del method? New ListBox and TextBox are created and something in them is cleared. - Anton Shchyrov
  • @FoggyFinder uploaded the whole project to the cloud, here's the link cloud.mail.ru/public/87MA/BXeQPa4UV - Pavel Dorosh
  • @AntonShchyrov I just started to understand programming ... Can you explain in more detail what is wrong? How to do it, and why - Pavel Dorosh
  • @AntonShchyrov without lines ListBox listBox1 = new ListBox (); TextBox textBox1 = new TextBox (); compiler swears. Writes that the elements in the remaining rows do not exist in this context - Pavel Dorosh

2 answers 2

Your class knows nothing about form elements and therefore it does not work!

For some reason you create new objects inside your class that are not connected with what you see on the form.

For everything to work, you have to make a method that accepts ListBox and TextBox objects and then the class will work with real objects on the form.

UPD

 public static class Delete { public static void del(ListBox listBox1,TextBox textBox1)//добавляешь аргументы в метод { listBox1.Items.Remove(listBox1.SelectedItem); textBox1.Text = ""; textBox1.Clear(); } } 

And pass the elements:

 public void deleteButton_Click(object sender, EventArgs e) { // удаляет выбранный элемент из списка Delete.del(listBox1,textBox1); } 
  • can you please in more detail. Just without these first two lines, the compiler swears. Writes that the elements do not exist in this context, or something like this ... - Pavel Dorosh
  • @PavelDorosh, he swears right, because, as I said, your class does not know anything about them. Pass them through the argument. - iluxa1810
  • Thank you very much, you really helped !! - Pavel Dorosh
 public void deleteButton_Click(object sender, EventArgs e) { if (listBox1.SelectedItem != -1) listBox1.Items.Remove(listBox1.SelectedItem); textBox1.Clear(); } 
  • I understand that the first line in the body of the handler is to check whether the element is selected. I know what goes further. Is it possible to do this through an appeal to the class? So that the handler body was written in another class, and in the event handler there was only an appeal to that class. - Pavel Dorosh
  • @PavelDorosh What will work with another class give you? You need to work with specific objects about which the other class knows nothing. Well, you can create a class, give it two form objects and let it call these three lines. But this is nonsense - Anton Shchyrov
  • Yes, I understand that this is crazy, but our topic is working with classes. If you look at the code that I dropped above, you will see that the rest of the buttons with classes do not work (I originally brought such a project). But I was told to do with the classes. I created a class for the Delete button, passed everything I needed, the program starts, but the button does not work. So I ask what's wrong with my code? - Pavel Dorosh