none
Passing data from class to form in C#

    Pergunta

  •  

    Hi there

    How can pass data from one class located in class.cs to textbox or function located in form,I have tried some idea  which did pass the data but form controls are not dispalying the data ,please help me

     

     

     

    the first one

    public void Matc(MatchCollection matches,List<string> Href){  

    MSC_Project_01.Form2 Formed = new MSC_Project_01.Form2();

    do some thing here and passed to Output function

    Formed .Output(ref linedfounded);

     

    function inserted on form project

    public void Output( ref List<string> hyperlinked) {

     foreach (string linked in hyperlinked) {

    textBox1.Text = linked ;}}

    textbox setting

     private System.Windows.Forms.TextBox textBox1;

     

    please help me out as this give some hard time ,

    Many thanks

    sábado, 23 de outubro de 2010 14:51

Respostas

  • OK, I still don't know where those other classes are getting instantiated. But, I'll assume for the moment that they are variables on your main form ... which they'd have to be if you've only got the one main form.

    There are several ways you can handle this, but I'll give you the easiest. First, if we continue to use your class "name" the way you've currently structured it, it should look like this instead:

    Public void class name () 
    {
     public name(Form1 form)
     {
      string message = "some message here";
      form.Outputmessage(message);
     }
    }
    
    
    

    And then, Form1 might look like this:

    public partial class Form1
    {
     private name MyNameClass;
    
     public void Outputmessage(string message)
     {
      this.textBox1.Text = message
     }
    
     // Now, say you have a button-click on your Form to instantiate MyNameClass
     private void button_Click(object sender, EventArgs e)
     {
      this.MyNameClass = new name();
     }
    }
    
    

    When the button is clicked, the name class is instantiated and in its constructor it calls the Outputmessage() method. Not very elegant, but it would get the job done.

     


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    • Marcado como Resposta sager79 sábado, 23 de outubro de 2010 21:20
    sábado, 23 de outubro de 2010 19:53
  •  

     

    Thanks to you all ,the issue i been resloved ,my project is now working fine,both your idea is working ,i post my simple again for other people who have this issue

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Windows.Forms;

     

    namespace WindowsFormsApplication2

    {

        class exmaple

        {

            WindowsFormsApplication2.Form1  formed;

            string text;

     

            public exmaple(Form1 formed)

            {

                this.formed = formed;

            }

     

            public void testexmaple()

            {

                text = "meassge here ";

     

                formed.test(text);

            }

        }

     

     

    }

     

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Linq;

    using System.Text;

    using System.Windows.Forms;

     

    namespace WindowsFormsApplication2

    {

        public partial class Form1 : Form

        {

            private exmaple exmapled;

     

            public Form1()

            {

                InitializeComponent();

            }

     

            private void Form1_Load(object sender, EventArgs e)

            {

                exmaple exmapled = new exmaple(this);

            }

     

            public void test(string text)

            {

                textBox1.Text = text;

            }

     

            private void button1_Click(object sender, EventArgs e)

            {

                exmapled = new exmaple(this);

                exmapled.testexmaple();

            }

        }

    }

     

     

     

    • Marcado como Resposta sager79 terça-feira, 26 de outubro de 2010 12:33
    sábado, 23 de outubro de 2010 21:19

Todas as Respostas

  • Have you actually put any strings in the linedfounded variable? (Which I assume is a List<string>). The little bit of code you have posted looks like it should work ok, so we're not seeing the part that's causing the problem.

    BTW, you don't need to explicitly pass your List<string> by reference, but that wouldn't be causing your problem.


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    sábado, 23 de outubro de 2010 16:04
  • Sorry, I'm kind of lost with that last bit of code you posted.

    I recommend that you try to follow your code while debugging. Set a breakpoint on the line where you're setting the TextBox.Text property and see what the string is that you're trying to set it to.


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    sábado, 23 de outubro de 2010 16:33
  • Hi there

    I know the code is working fine ,but am not sure why the values are not displayed in form, I do have issue even in passing message to from classes to form function, these messages are passed but "these are not displayed in form  textbox " when the application running

    I’d  be really  helpful, if you  help me in one point to  which is " how can i pass messages(string) or value from class file let say class1.cs to form (form1)

      Just example

    class1.cs

     

    Public void class name ()

     {

    MSC_Project_01.Form1 Formed = new MSC_Project_01.Form1();

     message="some meessage here";

    Formed.Outputmessage(message);

    }

     form1.cs  window application form

    public void Outputmessage(string Text) {

    textBox3.Text += DateTime.Now.ToLongTimeString() + ": " + Text + "\r\n";

       

     

     

    sábado, 23 de outubro de 2010 16:33
  • Is "form1" your main form? How/where are you instantiating and calling the class "name"? If form1 is your main form and if you're expecting the message to show up based on the class "name" instantiating a new form1, that's not going to show up on your existing  form1 ... I don't know if that is the situation here or not though. If you don't quite understand what I mean, then in your class "name", after instantiating Formed, try calling Formed.Show() after you've called Formed.Outputmessage() and you'll see what I mean.

    If what I've just written is how you're trying to run this, then I need more information to help you do it correctly ... basically I need to know where the class "name" fits into the picture and you expect to use it.


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    sábado, 23 de outubro de 2010 18:56
  • Hello,

     

    First thing first, you have to make sure that the message you are trying to send is coming from the MainThread, not from a BackgroundTread.

    Second, you can just use a property on the form like so:

        public string LabelText
        {
          get
          {
            return label1.Text;
          }
          set
          {
            label1.Text = value;
          }
        }
    

    and access that from your class.

    If you still need more information i have prepared 2 forms on which you can test this:

      public class Form1 : Form
      {
        private Form2 form2 = new Form2();
    
        public Form1()
        {
          InitializeComponent();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
          if (form2.IsDisposed || !form2.Visible)
          {
            form2 = new Form2();
            form2.Show();
          }
    
          form2.LabelText = textBox1.Text;
        }
    
        #region DESIGNER CODE
    
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
    
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
          if (disposing && (components != null))
          {
            components.Dispose();
          }
          base.Dispose(disposing);
        }
    
        #region Windows Form Designer generated code
    
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
          this.textBox1 = new System.Windows.Forms.TextBox();
          this.button1 = new System.Windows.Forms.Button();
          this.SuspendLayout();
          //
          // textBox1
          //
          this.textBox1.Location = new System.Drawing.Point(13, 230);
          this.textBox1.Name = "textBox1";
          this.textBox1.Size = new System.Drawing.Size(178, 20);
          this.textBox1.TabIndex = 1;
          //
          // button1
          //
          this.button1.Location = new System.Drawing.Point(197, 230);
          this.button1.Name = "button1";
          this.button1.Size = new System.Drawing.Size(75, 23);
          this.button1.TabIndex = 2;
          this.button1.Text = "Send Message";
          this.button1.UseVisualStyleBackColor = true;
          this.button1.Click += new System.EventHandler(this.button1_Click);
          //
          // Form1
          //
          this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
          this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
          this.ClientSize = new System.Drawing.Size(284, 262);
          this.Controls.Add(this.button1);
          this.Controls.Add(this.textBox1);
          this.Name = "Form1";
          this.Text = "Form1";
          this.ResumeLayout(false);
          this.PerformLayout();
        }
    
        #endregion Windows Form Designer generated code
    
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Button button1;
    
        #endregion DESIGNER CODE
      }
    
      public class Form2 : Form
      {
        public Form2()
        {
          InitializeComponent();
        }
    
        public string LabelText
        {
          get
          {
            return label1.Text;
          }
          set
          {
            label1.Text = value;
          }
        }
    
        #region DESIGNER CODE
    
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
    
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
          if (disposing && (components != null))
          {
            components.Dispose();
          }
          base.Dispose(disposing);
        }
    
        #region Windows Form Designer generated code
    
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
          this.label1 = new System.Windows.Forms.Label();
          this.SuspendLayout();
          //
          // label1
          //
          this.label1.AutoSize = true;
          this.label1.Location = new System.Drawing.Point(13, 13);
          this.label1.Name = "label1";
          this.label1.Size = new System.Drawing.Size(35, 13);
          this.label1.TabIndex = 0;
          //
          // Form1
          //
          this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
          this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
          this.ClientSize = new System.Drawing.Size(284, 262);
          this.Controls.Add(this.label1);
          this.Name = "Form1";
          this.Text = "Form2";
          this.ResumeLayout(false);
          this.PerformLayout();
        }
    
        #endregion Windows Form Designer generated code
    
        private System.Windows.Forms.Label label1;
    
        #endregion DESIGNER CODE
      }
    
    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    sábado, 23 de outubro de 2010 19:05
  •  

    Thanks for reply ,i think you miss understood me,

    I have only one main form ,and four classes are there to do some work ,i do not have aonther form ,so form.show() method is not what looking for, and not idea for me issue ,

    my issue , i have  four classes inside Windows Forms Application project ,these classes are send values and message to main from in order to display them ,so, the values and messages are passed throught ,but fro some reason are not dispalying on main from (textbox,listview and so on )

    My question

    How can i   pass values and messages to the main form (only one form ) ,from four or more classes ,the values and messages are passed beacuse there is outcome of work these classes done ,refer to my example please ,

    Many thanks

     

    sábado, 23 de outubro de 2010 19:27
  • Sorry, I'm kind of lost with that last bit of code you posted.

    I recommend that you try to follow your code while debugging. Set a breakpoint on the line where you're setting the TextBox.Text property and see what the string is that you're trying to set it to.


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    I have  follow my code while debugging. the TextBox.Text got the passed values but not dispaying it on the main form ,i have only one form ,form.show  () method is not what i am looking for,it 's dispays my forms many times which not idea for me
    sábado, 23 de outubro de 2010 19:37
  • In your example, you are always creating a new form, so you are always sending values to a new form instance , that's why the values are not changing on your form (it is a different instance).

    You could always transform your form into a singleton, that way you would have just one instance, but this is just a suggestion,

     

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    sábado, 23 de outubro de 2010 19:41
  • OK, I still don't know where those other classes are getting instantiated. But, I'll assume for the moment that they are variables on your main form ... which they'd have to be if you've only got the one main form.

    There are several ways you can handle this, but I'll give you the easiest. First, if we continue to use your class "name" the way you've currently structured it, it should look like this instead:

    Public void class name () 
    {
     public name(Form1 form)
     {
      string message = "some message here";
      form.Outputmessage(message);
     }
    }
    
    
    

    And then, Form1 might look like this:

    public partial class Form1
    {
     private name MyNameClass;
    
     public void Outputmessage(string message)
     {
      this.textBox1.Text = message
     }
    
     // Now, say you have a button-click on your Form to instantiate MyNameClass
     private void button_Click(object sender, EventArgs e)
     {
      this.MyNameClass = new name();
     }
    }
    
    

    When the button is clicked, the name class is instantiated and in its constructor it calls the Outputmessage() method. Not very elegant, but it would get the job done.

     


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    • Marcado como Resposta sager79 sábado, 23 de outubro de 2010 21:20
    sábado, 23 de outubro de 2010 19:53
  • using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Linq;

    using System.Text;

    using System.Windows.Forms;

     

    namespace WindowsFormsApplication2

    {

        public partial class Form1 : Form

        {

     

     

            private exmaple exmapled = new exmaple();

          

     

     

            public Form1()

            {

                InitializeComponent();

              

            }

     

            private void Form1_Load(object sender, EventArgs e)

            {

              

               

            }

            public void test(string text)

            {

     

                textBox1.Text = text;

            }

     

            private void button1_Click(object sender, EventArgs e)

            {

                exmapled.testexmaple();

              

            }

        }

    }

     

    class

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Windows.Forms;

    namespace WindowsFormsApplication2

    {

        class exmaple

        {

            

            string text;

            public exmaple()

            {

              

            }

            public void testexmaple()

            {

                 Form1 formed = new Form1();

                 text = "example";

                 formed.test(text);

             

             }

        }

    }

     

     

     

    sábado, 23 de outubro de 2010 19:54
  • Like i said, you are creating the form again => a new reference , you should change things like so:

     

     class
    
     exmaple
     {
      private
    
     Form3 formed;
      string
    
     text;
    
      public
    
     exmaple(Form3 formed)
      {
       this
    
    .formed = formed;
      }
    
      public
    
     void
    
     testexmaple()
      {
       text = "example"
    
    ;
    
       formed.test(text);
      }
     }
    
     public
    
     partial
    
     class
    
     Form3 : Form
     {
      private
    
     exmaple exmapled = new
    
     exmaple(this
    
    );
    
      public
    
     Form3()
      {
       InitializeComponent();
      }
    
      private
    
     void
    
     Form1_Load(object
    
     sender, EventArgs e)
      {
      }
    
      public
    
     void
    
     test(string
    
     text)
      {
       textBox1.Text = text;
      }
    
      private
    
     void
    
     button1_Click(object
    
     sender, EventArgs e)
      {
       exmapled.testexmaple();
      }
     }
    
    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga

     

     


    If a post answers your question, please click "Mark As Answer " on that post and "Mark as Helpful ".
    • Editado Emanuel, Varga sábado, 23 de outubro de 2010 20:19 fixed mistake mentioned in next post
    sábado, 23 de outubro de 2010 19:59
  • Like i said, you are creating the form again => a new reference , you should change things like so:

     

     class exmaple
    
     {
    
      private Form formed;
    
      string text;
    
    
    
      public exmaple(Form formed)
    
      {
    
       this.formed = formed;
    
      }
    
    
    
      public void testexmaple()
    
      {
    
       text = "example";
    
    
    
       formed.test(text);
    
      }
    
     }
    
    
    
     public partial class Form3 : Form
    
     {
    
      private exmaple exmapled = new exmaple(this);
    
    
    
      public Form3()
    
      {
    
       InitializeComponent();
    
      }
    
    
    
      private void Form1_Load(object sender, EventArgs e)
    
      {
    
      }
    
    
    
      public void test(string text)
    
      {
    
       textBox1.Text = text;
    
      }
    
    
    
      private void button1_Click(object sender, EventArgs e)
    
      {
    
       exmapled.testexmaple();
    
      }
    
     }
    
    
    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga

     

     


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".


     I am sorry ,how this does actual work ,i have tried you idea but i have two error out of this change

    Error 1 'System.Windows.Forms.Form' does not contain a definition for 'test' and no extension method 'test' accepting a first argument of type 'System.Windows.Forms.Form' could be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\Emad\My Documents\WindowsFormsApplication2\WindowsFormsApplication2\exmaple.cs 23
    Error 2 Keyword 'this' is not available in the current context C:\Documents and Settings\Emad\My Documents\WindowsFormsApplication2\WindowsFormsApplication2\Form1.cs 14

    sábado, 23 de outubro de 2010 20:15
  • Sorry, i was in a hurry and missed something in the conversion:

    change this:

     

    private
    
     Form formed;
    
    

    to this:

    private Form3 formed;
    

    and

     public exmaple(Form formed)
    

    to this:

     public exmaple(Form3 formed)

    in my example.

     

    Best Regards,
    Emanuel Varga

     


    If a post answers your question, please click "Mark As Answer " on that post and "Mark as Helpful ".
    • Editado Emanuel, Varga sábado, 23 de outubro de 2010 20:18 fixed the constructor also
    sábado, 23 de outubro de 2010 20:17
  • Emanuel's example is basically a variation of what I posted. Either one should work for you, Elsamelghi.
    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    sábado, 23 de outubro de 2010 20:53
  •  

     

    Thanks to you all ,the issue i been resloved ,my project is now working fine,both your idea is working ,i post my simple again for other people who have this issue

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Windows.Forms;

     

    namespace WindowsFormsApplication2

    {

        class exmaple

        {

            WindowsFormsApplication2.Form1  formed;

            string text;

     

            public exmaple(Form1 formed)

            {

                this.formed = formed;

            }

     

            public void testexmaple()

            {

                text = "meassge here ";

     

                formed.test(text);

            }

        }

     

     

    }

     

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Linq;

    using System.Text;

    using System.Windows.Forms;

     

    namespace WindowsFormsApplication2

    {

        public partial class Form1 : Form

        {

            private exmaple exmapled;

     

            public Form1()

            {

                InitializeComponent();

            }

     

            private void Form1_Load(object sender, EventArgs e)

            {

                exmaple exmapled = new exmaple(this);

            }

     

            public void test(string text)

            {

                textBox1.Text = text;

            }

     

            private void button1_Click(object sender, EventArgs e)

            {

                exmapled = new exmaple(this);

                exmapled.testexmaple();

            }

        }

    }

     

     

     

    • Marcado como Resposta sager79 terça-feira, 26 de outubro de 2010 12:33
    sábado, 23 de outubro de 2010 21:19