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

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.