Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Accessing objects

Accessing objects

Scheduled Pinned Locked Moved C#
questionworkspace
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    John L DeVito
    wrote on last edited by
    #1

    Heyas all, Really trying my hardest to take all this information in. I come from a unix C environment and I'm finding the OO paradigm very aawkward so far, but I'm gonna keep it. My question is, If say I have a label on a form, and on a second form, I have a button that when clicked will populate the label with the text in a textbox from the first form. basically how do go about making the objects on a seperate form available from within another form. That sounds really confusing. I hope someone understands what I trying to say. P.S. - Yes, English is my first language :-) kha0s "There are 10 types of people in this world; Those that know binary and those that do not."

    F H 2 Replies Last reply
    0
    • J John L DeVito

      Heyas all, Really trying my hardest to take all this information in. I come from a unix C environment and I'm finding the OO paradigm very aawkward so far, but I'm gonna keep it. My question is, If say I have a label on a form, and on a second form, I have a button that when clicked will populate the label with the text in a textbox from the first form. basically how do go about making the objects on a seperate form available from within another form. That sounds really confusing. I hope someone understands what I trying to say. P.S. - Yes, English is my first language :-) kha0s "There are 10 types of people in this world; Those that know binary and those that do not."

      F Offline
      F Offline
      frank21
      wrote on last edited by
      #2

      Each form you create in .net is in itself a class so if you instantiate an object of one form within the code behind for the other form you will be able to access it's components ie: Within the Form2 code write Form1 myForm1 = new Form1(); Then also within the Form2 code write textboxname.DataSource = myForm1.Text.ToString(); and it should put the data in the approppriate place Please not what I have written is very basic there is a large amount of help for questions like yours in places like this website and msdn so don't be shy dig right in an get an early understanding of OOP with .net

      H 1 Reply Last reply
      0
      • F frank21

        Each form you create in .net is in itself a class so if you instantiate an object of one form within the code behind for the other form you will be able to access it's components ie: Within the Form2 code write Form1 myForm1 = new Form1(); Then also within the Form2 code write textboxname.DataSource = myForm1.Text.ToString(); and it should put the data in the approppriate place Please not what I have written is very basic there is a large amount of help for questions like yours in places like this website and msdn so don't be shy dig right in an get an early understanding of OOP with .net

        H Offline
        H Offline
        Heath Stewart
        wrote on last edited by
        #3

        Why are you calling Text.ToString()? The Text property is already a String. Calling ToString() is a waste.

        Microsoft MVP, Visual C# My Articles

        1 Reply Last reply
        0
        • J John L DeVito

          Heyas all, Really trying my hardest to take all this information in. I come from a unix C environment and I'm finding the OO paradigm very aawkward so far, but I'm gonna keep it. My question is, If say I have a label on a form, and on a second form, I have a button that when clicked will populate the label with the text in a textbox from the first form. basically how do go about making the objects on a seperate form available from within another form. That sounds really confusing. I hope someone understands what I trying to say. P.S. - Yes, English is my first language :-) kha0s "There are 10 types of people in this world; Those that know binary and those that do not."

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          You need to pass an instance of the first form to your second form. For example:

          public class Form1 : Form
          {
          internal Label label1;
          private Button button1;
          public Form1()
          {
          label = new Label();
          button1 = new Button();
          // Initialize...
          button1.Click += new EventHandler(button1_Click);
          }
          private void button1_Click(object sender, EventArgs e)
          {
          using (Form2 form = new Form2(this))
          form.ShowDialog(this);
          }
          }
          public class Form2 : Form
          {
          private TextBox textBox1;
          private Button button1;
          private Form1 form;
          public Form2(Form1 form)
          {
          this.form = form;
          textBox1 = new TextBox();
          button1 = new Button();
          // Initialize....
          button1.Click += new EventHandler(button1_Click);
          }
          private void button1_Click(object sender, EventArgs e)
          {
          if (form != null) form.Label.Text = textBox1.Text;
          }
          }

          Microsoft MVP, Visual C# My Articles

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups