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.
Absolvováno
Jak se ti líbí článek?
Před uložením hodnocení, popiš prosím autorovi, co je špatněZnaků 0 z 50-500
Jak se ti kurz líbí?
Tvé hodnocení kurzuZnaků 0 z 50-500

Člen

Odpovídá na joci
Jakub Lásko[Saarix]:7.7.2015 9:03
Jakub Lásko[Saarix]:7.7.2015 9:03
Majkel má pravdu bez toho kódu ti moc neporadíme, hoď sem kód té page kde máš ten error.
joci:7.7.2015 17:16
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;
}
}
Odpovídá na Jan Vargovský
joci:9.7.2015 22:10
joci:9.7.2015 22:10
Ale selectedValue nemam nulovy, nulovy je objekt converterLength v metode SelectionChanged.
Jakub Lásko[Saarix]:10.7.2015 7:16
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í
joci:11.7.2015 20:11
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
}
}
Děláme co je v našich silách, aby byly zdejší diskuze co nejkvalitnější. Proto do nich také mohou přispívat pouze registrovaní členové. Pro zapojení do diskuze se přihlas. Pokud ještě nemáš účet, zaregistruj se, je to zdarma.
Zobrazeno 12 zpráv z 12.