Access form class textBox from other class
Page 1 of 113 Replies - 28318 Views - Last Post: 28 July 2008 - 12:57 PM
#1
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?
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?
Replies To: Access form class textBox from other class
#2
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
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
#3
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
#4
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
#5
Re: Access form class textBox from other class
Posted 21 July 2008 - 07:55 AM
#6
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
and the same for width
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
#7
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
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
#8
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.
#9
Re: Access form class textBox from other class
Posted 26 July 2008 - 03:29 AM
zakary, 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
}
}
#10
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();
}
}
#11
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?
But does frmA excist at you?
I mean can you write frm and then frmA comes up as a option?
#12
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
#13
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
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
#14
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;
}
}
}
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|