Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2007
    Posts
    3

    Accessing form objects from another class file

    Hello Everyone

    I am fairly new to C#. I have setup an app with a form.cs file and a programMessage.cs. file.

    On the form is a field called programMessagesBox that I would like to be able to access from the programMessage.cs file. Unfortunately I am getting an error message that says the field is not in context.

    From what I can tell, I have both class files using the same namespace.

    What am I missing here? Any help would be greatly appreciated.

    form.cs
    Code:
    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;
    using System.IO;
    
    
    namespace PullOCR
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
        ...
    programMessage.cs
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    
    namespace PullOCR
    {
        public static class programMessage
        {        
            public static void writeMessage(string message)
            {
                if (programMessagesBox.Text == "")
                {
                    programMessagesBox.Text = message;
                }
                else
                {
                    programMessagesBox.AppendText("\n" + message);
                }
            }
    
        }
    }

  2. #2
    Join Date
    Mar 2005
    Location
    Sydney, Australia
    Posts
    1,215
    is programMessagesBox.Text a public field?

    If so, be sure to use naming conventions and change it to have a capital P in Program

  3. #3
    Join Date
    Aug 2007
    Posts
    3
    Hi Jacquesy.

    Thank you for your help. I was able to get some additional help on this and this is what I came up with. I appears I needed to pass in the method to the control.

    In the call for the writeMessage, I updated it to this

    form1.cs
    Code:
    programMessage.writeMessage(edocInputDirbox,programMessagesBox, 1);
    programMessage.cs
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace PullOCR
    {
        static class programMessage
        {
            public static void writeMessage(System.Windows.Forms.TextBox tbox,
                                            System.Windows.Forms.RichTextBox outbox,int messageID)
            {
                
    
                if (outbox.Text == "")
                {
                    outbox.Text = String.Format(System.Configuration.ConfigurationSettings.AppSettings["string1"], tbox.Text);
                }
                else
                {
                    outbox.Text = outbox.Text + "\n" + String.Format(System.Configuration.ConfigurationSettings.AppSettings["string1"], tbox.Text);
                }
                
    
            }
    
        }
    }
    Thanks so much for the help.

Posting Permissions

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