Results 1 to 6 of 6

Thread: [RESOLVED] [3.0/LINQ] Get Value from Other Form

  1. #1

    Thread Starter
    Member
    Join Date
    May 2008
    Posts
    59

    Resolved [RESOLVED] [3.0/LINQ] Get Value from Other Form

    Hi, I've tried a lot of manipulations to get the current data in a textbox from other form but it always return an empty. How to get the recent value? below is part of the code i hv started:
    Code:
    --code in Form2
    Form1 frm1 = new Form1();
    frm2txtbox.text=frm1.frm1txtbox.text;
    i keep changing the text value of frm1txtbox at run time, but still frm2txtbox.text still empty. i've already set the modifier of the frm1txtbox into Public. need ur advise..

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [3.0/LINQ] Get Value from Other Form

    Hey,

    By default, TextBox are set to private in C#, so in order to access the control on another form, you either have to change the protection level of the form, or create a public property that will set and get the text.

    The code that you have above will always return null, since you are creating a new instance of Form2, which will always reset the Text Property of the form. However, I am going to assume that above it not all of the code.

    Here is one way of doing it, but bear in mind there are other ways:

    Main Form Code:
    Code:
        public partial class MainForm : Form
        {
            public MainForm()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                SecondForm secondForm = new SecondForm();
                secondForm.Owner = this;
                secondForm.Show();
            }
    
            public string TextBoxText
            {
                get
                {
                    return this.textBox1.Text;
                }
                set
                {
                    this.textBox1.Text = value;
                }
            }
        }
    Second Form Code:
    Code:
        public partial class SecondForm : Form
        {
            public SecondForm()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                MainForm mainForm = (MainForm)this.Owner;
                mainForm.TextBoxText = this.textBox1.Text;
            }
        }
    Here I have used the Owner Property to expose a reference to the first form from the second one.

    That make sense?

    Gary

  3. #3
    Junior Member
    Join Date
    Mar 2009
    Location
    Dhaka
    Posts
    18

    Re: [3.0/LINQ] Get Value from Other Form

    Thank u Gray.By your help i solved my problem

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [3.0/LINQ] Get Value from Other Form

    Hey,


    Glad to hear it.

    If you question has been answered, remember to mark your thread as resolved. If you are unsure how to do that, there is a link in my signature explaining how.

    Gary

  5. #5

    Thread Starter
    Member
    Join Date
    May 2008
    Posts
    59

    Re: [3.0/LINQ] Get Value from Other Form

    Thanks gep13

  6. #6
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] [3.0/LINQ] Get Value from Other Form

    Ha ha, when tuhin responded, I just assumed that they were the original poster of the thread Glad it is working for you as well jbatman.

    Gary

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