Hello,

I'm having an issue trying to get the data from several TextBoxes in FormB to FormA.
In FormB I use a string to gather the data from the TextBox like so:

private void asBtnOk_Click(object sender, EventArgs e)
        {
            string asRate = asBoxRate.Text;
        }

Then I go to FormA and I try to write the asRate string data to a Text File Like so:

using (StreamWriter sws = new StreamWriter("Settings.txt"))
            {
                sws.WriteLine("rate \"" + asRate, "\"");
            }

I Believe my problem is that the string is in a "private" object but I don't know of any other way to get this to work.

So, Any and all help is greatly appreciated.


Thanks in advance.
- Poab9200

Dani AI

Generated

Short version: the value is almost always lost because either it was declared only inside the OK button handler (local scope), you read a different FormB instance than the one the user closed, or you showed the form modelessly and read the value before the dialog finished. , and all pointed to pieces of this — combine them into a single flow and it will work.

Checklist to fix it

  • Keep FormB controls private and expose a public read-only property (or method) that returns the textbox value. Do not make the textbox itself public.
  • In FormB's OK handler assign whatever backing field/property you use and set the form result (DialogResult = OK) before closing so ShowDialog can return OK.
  • From FormA create one FormB instance, call ShowDialog (modal). After ShowDialog returns, check the DialogResult and read the property from that same FormB instance. Do not create a new FormB just to read the value.
  • If you use a using block for the dialog, read the value inside the block after ShowDialog returns (the using disposes at block end).

Troubleshooting pointers

  • Put a breakpoint immediately after ShowDialog and inspect the property. If it is empty, the OK handler probably never set it or the DialogResult logic closed the form prematurely.
  • Verify the file location: a relative "Settings.txt" is usually created under bin\Debug (or the process working folder). Use an explicit path while testing to avoid confusion, and remember StreamWriter(path) overwrites by default.
  • If you opened the form with Show (modeless), you must read the value on FormClosed (or via an event), because Show returns immediately.

Alternatives for future design

  • Raise a custom event from FormB carrying the value and have FormA subscribe.
  • Use a small settings object or Properties.Settings to persist values across sessions.

Applying the modal dialog pattern and confirming you read the value from the same instance immediately after ShowDialog will resolve the behavior you observed.

Recommended Answers

All 7 Replies

>I Believe my problem is that the string is in a "private" object
That's the way it should be. I've had to smack co-workers on the head before for solving this problem by making those data members public. :icon_rolleyes: Anyway, you can access the data from another form through properties as long as you have an object of the form:

internal class FormA: Form {
  // ...

  public FormA()
  {
    InitializeComponent();

    using ( FormB dlg = new FormB() ) {
      if ( dlg.ShowDialog() == DialogResult.OK ) {
        using ( StreamWriter sws = new StreamWriter ( "Settings.txt" ) )
          sws.WriteLine ( "rate \"" + dlg.Rate, "\"" );
      }
    }
  }
}

internal class FormB: Form {
  private string asRate;

  public string Rate
  {
    get { return asRate; }
  }

  // ...

  private void asBtnOk_Click ( object sender, EventArgs e )
  {
    asRate = asBoxRate.Text;
  }
}

the problem is that asRate is declared in the click event of the button, declare it as public or as a property of the Form and then you cand acces it ...
so where you have
public partial class FormB:Form
{
// if you use .net 3.5 you can do it like
// public string asRate { get; set; }
public string asRate = "";
..
..
..
}


and then in FormA you can access it like :

sws.WriteLine("rate \"" + formB.asRate, "\"");

-- later edit.. is see someone posted already :P ... Narue ... fast one :) .

>I Believe my problem is that the string is in a "private" object
That's the way it should be. I've had to smack co-workers on the head before for solving this problem by making those data members public. :icon_rolleyes: Anyway, you can access the data from another form through properties as long as you have an object of the form:

internal class FormA: Form {
  // ...

  public FormA()
  {
    InitializeComponent();

    using ( FormB dlg = new FormB() ) {
      if ( dlg.ShowDialog() == DialogResult.OK ) {
        using ( StreamWriter sws = new StreamWriter ( "Settings.txt" ) )
          sws.WriteLine ( "rate \"" + dlg.Rate, "\"" );
      }
    }
  }
}

internal class FormB: Form {
  private string asRate;

  public string Rate
  {
    get { return asRate; }
  }

  // ...

  private void asBtnOk_Click ( object sender, EventArgs e )
  {
    asRate = asBoxRate.Text;
  }
}

OK, I've tried this method and still no luck.
When I try your example I click the "create" button and another form comes up.
The same exact form that resembles FormB, that is fine.
But when I enter in the data
into the asRate.Textbox and I click the OK button to write the data to the settings.txt file, and I open it using a separate button that I added to the Main Form for ease of testing their is no data.

Why is this?

Thanks in advance,
- Poab9200

Because the form is local to the create button

Well do you know of any way I may solve this issue?

Make the second form a local variable to your main form, so it remains accessible and obviously you wouldnt necessarily need to make new instances of it if you need to show it more than once..

Or, a better way would be to have private variables on your main form, that you assign an event to your second forms close so that it updates on closing the form, and remembers those settings until you change them

Ok, I understand what you have stated above, but I am just learning C# and I know for a fact It's going to be quite frustrating but may you give me an example. Like I said, I understand what your saying It's just I have no clue on what needs to be done to accomplish my task.

Sorry for the aggrevation

- Thanks in advance.
Poab9200

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.