Diskuze: WPF - SelectedChanged metoda - objekt nebol vytvoreny
V předchozím kvízu, Test znalostí C# .NET online, jsme si ověřili nabyté zkušenosti z kurzu.
Člen
Zobrazeno 12 zpráv z 12.
//= Settings::TRACKING_CODE_B ?> //= Settings::TRACKING_CODE ?>
V předchozím kvízu, Test znalostí C# .NET online, jsme si ověřili nabyté zkušenosti z kurzu.
Majkel má pravdu bez toho kódu ti moc neporadíme, hoď sem kód té page kde máš ten error.
tak teda tu je:
public partial class MainWindow : Window
{
Language language;
Converter<LenghtUnit> converterLength;
Converter<WeightUnit> converterWeight;
Converter<DataUnit> converterData;
List<Language> languages;
Regex numberFormat;
Match checkNumberFormat;
public MainWindow()
{
InitializeComponent();
//nacitanie jazykov
language = new Language();
language.LoadLanguages();
//ulozenie dostupnych jazykov
languages = language.GetLanguages();
//pridanie jazykov do ComboBoxu
language.AddLanguages(LanguagesBox);
converterLength = new Converter<LenghtUnit>();
converterWeight = new Converter<WeightUnit>();
converterData = new Converter<DataUnit>(); //<-- Tu vytvorenie
numberFormat = new Regex(@"^\d+[\. | ,]?\d+$");
switch (inputLengthBox.SelectedValue.ToString())
{ //tu to funguje
case "System.Windows.Controls.ComboBoxItem: mm": converterLength.InputUnit = LenghtUnit.MM; break;
case "System.Windows.Controls.ComboBoxItem: cm": converterLength.InputUnit = LenghtUnit.CM; break;
case "System.Windows.Controls.ComboBoxItem: m": converterLength.InputUnit = LenghtUnit.M; break;
case "System.Windows.Controls.ComboBoxItem: km": converterLength.InputUnit = LenghtUnit.KM; break;
}
}
//Length ComboBox
private void inputLengthBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
switch (inputLengthBox.SelectedValue.ToString())
{ //tu uz nie :
//An exception of type 'System.NullReferenceException' occurred in PrevodnikJednotiek.exe but was not handled in user code
//Additional information: Object reference not set to an instance of an object.
case "System.Windows.Controls.ComboBoxItem: mm": converterLength.InputUnit = LenghtUnit.MM; break;
case "System.Windows.Controls.ComboBoxItem: cm": converterLength.InputUnit = LenghtUnit.CM; break;
case "System.Windows.Controls.ComboBoxItem: m": converterLength.InputUnit = LenghtUnit.M; break;
case "System.Windows.Controls.ComboBoxItem: km": converterLength.InputUnit = LenghtUnit.KM; break;
}
}
Ale selectedValue nemam nulovy, nulovy je objekt converterLength v metode SelectionChanged.
Je to zvláštní no, bez celého kódu to asi nepude. Takhle jak to tam máš by to mělo fungovat. Otázka nyní je, jestli v nějakém mezikroku co tu nevidíme se to rozhodí.
Anarchie instancí
Tu je celý kód:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
using System.Globalization;
using System.Threading;
namespace PrevodnikJednotiek
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
Language language;
Converter<LenghtUnit> converterLength;
Converter<WeightUnit> converterWeight;
Converter<DataUnit> converterData;
List<Language> languages;
Regex numberFormat;
Match checkNumberFormat;
public MainWindow()
{
converterLength = new Converter<LenghtUnit>();
converterWeight = new Converter<WeightUnit>();
converterData = new Converter<DataUnit>(); //<-- Tu vytvorenie
InitializeComponent();
//nacitanie jazykov
language = new Language();
language.LoadLanguages();
//ulozenie dostupnych jazykov
languages = language.GetLanguages();
//pridanie jazykov do ComboBoxu
language.AddLanguages(LanguagesBox);
numberFormat = new Regex(@"^\d+[\. | ,]?\d+$");
#region nastavenie jazyka pre GUI
foreach (Language lang in languages)
{
if (lang.LangID.Contains(language.SystemLanguage))
{
language.ChangeLanguage(lang.LangID, ConvertBTN, languageTB); // zmeni jazyk GUI
break;
}
else
{
language.ChangeLanguage("SK", ConvertBTN, languageTB); // zmeni jazyk GUI
}
}
#endregion
#region nastavenie defaultnych hodnot comboboxu
switch (inputLengthBox.SelectedValue.ToString())
{ //tu to funguje
case "System.Windows.Controls.ComboBoxItem: mm": converterLength.InputUnit = LenghtUnit.MM; break;
case "System.Windows.Controls.ComboBoxItem: cm": converterLength.InputUnit = LenghtUnit.CM; break;
case "System.Windows.Controls.ComboBoxItem: m": converterLength.InputUnit = LenghtUnit.M; break;
case "System.Windows.Controls.ComboBoxItem: km": converterLength.InputUnit = LenghtUnit.KM; break;
}
#endregion
}
private void Convert_Click(object sender, RoutedEventArgs e)
{
if (!inputLength.Text.ToString().Equals(""))
{
checkNumberFormat = numberFormat.Match(inputLength.Text.ToString());
if (checkNumberFormat.Success)
converterLength.InputValue = Double.Parse(inputLength.Text.Replace(',', '.'), NumberStyles.AllowDecimalPoint, NumberFormatInfo.InvariantInfo);
else
MessageBox.Show(language.CurrentLanguage);
}
if (!inputWeight.Text.ToString().Equals("")) {
checkNumberFormat = numberFormat.Match(inputWeight.Text.ToString());
if( checkNumberFormat.Success)
converterWeight.InputValue = Double.Parse(inputWeight.Text.Replace(',', '.'), NumberStyles.AllowDecimalPoint, NumberFormatInfo.InvariantInfo);
else
MessageBox.Show(language.CurrentLanguage);
}
if (!inputData.Text.ToString().Equals("")) {
checkNumberFormat = numberFormat.Match(inputData.Text.ToString());
if( checkNumberFormat.Success)
converterData.InputValue = Double.Parse(inputData.Text.Replace(',', '.'), NumberStyles.AllowDecimalPoint, NumberFormatInfo.InvariantInfo);
else
MessageBox.Show(language.CurrentLanguage);
}
//prevedenie hodnot
converterLength.Convert();
converterWeight.Convert();
converterData.Convert();
//vypisanie hodnot
converterLength.ShowResults(outputLength);
converterWeight.ShowResults(outputWeight);
converterData.ShowResults(outputData);
}
#region Selection Events
//Zmena jazyka
private void LanguagesBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
language.ChangeLanguage(LanguagesBox.SelectedItem.ToString(), ConvertBTN, languageTB);
}
//Length ComboBox
private void inputLengthBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
switch (inputLengthBox.SelectedValue.ToString())
{ //tu uz nie :
//An exception of type 'System.NullReferenceException' occurred in PrevodnikJednotiek.exe but was not handled in user code
//Additional information: Object reference not set to an instance of an object.
case "System.Windows.Controls.ComboBoxItem: mm": converterLength.InputUnit = LenghtUnit.MM; break;
case "System.Windows.Controls.ComboBoxItem: cm": converterLength.InputUnit = LenghtUnit.CM; break;
case "System.Windows.Controls.ComboBoxItem: m": converterLength.InputUnit = LenghtUnit.M; break;
case "System.Windows.Controls.ComboBoxItem: km": converterLength.InputUnit = LenghtUnit.KM; break;
}
}
#endregion
}
}
Zobrazeno 12 zpráv z 12.