Results 1 to 2 of 2

Thread: [2.0] Close form and update textbox in the other?

  1. #1

    Thread Starter
    Hyperactive Member Pozzi's Avatar
    Join Date
    Feb 2001
    Location
    The Stones!
    Posts
    507

    Question [2.0] Close form and update textbox in the other?

    Good Afternoon,

    I'm new C#, so please bare with me...

    I have an application that loads a 'Main' form. This form contains, amongst other things a Text Box and a Button. When the user clicks on the button another form opens as a dialog form over the top and the user can select items from a Checked List Box, once done they click on the OK button on the second form which should close it and add the list created by the user to the Text Box on the main form.

    Doing such a thing in VB, no problem, but how do I do this in C#?

    I've searched and searched but nothing I've found has worked when tried.

    Any assistance, guidance or sample would be very much appreciated.

    Regards
    VB.Net (VS 2010)

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Close form and update textbox in the other?

    When you say doing it in VB, do you mean VB6 or VB.NET, because if you mean VB.NET then it's exactly the same in C#.

    You create an instance of the second form and display it by calling ShowDialog. In the second form, when you want to close it you do so by setting its DialogResult property to the appropriate value, e.g. OK or Cancel. That value is then returned by ShowDialog, so the caller knows what happened. If the result is OK then the caller can then call methods or properties of the second form to get the data it needs. This means that you need to declare methods and properties in the second form to return that data. Here's a simple example of a dialogue that has a TextBox and two Buttons. If the user presses the OK button the caller will retrieve the value they typed in the TextBox and display it.
    Code:
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
            using (Form2 dialogue = new Form2())
            {
                if (dialogue.ShowDialog() == DialogResult.OK)
                {
                    MessageBox.Show("You entered " + dialogue.UserEnteredValue);
                }
            }
        }
    }
    Code:
    public partial class Form2 : Form
    {
        // This read-only property returns the Text from the TextBox.
        public string UserEnteredValue
        {
            get
            {
                return this.textBox1.Text;
            }
        }
        
        public Form2()
        {
            InitializeComponent();
        }
    
        private void okButton_Click(object sender, EventArgs e)
        {
            // The user pressed the OK button so dismiss the form and return OK.
            this.DialogResult = DialogResult.OK;
        }
    
        private void cancelButton_Click(object sender, EventArgs e)
        {
            // The user pressed the Cancel button so dismiss the form and return Cancel.
            this.DialogResult = DialogResult.Cancel;
        }
    }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width