如何在Form2中使用Form1的文本框数据 [英] How to use textbox data of form1 in form2

查看:268
本文介绍了如何在Form2中使用Form1的文本框数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试在form2中使用form1的文本框数据(文件名)

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




在上面的代码中,CANTool是form1,我正在尝试在form2中使用form1的文本框数据(CANTool.txtDataLogFile.Text).

我收到类似下面的错误


错误1"SCANLA.CANTool.txtDataLogFile"由于其保护级别而无法访问
错误2非静态字段,方法或属性``SCANLA.CANTool.txtDataLogFile''需要对象引用

我该如何解决这些错误


谢谢
John

解决方案

您正在使用的东西不是类(对象)的实例,而是一种类型.

例如:

  class  MyClass {
    内部 静态 字符串 SomeStaticProperty {获取; }
    内部  int  SomeInstanceProperty { get ; }
}

// 您可以做到
字符串 text = MyClass.SomeStaticProperty;
// 
MyClass someInstance =  MyClass();
 int   = someInstance.SomeInstanceProperty;
// 但不是
 int   = MyClass.SomeInstanceProperty; // 无法编译 



关于字段也是如此.您需要依靠实例和类型的静态成员.您不应该使用反复试验的方法"来写作,而需要理解所写的每一行.

对于无法访问的成员,请学习访问权限和访问修饰符:
http://msdn.microsoft.com/en-us/library/ms173121.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/wxh6fsc7.aspx [ ^ ].

-SA


我建​​议您^ ].
之后,您可以继续 [ ^ ].


编辑#1 ...

假设Form1创建了Form2的实例,并且Form2的实例需要访问Form1的实例上的TextBox:

在Form2中:

创建类型为''TextBox:

  public  TextBox tbFileName { get ; 设置; } // 在Form1中,您可能不需要'get  

:

private Form2 f2 = new Form2();

您当您需要在Form2中访问Form1的文本框 :

private void Form1_Load(object sender, EventArgs e)
{
     f2.tbFileName = nameOfYourTextBox;
}

现在="c#"> 字符串 currentTextInForm1 = tbFileName.Text;


Hi,

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

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

解决方案

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

For example:

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


I''d recommend you to start from scratch[^].
Afterwards you can move on to this[^].


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:

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:

string currentTextInForm1 = tbFileName.Text;


这篇关于如何在Form2中使用Form1的文本框数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆