Thread: Update FOrm textboxs from a seperate class

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    85

    Update FOrm textboxs from a seperate class

    I have a Form - Form1, which contains several textboxs all with private accesibility.

    i also have a class - Populate, from which i want to be able to modify the textbox.text values on form1 when i call into it.

    i have tried various methods but nothing seems to give me what i want.

    what is the best way to allow my code to modify textbox.text values WITHOUT compromising the integrity of my original form ? i dont want to expose the textboxs with public accessibility.

    I am calling the method in the populate class from within a ' comboBox1_SelectedIndexChanged ' method which passes over 2 parameters - namely a XMLdocument and a string.

    This method will eventually return a new string but i need to be able to say form1.textbox1.text = "blah blah" in this method and i cant access the textboxs.

    any ideas please. im sure i have done something before where i passed the form to the method but i cant get that to work either and thought there must be another way of exposing the textboxs anyway.

    thanks

    just an after thought - if i instantiate a class at the start of the class ( i.e. form1) rather than within the method i need to access it from (ie comboBox1_SelectedIndexChanged) is there any substantial memory or security issues here ? i understand that fgrom the start of the class it will be available to all methods with the class but does this haver a major impact?
    Last edited by deviousdexter; 05-07-2009 at 09:47 AM.

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Quote Originally Posted by deviousdexter View Post
    This method will eventually return a new string but i need to be able to say form1.textbox1.text = "blah blah" in this method and i cant access the textboxs.
    Why would you need to do this? Maybe you can describe what you want to achieve. You can always expose a public interface like a property or method to your Populate class so you can have it your way, but this seems a bit fishy and not the best design decision.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    85
    I am trying to do it this way to try to make the method in populate more flexible for future use.

    I also want to ensure it is done the correct ( safe) way., rather than one that does the job.

    if my xmldocument has 4 strings in it that i want to use i.e "tom" "fred "anne" "betty" and i pass it to the populate class.
    within the populate class i want to be able to find the string "betty" and put in in sya textbox2.text, put "fred" in textbox3.text and return "tom" to the calling method (which i can do).

    so i need to know how to access the tecxtbox from within the populate class.

    Code:
    // form1
    
    public Form1()
            {
                InitializeComponent();
                
                String newstring = null;
    
                 string mystring = "hello";
                 XmlDOcument myXmldocument = new XmlDOcument(); // this is already opened prior to the index changing in combo box
               // contains 2 textboxs - textbox1 - textbox2.
             }
    
    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
    
             Populate pop = new Populate();
    
             newstring = pop.populatemember(myXmldocument, mystring);
            }
    
     //populate.cs
    
    public class Populate
           {
    
            public string PopulateMember(XmlDocument myDoc, string name)
               {
                  Form1.textbox1.Text = " blah blah";
                  Form2.textbox2.Text = "blah blah";
    
                  
                 return mynewstring;
    
                }
    hope this makes it clearer :S

    ]
    edit-
    sry Nvoigt completely misinterpreted your question.

    i have various data in my xmldocument which i want on form1 but i also need to build a new string for a url from data contained with in the xml hence the return string - this will hold my new URL. But while i am parsing the xml i thought it would be easier to update the textboxs at the same time.
    Last edited by deviousdexter; 05-07-2009 at 10:22 AM.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    Code:
    // form1
    
    public Form1()
            {
                InitializeComponent();
                
                String newstring = null;
    
                 string mystring = "hello";
                 XmlDOcument myXmldocument = new XmlDOcument(); // this is already opened prior to the index changing in combo box
               // contains 2 textboxs - textbox1 - textbox2.
             }
    
    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
    
             Populate pop = new Populate(this);
    
             newstring = pop.populatemember(myXmldocument, mystring);
            }
    
     //populate.cs
    
    public class Populate
           {
            private Form parent;
    
            public Populate(Form parent)
            {
                this.parent = parent;
            }
    
            public string PopulateMember(XmlDocument myDoc, string name)
               {
                  this.parent.textbox1.Text = " blah blah";
                  this.parent.textbox2.Text = "blah blah";
    
                  
                 return mynewstring;
    
                }

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    85
    I have tried the above changes

    but still says 'no definition found'.

    typing ' this.parent.' and checking the intellisense panel does not show any of the textboxs i have on form1.

    My initial theory for this piece of code was to tidy the form1 code and keep it easy to follow.
    Maybe i am going about this the wrong way and shoudl only have the class open and return the XMLDocument to form1 and keep all the textbox changes on form1.

    Is it advisable to keep all form manipulation specific code in with the Form1 class. i.e. modifying buttons, drop downs, etc etc ?
    Last edited by deviousdexter; 05-07-2009 at 11:59 AM.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    textbox1 and textbox2 are public right?

    Ok, another way:

    Code:
    // form1
    
    public Form1()
            {
                InitializeComponent();
                
                String newstring = null;
    
                 string mystring = "hello";
                 XmlDOcument myXmldocument = new XmlDOcument(); // this is already opened prior to the index changing in combo box
               // contains 2 textboxs - textbox1 - textbox2.
             }
    
    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
    
             Populate pop = new Populate();
    
             newstring = pop.populatemember(myXmldocument, mystring, textbox1, textbox2);
            }
    
     //populate.cs
    
    public class Populate
           {
            public string PopulateMember(XmlDocument myDoc, string name, TextBox box1, TextBox box2)
               {
                  box1.Text = " blah blah";
                  box2.Text = "blah blah";
    
                  
                 return mynewstring;
    
                }

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    85
    i have alot of textboxs and didnt really want to pass them all one by one - if i could pass them as a collection that would be better.

  8. #8
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    Set the textboxes to public, then they'll appear when you use this.parent...

    Here's a test project I made which does exactly what you're asking for:

    Code:
    // Form1 designer
    
    namespace box_test
    {
        partial class Form1
        {
            private System.ComponentModel.IContainer components = null;
    
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                    components.Dispose();
    
                base.Dispose(disposing);
            }
    
            private void InitializeComponent()
            {
                this.textBox1 = new System.Windows.Forms.TextBox();
                this.textBox2 = new System.Windows.Forms.TextBox();
                this.SuspendLayout();
                this.textBox1.Location = new System.Drawing.Point(21, 27);
                this.textBox1.Name = "textBox1";
                this.textBox1.Size = new System.Drawing.Size(100, 20);
                this.textBox1.TabIndex = 0;
                this.textBox2.Location = new System.Drawing.Point(21, 53);
                this.textBox2.Name = "textBox2";
                this.textBox2.Size = new System.Drawing.Size(100, 20);
                this.textBox2.TabIndex = 1;
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(284, 264);
                this.Controls.Add(this.textBox2);
                this.Controls.Add(this.textBox1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.ResumeLayout(false);
                this.PerformLayout();
    
            }
    
            public System.Windows.Forms.TextBox textBox1;
            public System.Windows.Forms.TextBox textBox2;
        }
    }
    
    
    // Form1
    
    using System.Windows.Forms;
    
    namespace box_test
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                SomeObject test = new SomeObject();
                test.populateboxes(this);
            }
        }
    }
    
    
    // some object
    
    using System.Windows.Forms;
    
    namespace box_test
    {
        class SomeObject
        {
            public void populateboxes(Form1 parent)
            {
                parent.textBox1.Text = " blah blah";
                parent.textBox2.Text = "blah blah";
            }
        }
    }
    Last edited by theoobe; 05-07-2009 at 04:03 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  2. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  3. Replies: 4
    Last Post: 11-15-2008, 06:11 PM
  4. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  5. Question about getting an info class from another Form
    By Joelito in forum C# Programming
    Replies: 0
    Last Post: 10-16-2006, 01:02 PM