Reputation: 135
I'm learning Windows Forms and I'm stuck when working with Windows Forms and Classes. I want to make a form in which a user can enter the temperature in Farenheit then click a "convert" button and this will take the input value to a "convert" class where it gets converted to Celsius then have both Farenheit and Celsius displayed on a message box.
I have designed the form and I know how to create the class. What I don't know is how to take the input value (farenheit) from the form to a class then calling the converted value from the form and display it in a message box.
I'm a beginner in C# so I'd appreciate your comprehension and beginner-like answers. Thank you!
Upvotes: 3
Views: 45518
Reputation: 176896
You need to create method
public class Convertor
{
public datatype FarenheitToCelsius(String value)
{
datatype celsius;
...conversion logic
return celsius;
}
}
Then you need to call method of class
public class form1
{
public void button_click(arguments...)
{
Convertor c = new Convertor();
MessageBox.Show(c.FarenheitToCelsius(textbox1.text));
}
}
Note: this is just a partial example
Upvotes: 8
Reputation: 11844
You need to declare two public properties in the class one is setFarenheit
and another one is getConvertedheit
, now you can call the class and can create properties as follows.The following two properties are inside the class
public string setFarenheit { set; }
public string getConvertedheit { get; set; }
and assign the converted heit value to the getConvertedheit property in your class.
getConvertedheit = heitConvertedintoCelcius;//your converted celcius heit temp inside the class.
And in the Form class you can call it like follows if they exist in same namespace.
HeitConvertingClass hcc = new HeitConvertingClass();
hcc.setFarenheit=Userinput(the datatype is your choice may be int or float);
MessageBox.Show(hcc.getCovertedheit.ToString());
Upvotes: 1
Reputation: 37819
Well, there's two ways that this can go: databinding or accessing the values via the TEXT property of the text box.
The second is the easier to show in this scenario, so let's go with that.
You'd have this code in your CONVERT button's Click Event handler (and this is assuming that your conversion class has a CONVERT method that takes in a the farenheit temp as a string and then returns the string you want to display).
convert c = new convert();
myConversionString as string = c.ConvertForDisplay(MyTextBoxName.Text);
MessageBox.Show(myConversionString);
That's the BASIC way to get that value into your class, and the end result back into the UI.
Upvotes: 1