I am an absolute beginner and a bit desperate.
I found some answers to my question on google, but they are formulated in a way that is not very explanative and simple.


I have a mainform (form1) and a custom made dialog (form2).
Form1 has a ButtonAddString and a Label1.
Form, 2 has aTextbox1 and OK and Cancel buttons.

In runtime, when I push the ButtonAddString, it opens the Form2 dialog and I can write a string in dialog into Textbox1 and want it to pass it to the mainform to Label1.Text

The problem as I understand it, is that when I close the dialog, the instance of that Form2 form does not exists anymore. Also intelisense does not "provide" me with ability to read form2 values in form1 code.

Pls help.

Define an global object from dialogForm in the button which open this dialog which exists in MainForm initialize new dialogForm object then get its string instance.

More...

DialogForm contains public string object which has the value of the texrbox

MainForm has global object from DialogForm

class MainForm : Form
{
DialogForm dialogForm; 

On MainFormButtonClick
{
dialogForm = new DialogForm();
dialogForm.ShowDialog();
string whatWroteInDialogForm = dialogForm.stringValue;
dialogForm.Dispose();
}
}

Any miss understanding kindly reply..

>when I close the dialog, the instance of that Form2 form does not exists anymore
Not quite. Here's a common idiom that I see and use for dialogs:

MyDialog dlg = new MyDialog();

if ( dlg.ShowDialog() == DialogResult.OK ) {
  // Filling a text box, for this example
  this.textBox1.Text = dlg.ResultText;
}

ResultText would be a public property for the dialog:

internal class MyDialog: Form {
  private string _resultText;

  //...

  public string ResultText {
    get { return _resultText; }
    private set { _resultText = value; }
  }

  //...
}

Another way to communicate between forms is events, but with dialog forms that's usually overkill.

>when I close the dialog, the instance of that Form2 form does not exists anymore
Not quite. Here's a common idiom that I see and use for dialogs:

MyDialog dlg = new MyDialog();

if ( dlg.ShowDialog() == DialogResult.OK ) {
  // Filling a text box, for this example
  this.textBox1.Text = dlg.ResultText;
}

ResultText would be a public property for the dialog:

internal class MyDialog: Form {
  private string _resultText;

  //...

  public string ResultText {
    get { return _resultText; }
    private set { _resultText = value; }
  }

  //...
}

Another way to communicate between forms is events, but with dialog forms that's usually overkill.

Whoaaa..this helped :)

Looking at the code snippet and thinking about it helped me realize some things.

When I push the button in the mainform that causes the dialog to show up, I am creating and instance of the dialog class.
I need to implement a property and a field in the dialog class. Then, on runtime, when I write some text in the textbox in the dialog, and then push the ok button, for the button_click event, I assign the string value from textbox to the field of dialog class. Then it closes.

But I am able to read that property value of that dialog in main form and asign it to a label on the mainform. Tha also indicats that the dialog instance does not cease to exist , but is only closed.

Please correct me if my understanding is not correct, it is very hard to think on that level of abstraction in the beginnings.

Thanks again.

>Please correct me if my understanding is not correct
It sounds good to me.

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.