how do i retrieve complex numbers (a + jb) so that just a appears in one textbox and just b appears in another textbox?

Dani AI

Generated

— the solution depends entirely on how the complex number is stored in the ListBox (as and asked). Three common cases and practical steps:

  1. ListBox stores real objects (recommended)
    If you put a numeric Complex object or a small ComplexNumber class into Items (and only use ToString for display), read the values directly from the object instead of parsing the display text. Example (System.Numerics.Complex):
using System.Numerics;
using System.Globalization;

if (listBox1.SelectedItem is Complex c)
{
    textBoxReal.Text = c.Real.ToString(CultureInfo.InvariantCulture);
    textBoxImag.Text = c.Imaginary.ToString(CultureInfo.InvariantCulture);
}

See the official Complex docs for details: System.Numerics.Complex

  1. ListBox stores strings like "a + jb"
    Normalize the string (remove spaces, remove the j or i), locate the separator between real and imaginary (skip a leading sign), then use TryParse to avoid exceptions. This approach handles "3+4j", "3+j4", "3 - j4", "-3+4j", etc.:
using System.Globalization;

string s = listBox1.SelectedItem?.ToString() ?? "";
s = s.Replace(" ", "").Replace("j", "").Replace("i", "");
int idx = s.IndexOf('+', 1);
if (idx == -1) idx = s.IndexOf('-', 1);

if (idx > 0 &&
    double.TryParse(s.Substring(0, idx), NumberStyles.Float, CultureInfo.InvariantCulture, out double real) &&
    double.TryParse(s.Substring(idx), NumberStyles.Float, CultureInfo.InvariantCulture, out double imag))
{
    textBoxReal.Text = real.ToString(CultureInfo.InvariantCulture);
    textBoxImag.Text = imag.ToString(CultureInfo.InvariantCulture);
}
else
{
    // handle unexpected formats or single-part numbers
}
  1. Best practices / troubleshooting
  • Prefer storing structured data (objects) in the ListBox and set DisplayMember for the UI; parsing displayed strings is fragile.
  • Use TryParse with an appropriate CultureInfo to handle decimal separators.
  • If users may enter many formats, add input validation or a short parser that accepts both i and j.
  • For multi-select ListBoxes, loop SelectedItems rather than using SelectedItem.

This avoids brittle string parsing and keeps the UI code simple and reliable (as hinted, setting Text is trivial once you have numeric values).

Recommended Answers

All 3 Replies

Depends on how your complex number is defined.
Is it just a string or is it a struct or a class? Please tell.

Well Chargerfan, you have to more specific about your question....

Ok, my knowledge of complex numbers is scant to say the least. This to me looks like a maths question hidden inside a c# question.

To answer the c# part just assign the Text property of the edit box with the value eg.

float a = 0.23f;
textBox1.Text = a.ToString();

If you are having problems with the maths visit here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.