예제 #1
0
 private void GetWeather()
 {
     if (!string.IsNullOrEmpty(TxtCity.Text) && !string.IsNullOrEmpty(CountryBox.SelectedItem.ToString())) // Check if user provided information
     {
         string             country     = CountryBox.SelectedItem.ToString();
         string             city        = TxtCity.Text;
         WeatherInformation information = new WeatherInformation(city, country);
         information.GetInformation();
         TxtTemperature.Text    = "Temperature: " + information.getTemp();
         TxtWind.Text           = "Wind: " + information.getWindSpeed();
         TxtHumidity.Text       = "Humidity: " + information.getHumidity();
         TxtStatus.Text         = "Status: " + information.getStatus();
         ImageBox.ImageLocation = information.getImage();
         txtArea.Text           = information.getArea();
     }
     else
     {
         MessageBox.Show("Please fill all required fields!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }
예제 #2
0
        /* This function is called by the eventhandler that is invoked
         * once a user searches for a city. The function checks whether
         * the user input is valid or invalid and sets up the GUI accordingly.
         */
        private void GenerateDefaultInformation()
        {
            // Reg expressions to check whether input is allowed or not
            string checkInteger          = @"\d+";
            string checkSpecialCharacter = @"[@#$%&*+\-_(),+':;?.,![\]\s\\/]+$";

            // Remove new lines
            string input = userInput.Text;

            input          = Regex.Replace(input, @"\t|\n|\r", "");
            userInput.Text = input;

            bool invalidSearch = false;

            // If input contains invalid characters, let the user know and retry
            if (userInput.Text == "" || Regex.Match(userInput.Text, checkInteger).Success ||
                Regex.Match(userInput.Text, checkSpecialCharacter).Success)
            {
                // Take care of cases if input is invalid
                invalidSearch = true;
                // Ensures old values will not appear
                weatherInformation = null;
            }
            else
            {
                // Get weather information
                weatherInformation = requestHandler.FetchDataFromInput(userInput.Text.ToString());

                if (weatherInformation == null)
                {
                    invalidSearch = true;
                }
                else
                {
                    if (fahrenCBox.Checked)
                    {
                        temperatureDefaultView.Text = ConvertTemperature(weatherInformation.main.temp, false);
                    }
                    else
                    {
                        temperatureDefaultView.Text = ConvertTemperature(weatherInformation.main.temp, true);
                    }

                    // Sets the default values and displays it on screen
                    windSpeedView.Text          = weatherInformation.wind.speed.ToString();
                    weatherDescriptionView.Text = weatherInformation.weather[0].description.ToString();
                    countryCodeView.Text        = weatherInformation.sys.country.ToString();

                    // A user can now choose to display optional values
                    minTmpCBox.Enabled      = true;
                    maxTmpCBox.Enabled      = true;
                    humidityCBox.Enabled    = true;
                    pressureCBox.Enabled    = true;
                    coordinatesCBox.Enabled = true;
                }
            }

            if (invalidSearch)
            {
                // Print debug info on display
                Toast.MakeText(this, $"Could not find any data for '{userInput.Text.ToString()}'", ToastLength.Long).Show();
                // Will reset values
                ResetDefaultComponents();
                ResetOptionalValues();
            }
            else
            {
                // If some options are left as chosen when user queries information
                // about a new city, the corresponding values will be override by new information
                OverrideOptionalValues();
            }
        }