Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6810929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:15:55+00:00 2026-05-26T20:15:55+00:00

I’m a beginner learning Windows Forms Applications in C# and I want to write

  • 0

I’m a beginner learning Windows Forms Applications in C# and I want to write a program that will prompt the user to enter the name of a place and to enter 2 digit temperature (F) between ranges 20 and 100. When the user presses the “OK” button on the form I want to have a messagebox displaying the name of the place they entered, and the temperature (F) they entered plus its Celsius conversion and an error message in case any of the fields is left blank or if the temp falls beyond the range.

I would like to have a class to handle the conversion and the logic.

I’m still learning guys and I’ve spent all day on this already only getting a few pieces of code working but I’m afraid I’m not understanding what I’m doing. I’m ok with the form design, what I have an issue with, is the c# code and using classes. I’d really appreciate if someone could show me code or good hints for what I want to do.

For your sake I’m not including my code because it may confuse you then we’ll spend hours talking about “why did I do something” but if you really want to see it let me know.

Being a beginner I’d much appreciate beginner-like answers.
Thank you guys !

// Form1.cs

using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace airports
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void btn_quit_Click(object sender, EventArgs e)
        {
            base.Close();
            return;
        }

        public void btn_record_Click(object sender, EventArgs e)
        {
            string place;
            place = Console.ReadLine();
            Calculations name = new Calculations();
            //double temp = Convert.ToDouble(txtTemperature.Text);
            //Calculation ctemp = new Calculation();


        MessageBox.Show(name.GetPlaceName("Calculations: " + txtPlaceName.Text));
        //MessageBox.Show(string.Format("Temperature: {0}", (ctemp.GetTemp(celsiusTemp.text))));


}


//Class


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Calculations
{
    class Calculations
    {


        string placeName;
        double celsiusTemp;

        public string GetPlaceName(string place)        
{

                return placeName = place;            
        }

    public double GetTemp(double temp)
        {
            return celsiusTemp = (temp + 10); 
        }

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:15:56+00:00Added an answer on May 26, 2026 at 8:15 pm

    What you need to do is to take a step back and just ignore the whole WinForms problem. Try to solve the specific problem first:

    I want to write a program that will prompt the user to enter the name of a place and to enter 2 digit temperature (F) between ranges 20 and 100. When the user presses the “OK” button on the form I want to have a messagebox displaying the name of the place they entered, and the temperature (F) they entered plus its Celsius conversion and an error message in case any of the fields is left blank or if the temp falls beyond the range.

    What could be breaken down into two different problems:

    1. Enter to temperature with value between 20 and 99 (since you specified two digits)
    2. Convert the temperature into Celsius

    The validation is something that always should be done, so I don’t consider it as a separate problem.

    The next thing to do is to think about how you want the solution to work. Always start with the solution, since it’s often easier than trying to create the implementation directly. Test Driven Development is really great since it does just that.

    I would want something like this (pseudo code):

    var fahrenheit = dialog.GetInput();
    var celcius = fahrenheit.Convert(new FahrenheitToCelciusConverter());
    resultDialog.Show(fahrenheit, celsius);
    

    Defining objects

    Now since we know what we want to do, lets focus more on the actual implementation. By looking at the pseudo-code we’ve identified that we need a couple of classes.

    First of all, we need to get the input

    // the validation should be done in this form when the user 
    // presses OK. But that's up to you to figure out
    public class PlaceAndTemperatureForm : Form
    {
        public PlaceAndTemperature GetPlaceAndTemperature()
        {
            return new PlaceAndTemperature(txtName.Text, int.Parse(txtTemperature .Text));
        }
    }
    

    And since we need to get the data from the form, lets create a class to contain it:

    public class PlaceAndTemperature 
    {
         public PlaceAndTemperature(string name, int temperature)
         {
             Name = name;
             Temperature = temperature;
         }
         public string Name { get; private set; } 
         public int Temperature { get; private set; } 
    
    }
    

    Look at the class above. It has private setters which means that no other class or code can modify it. It’s called immutable objects. The good thing is that we know that it’s always safe to use and cannot be in an unexpected/invalid state. You should always try to have immutable objects, or at least not let any other objects change the state without having the class itself validate the changes.

    Next up is the conversion. We wrote the following code:

    var celcius = placeAndTempature.Convert(new FahrenheitToCelciusConverter());
    

    Hence we need to modify the class to take a converter and return a new place.

    public class PlaceAndTemperature 
    {
         public PlaceAndTempature(string name, int temperature)
         {
             Name = name;
             Temperature = temperature;
         }
         public string Name { get; private set; } 
         public int Temperature { get; private set; } 
    
         public PlaceAndTemperature Convert(ITemperatureConverter converter)
         {
             return new PlaceAndTemperature(Name, converter.Convert(Temperature));
         }
    }
    

    I’ve just introduced a new concept called Interfaces. You can look at them as word templates or a blue print. They are there to define how a class should look like, but not to have any logic. It’s a specification.

    public interface ITemperatureConverter 
    {
        int Convert(int source);
    }
    

    The problem now is that we have two PlaceAndTemperature objects, the original one with fahrenheit and one with celcius. We need to be able to identify which kind of unit is used, or we’ll probably end up with some nasty bugs later on.

    Lets add a unit specification using an enum.

    public enum TempatureUnit
    {
        Celcius,
        Fahrenheit
    }
    

    And modify the previous classes and interfaces:

    public interface ITemperatureConverter 
    {
        int Convert(int source);
        TempatureUnit SourceUnit {get;}
        TempatureUnit TargetUnit {get;}
    }
    
    public class PlaceAndTemperature 
    {
         public PlaceAndTempature(string name, int temperature, TempatureUnit unit)
         {
             Name = name;
             Temperature = temperature;
             TempatureType = unit;
         }
         public string Name { get; private set; } 
         public int Temperature { get; private set; } 
         public TempatureUnit TemperatureType {get; private set; }
         public PlaceAndTemperature Convert(ITemperatureConverter converter)
         {
             return new PlaceAndTemperature(Name, converter.Convert(Temperature), converter.TargetUnit);
         }
    }
    

    Putting it all together

    Great. Not much left now. Let’s revisit the original code and update it:

    public void Button1_Click(object source, EventArgs e)
    {
        var form = new PlaceAndTemptureForm();
        var result = form.ShowDialog(this);
        if (result != DialogResult.Ok)
            return; // failed for some reason
    
        var fahrenheit = form.GetPlaceAndTemperature();
    
        var celcius = fahrenheit .Convert(new FahrenheitToCelciusConverter());
    
        var form = new ResultForm();
        form.SetTemperatures(fahrenheit, celsius);
        resultDialog.ShowDialog(this);
    }
    

    Summary

    There is still some things that you need to fix yourself. Like creating the FahrenheitToCelciusConverter. Start with something like:

    public class FahrenheitToCelciusConverter : ITemperatureConverter
    {
        public int Convert(int source)
        {
             // put your code here
        }
        public TempatureUnit SourceUnit {get { return TemperatureUnit.Fahrenheit; }}
        public TempatureUnit TargetUnit {get { return TemperatureUnit.Celcius; }}
    }
    

    And you’ll also need to create the result form, or simply update the main form.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
I need to clean up various Word 'smart' characters in user input, including but
i want to parse a xhtml file and display in UITableView. what is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.