How to write such code into a function so that you can call it from the button's processing method

int x = Convert.ToInt32(textBox1.Text); string bin = Convert.ToString(x, 2); label3.Text = bin; 

What did i mean

 public static string perevod(int val) { string res = Convert.ToString(val, 2); return res; } private void button1_Click(object sender, EventArgs e) { int val = Convert.ToInt32(textBox1.Text); string res = perevod(val); label3.Text = perevod(val); } 
  • What kind of "Maine" is meant here? - Grundy
  • Yes, not the main event of the button. I will correct - test19
  • figured out already, so thanks for the help - test19
  • @SOFL, if any of the comments / responses were helpful to you - check them off. - Kirill21
  • 2
    somehow there is no need for a fukntion at all. It makes no sense to take one line to the function - Grundy

1 answer 1

If you want the form elements to be available in main (), which is not true. so

 public static String Method(String fromTextBox) { int x = Convert.ToInt32(fromTextBox); string bin = Convert.ToString(x, 2); return bin; } 

Then assign your label. If I understand you correctly

  • In general, the transfer between forms is done using the MVP pattern for WinForms. - Kirill21
  • Did not quite understand why the argument fromTextBox does not return a rettern? - test19
  • @SOFL, how I understood it, proceeding from your code. I present the call in Maine like label3.Text = Method (textBox1.Text); and form elements are accessible in the lanes - Kirill21
  • I just about the way I wanted - test19
  • I will add to the topic, look - test19