13 Replies - 33694 Views - Last Post: 28 July 2008 - 12:57 PM Rate Topic: -----

#1 Dumpen  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 21-March 08

Access form class textBox from other class

Posted 21 July 2008 - 06:47 AM

Hello.

Letís say I have class a and b.

Class a is a part of my form and looks like this: (Just like a regular form class)
public partial class a : Form

On the form there is a textBox and I want to access that textBoxs properties (like textBox.text, textBox.Width) and change the properties from class b.

When I am on class a I can access the textBox since it is a part of the form, but I canít seem to find a good solution when I am in class b

I tryed b classB = new b();

But did not seem to do much.

Any advice?

Is This A Good Question/Topic? 0
  • +

Replies To: Access form class textBox from other class

#2 allensmith  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 11
  • Joined: 04-June 08

Re: Access form class textBox from other class

Posted 21 July 2008 - 07:00 AM

Hello,

First of all declare a variable in Form2 of type Form1.

Form1 _frm;

Now define a function in Form2 which takes an argument of type form1.

public void ShowForm(Form1 frm)
{

_frm = frm;

this.Show();

}



Now in Form1 we will call Form2 in the following manner:

Form2 frm = new Form2();
frm.ShowForm(this);

What this does is that it passes the reference of the Form1 to the Form2 object.

Now in form2, any where we can acess the Form1 textbox using

_frm.TextBox.Text= "This is from Form2";

I hope this helps.

Regards,

Allen
Was This Post Helpful? 1
  • +
  • -

#3 Dumpen  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 21-March 08

Re: Access form class textBox from other class

Posted 21 July 2008 - 07:24 AM

Well its not two forms, but one form (with the public partial class a : Form class) and a public class b

This post has been edited by Dumpen: 21 July 2008 - 07:54 AM

Was This Post Helpful? 0
  • +
  • -

#4 zakary  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 427
  • Joined: 15-February 05

Re: Access form class textBox from other class

Posted 21 July 2008 - 07:35 AM

why dont you use properties for this like

public string TextValue
{
    set
    {
          textbox1.Text = value;
    }
}

public int TextWidth
{
    set
    {
          textbox1.Width = value;
    }
}


This post has been edited by zakary: 21 July 2008 - 07:36 AM

Was This Post Helpful? 1
  • +
  • -

#5 Dumpen  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 21-March 08

Re: Access form class textBox from other class

Posted 21 July 2008 - 07:55 AM

View Postzakary, on 21 Jul, 2008 - 07:35 AM, said:

why dont you use properties for this like

public string TextValue
{
    set
    {
          textbox1.Text = value;
    }
}

public int TextWidth
{
    set
    {
          textbox1.Width = value;
    }
}



Hmm I dont understand how I should use that :huh:
Was This Post Helpful? 0
  • +
  • -

#6 zakary  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 427
  • Joined: 15-February 05

Re: Access form class textBox from other class

Posted 21 July 2008 - 08:31 AM

put the above code in class_A then from class_B you
class_A classa = new class_A();

classa.TextValue = "Some Value";



and the same for width


classa.TextWidth = 15;


This post has been edited by zakary: 21 July 2008 - 12:38 PM

Was This Post Helpful? 0
  • +
  • -

#7 Dumpen  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 21-March 08

Re: Access form class textBox from other class

Posted 22 July 2008 - 01:44 AM

I cant seem to access the class a from class b

If I use:
public class classB
{
Form1 form1 = new Form1();

}

the variable form1 doesnt excist in classB even though I just created it

I just tryed with two forms instet of one form and one class and there I can use the variable, but textValue doesnt work at form2


If I use textValue at form1 it works, but not outside :S
Was This Post Helpful? 0
  • +
  • -

#8 zakary  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 427
  • Joined: 15-February 05

Re: Access form class textBox from other class

Posted 22 July 2008 - 05:13 AM

what are the namespaces for both classes they should be the same or you have to use the namespace with the Form1. Can you post both classes so we can help you better.
Was This Post Helpful? 0
  • +
  • -

#9 Dumpen  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 21-March 08

Re: Access form class textBox from other class

Posted 26 July 2008 - 03:29 AM

View Postzakary, on 22 Jul, 2008 - 05:13 AM, said:

what are the namespaces for both classes they should be the same or you have to use the namespace with the Form1. Can you post both classes so we can help you better.


Here it is:
http://peecee.dk/upl...download/124785

And the code in it:
using System.Windows.Forms;

namespace testApp
{
	public partial class a : Form
	{

		public a()
		{
			InitializeComponent();
		}

	}

	class b
	{
		a classA = new a();
		// classA doesnt excist even though I just created it
	}

}

Was This Post Helpful? 0
  • +
  • -

#10 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Access form class textBox from other class

Posted 27 July 2008 - 08:12 AM

this works just fine for me.

namespace WindowsApplication3
{
	public partial class a : Form
	{
		public a()
		{
			InitializeComponent();
		}
	}

	class b
	{
		a frmA = new a();
	}
}


Was This Post Helpful? 0
  • +
  • -

#11 Dumpen  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 21-March 08

Re: Access form class textBox from other class

Posted 27 July 2008 - 10:27 AM

It works for me to

But does frmA excist at you?

I mean can you write frm and then frmA comes up as a option?
Was This Post Helpful? 0
  • +
  • -

#12 zakary  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 427
  • Joined: 15-February 05

Re: Access form class textBox from other class

Posted 28 July 2008 - 06:14 AM

as i posted to you before, you should use properties to set the values. Here is my code with your code. Make sure you add the Textbox

using System.Windows.Forms;

namespace testApp
{
    public partial class a : Form
    {

        public a()
        {
            InitializeComponent();
        }
        public string TextValue  
        {  
            set  
            {  
                textbox1.Text = value;  
            }  
        }  
   
        public int TextWidth  
       {  
          set  
          {  
               textbox1.Width = value;  
          }  
       }

    }

    class b
    {
        a classA = new a();
        
        classA.TextValue = "Hello";
        classA.TextWidth = 20;
    }

}


This post has been edited by zakary: 28 July 2008 - 06:14 AM

Was This Post Helpful? 0
  • +
  • -

#13 Dumpen  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 21-March 08

Re: Access form class textBox from other class

Posted 28 July 2008 - 12:15 PM

Did you try that code?

Please do

I just did and gave two errors: (Probably because classA doesnt excist ;) )
Error 1 Invalid token '=' in class, struct, or interface member declaration C:\Documents and Settings\Administrator\Dokumenter\Visual Studio 2008\Projects\testApp\testApp\a.cs 35 26 testApp
Error 2 Invalid token '=' in class, struct, or interface member declaration C:\Documents and Settings\Administrator\Dokumenter\Visual Studio 2008\Projects\testApp\testApp\a.cs 36 26 testApp
Was This Post Helpful? 0
  • +
  • -

#14 zakary  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 427
  • Joined: 15-February 05

Re: Access form class textBox from other class

Posted 28 July 2008 - 12:57 PM

Sorry I for got the constructor for class b
using System.Windows.Forms;  
   
namespace testApp  
{  
    public partial class a : Form
    {
        public a()
        {
            InitializeComponent();
        }

        public string TextValue    
        {
            set    
            {    
                this.textBox1.Text = value;    
            }    
        }    
      
        public int TextWidth    
        {    
           set    
           {
               this.textBox1.Width = value;    
           }    
        }  
    } 
 
    class b  
    {  
        a classA = new a();  
        
        public b()
        {
            classA.TextValue = "Hello";
            classA.TextWidth = 20; 
        }
    } 
}


Was This Post Helpful? 1

Page 1 of 1