0

So, I'm working in C#, win forms. I have combobox with numbers from 1 to 10 in it. I am trying to read that integer like this:

int number = int.Parse(comboBox.SelectedText);

and I get error:

Error: Input string was not in the correct format

or if I try to read it like this:

int number = int.Parse(comboBox.GetItemText(comboBox.SelectedItem));

I can read only what is already written(1-10), but if I write by myself some integer, I get same error. I read with this first method all the time from TextBox and it is working perfectly fine. Please forgive me, this is my first month with C#

SᴇM
  • 7,024
  • 3
  • 24
  • 41
Falco Peregrinus
  • 507
  • 2
  • 7
  • 19
  • 1
    [`ComboBox.SelectedText`](https://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedtext(v=vs.110).aspx) doesn't do what you think it does. Use [`ComboBox.Text`](https://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.text(v=vs.110).aspx) instead. Moreover, it's recommended to use [`int.TryParse`](https://learn.microsoft.com/en-us/dotnet/api/system.int32.tryparse). – 41686d6564 stands w. Palestine May 05 '18 at 10:54
  • 1
    You should use `.Text` property. – JohnyL May 05 '18 at 10:55
  • Try [this](https://stackoverflow.com/questions/2831082/convert-combobox-string-value-to-int?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa) – Cosmin Ioniță May 05 '18 at 10:56
  • The first option is that the return value is not what you expect it to be. Store the value you retrieve in a temporary variable. Find out what is **actually** in there. On general parsing: How any number should look varries drastically between Regions of the world, even ones that share a common langauge. ToString() and Parae() will extract the current settings from the Windows the Application runs on. As regions change, how a DateTime or even Number has to look changes. In 94% of the cases you should not touch this. In 5% you have to remake it yourself. – Christopher May 05 '18 at 11:02
  • Ok, thank you everyone. I manage it with Text property. Oddly, first time I tried with it didn't work. As I sad , I just started with C# and oop so i ask stupid questions – Falco Peregrinus May 05 '18 at 11:52

0 Answers0