Click here to Skip to main content
15,886,780 members
Please Sign up or sign in to vote.
3.17/5 (4 votes)
See more:
Hi,

I am trying to use textbox data(filename) of form1 in form2

C#
fileContents = SCANLA.Run.ReadingLogFile(CANTool.txtDataLogFile.Text);



In the above code CANTool is form1 and I am trying to use text box data (CANTool.txtDataLogFile.Text)of form1 in form2.


I am getting some error like below


Error 1 'SCANLA.CANTool.txtDataLogFile' is inaccessible due to its protection level
Error 2 An object reference is required for the non-static field, method, or property 'SCANLA.CANTool.txtDataLogFile'

How can I solve these errors


Thanks
John
Posted
Comments
Sergey Alexandrovich Kryukov 20-Jan-14 10:28am    
What do you mean by "TextBox"? Full type name, please. (I guess, System.Windows.Forms.TextBox, but you need to add the tag to your questions, in this case, System.Windows.Forms.)
What's wrong with using the property TextBox.Text (whatever it is)?
—SA
Marco Bertschi 20-Jan-14 10:36am    
I guess he tries to reference a textbox located on form1 from form2, thus the "inaccessible due to its protection level "-error.
2nd, he didn't instatiate form1, thus the Error #2.
Sergey Alexandrovich Kryukov 20-Jan-14 10:38am    
Of course. I tried to answer, please see.
—SA

You are using something which is not the instance of the class (object), but is a type.

For example:
C#
class MyClass {
    internal static string SomeStaticProperty { get; }
    internal int SomeInstanceProperty { get; }
}

//you can do
string text = MyClass.SomeStaticProperty;
//or
MyClass someInstance = new MyClass();
int value = someInstance.SomeInstanceProperty;
//but NOT
int value = MyClass.SomeInstanceProperty; // won't compile


Same thing about fields. You need to lean about instance and static members of types. You should not use trial-and-error "method" of writing, but need to understand every line you write.

As to the inaccessible members, learn access and access modifiers:
http://msdn.microsoft.com/en-us/library/ms173121.aspx[^],
http://msdn.microsoft.com/en-us/library/wxh6fsc7.aspx[^].

—SA
 
Share this answer
 
I'd recommend you to start from scratch[^].
Afterwards you can move on to this[^].
 
Share this answer
 
edit #1 ...

Assuming that Form1 creates an instance of Form2, and that the instance of Form2 needs to access a TextBox on the instance of Form1:

In Form2:

Create a Public Property of Type 'TextBox:
C#
public TextBox tbFileName { get; set; } // you may be not need the 'get
In Form1:
private Form2 f2 = new Form2();
You need to inject the reference to the Textbox on Form1 into the Property of Type Textbox in the instance of Form2:
private void Form1_Load(object sender, EventArgs e)
{
     f2.tbFileName = nameOfYourTextBox;
}
Now, when you need to access Form1's TextBox in Form2:
C#
string currentTextInForm1 = tbFileName.Text;
 
Share this answer
 
v2
Comments
Member 10408451 20-Jan-14 13:09pm    
I am trying like below
CANTool frm1 = new CANTool();

string[] fileContents = Run.ReadingLogFile(frm1.txtDataLogFile.Text);
I am getting error like below
A field initializer cannot reference the non-static field, method, or property 'SCANLA.GatewayMessages.frm1'
BillWoodruff 20-Jan-14 16:26pm    
Okay, I may have mis-read your question; I assumed that Form1 needed to use a TextBox on Form2. I'll revise the code based on Form2 needing access to the TextBox on Form1.

What we need to know here is which is the main form: Form1, or Form2 ? Does Form1 create Form2, or, are both Forms created by another Form ?

I've posted a revised reply taking into account you want Form2 to access the TextBox on Form1.
Try this:

If you create a new Form2:

C#
Form2 test = new Form2(this);

In the constructor of Form2 you write:
C#
public Form2 (Form1 caller)
{
     InitializeComponent();
     Form1 name;                 //Not object but class;
     name = caller;             //You can choose any name
}

And now you can use the Data
C#
TextBox anyname = (Textbox)name.Controls["NameofTextboxinForm1"];
anyname.Text = "";  //or label1.Text = anyname.Text;


I hope i could help
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900