Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';t更改textBox.text的形式,从其他类-C#-VS 2010_C#_Visual Studio 2010 - Fatal编程技术网

Can';t更改textBox.text的形式,从其他类-C#-VS 2010

Can';t更改textBox.text的形式,从其他类-C#-VS 2010,c#,visual-studio-2010,C#,Visual Studio 2010,我有一张表格叫Form1。在它里面,我有一个叫做加密的按钮。当我按下那个按钮时,我调用了另一个类中的方法。我希望该方法更改textBox1.text的值,但使用此代码时不会发生任何事情 一年级 在另一个类中的方法中 第一个文本框中没有任何更改。就好像我什么都不按 如果您有任何帮助,我们将不胜感激。在您的其他课程中,您需要的是对单击按钮的表单的引用(其中包含您现有的文本框),而不是新表单 您正在实例化的新表单不是您在单击按钮的屏幕上看到的表单 (我假设您的事件处理程序存在于Form1类中,然后它会

我有一张表格叫Form1。在它里面,我有一个叫做加密的按钮。当我按下那个按钮时,我调用了另一个类中的方法。我希望该方法更改textBox1.text的值,但使用此代码时不会发生任何事情

一年级 在另一个类中的方法中 第一个文本框中没有任何更改。就好像我什么都不按


如果您有任何帮助,我们将不胜感激。

在您的其他课程中,您需要的是对单击按钮的表单的引用(其中包含您现有的文本框),而不是新表单

您正在实例化的新表单不是您在单击按钮的屏幕上看到的表单

(我假设您的事件处理程序存在于Form1类中,然后它会根据需要将信息“转发”到其他类的方法?如果不是……它应该!)

按钮引用可通过
发送方
对象和传递给事件处理程序的
事件参数
获得。通过将
this
关键字传递给其他类的方法,可以传递对当前
Form1
实例的引用。或者,如果对您有用,您可以将
发送方
传递给其他方法,或者只将对特定文本框的显式引用传递给其他方法

例如,要将对表单的引用传递给其他方法:

// Event handler on your form
private void button1_Click(object sender, EventArgs e)
{
    ButtonWasPressedOnForm(this);   
}

// Other method in your other class
public void ButtonWasPressedOnForm(Form formWhereButtonPressed)
{
    // To act on the text box directly:
    TextBox textBoxToUpdate = (TextBox)formWhereButtonPressed.Controls.Find("textBox1");
    textBoxToUpdate.Text = "whatever";

    // Or, using the Form1.txtBox1 property.
    formWhereButtonPressed.txtBox1 = "whatever";  
}
// Event handler on your form
private void button1_Click(object sender, EventArgs e)
{
    ButtonWasPressedOnForm(textBox1);   
}

// Other method in your other class
public void ButtonWasPressedOnForm(TextBox textBoxToUpdate)
{
    textBoxToUpdate.Text = "whatever";
}
// Event handler on your form
private void button1_Click(object sender, EventArgs e)
{
    Button clickedButton = (Button)sender;
    ButtonWasPressedOnForm(clickedButton);  
}

// Other method in your other class
public void ButtonWasPressedOnForm(Button clickedButton)
{
    Form theParentForm = clickedButton.FindForm();

    // To act on the text box directly:
    TextBox textBoxToUpdate = (TextBox)theParentForm.Controls.Find("textBox1");
    textBoxToUpdate.Text = "whatever";

    // Or, To act on the text box, via the form's property:
    theParentForm.txtBox1 = "whatever";
}
例如,要将对显式文本框的引用传递给其他方法:

// Event handler on your form
private void button1_Click(object sender, EventArgs e)
{
    ButtonWasPressedOnForm(this);   
}

// Other method in your other class
public void ButtonWasPressedOnForm(Form formWhereButtonPressed)
{
    // To act on the text box directly:
    TextBox textBoxToUpdate = (TextBox)formWhereButtonPressed.Controls.Find("textBox1");
    textBoxToUpdate.Text = "whatever";

    // Or, using the Form1.txtBox1 property.
    formWhereButtonPressed.txtBox1 = "whatever";  
}
// Event handler on your form
private void button1_Click(object sender, EventArgs e)
{
    ButtonWasPressedOnForm(textBox1);   
}

// Other method in your other class
public void ButtonWasPressedOnForm(TextBox textBoxToUpdate)
{
    textBoxToUpdate.Text = "whatever";
}
// Event handler on your form
private void button1_Click(object sender, EventArgs e)
{
    Button clickedButton = (Button)sender;
    ButtonWasPressedOnForm(clickedButton);  
}

// Other method in your other class
public void ButtonWasPressedOnForm(Button clickedButton)
{
    Form theParentForm = clickedButton.FindForm();

    // To act on the text box directly:
    TextBox textBoxToUpdate = (TextBox)theParentForm.Controls.Find("textBox1");
    textBoxToUpdate.Text = "whatever";

    // Or, To act on the text box, via the form's property:
    theParentForm.txtBox1 = "whatever";
}
例如,要将事件对象传递给其他方法:

// Event handler on your form
private void button1_Click(object sender, EventArgs e)
{
    ButtonWasPressedOnForm(this);   
}

// Other method in your other class
public void ButtonWasPressedOnForm(Form formWhereButtonPressed)
{
    // To act on the text box directly:
    TextBox textBoxToUpdate = (TextBox)formWhereButtonPressed.Controls.Find("textBox1");
    textBoxToUpdate.Text = "whatever";

    // Or, using the Form1.txtBox1 property.
    formWhereButtonPressed.txtBox1 = "whatever";  
}
// Event handler on your form
private void button1_Click(object sender, EventArgs e)
{
    ButtonWasPressedOnForm(textBox1);   
}

// Other method in your other class
public void ButtonWasPressedOnForm(TextBox textBoxToUpdate)
{
    textBoxToUpdate.Text = "whatever";
}
// Event handler on your form
private void button1_Click(object sender, EventArgs e)
{
    Button clickedButton = (Button)sender;
    ButtonWasPressedOnForm(clickedButton);  
}

// Other method in your other class
public void ButtonWasPressedOnForm(Button clickedButton)
{
    Form theParentForm = clickedButton.FindForm();

    // To act on the text box directly:
    TextBox textBoxToUpdate = (TextBox)theParentForm.Controls.Find("textBox1");
    textBoxToUpdate.Text = "whatever";

    // Or, To act on the text box, via the form's property:
    theParentForm.txtBox1 = "whatever";
}
此外,在“其他方法”上粘贴一个断点,以确保此代码甚至被触发。如果没有,请返回事件处理程序,确保该事件正在被触发。如果没有,请检查事件连线


尽管在所有情况下,您都需要注意要更新的控件的保护级别。。。如果要从
Form1
类外部更新它,则需要根据表单与其他类之间的关系将其公开、内部或保护

更好的OO方法是在
Form1
上有一个方法,允许其他类告诉
Form1
为它们更新控件(例如
updateTextBox(string newText)
)。因为它不是面向对象的最佳实践,允许外部对象直接作用于类的成员(因为这需要了解类的内部结构…应该对其进行封装,以便在不破坏类与外部世界之间存在的接口的情况下更改实现)

编辑:
实际上,在重读您的问题时,您已经使用get/set属性封装了文本框。美好的因此,您应该将对表单的引用传递给其他方法,然后通过属性更新表单的文本。已将此方法添加到上述示例中。

此外,Form1类的保护级别是什么?这就是我经常遇到的问题。。。我忘记了,如果类不是公共的,它就不能被它之外的类访问。“Protected”也可以使用,但它取决于两个类之间的关系。

我发现,在这种情况下,将信息从一个表单传递到另一个表单是将信息保存在类文件中,而不是保存在其自身的表单中

范例 表格1:

然后我要做的就是在第二张表格上,在表格上打电话

label1.text = dependents.passtexboxvaluetoclass;

通过这种方式,您可以调用大量信息,因为这些信息都存储在类中,而不是物理表单本身。您也可以通过在类中创建事件来实现这一点,您的表单订阅该事件,并将要更新的数据作为
EventArgs

,这是一种简单的方法。 首先在类中创建一个文本框

class class1
   {
   public static TextBox txt1=new TextBox();
   public static void Hello()
     {
      txt1.Text="Hello";
     }
   }
表单1上有一个按钮(按钮1)和一个文本框(文本框1)。 好的,现在复制所需文本框的地址必须更改 我测试过了,它工作正常

public partial class Form1 : Form
{
 public Form1()
    {
     InitializeComponent();  
    }
 private void button1_Click(object sender, EventArgs e)
     {
      class1.txt1=textBox1;
      class1.Hello();
     }
}

您正在调用的方法的定义是什么?谢谢,伙计!这是有道理的!但当我传递发送方和事件参数时。如何使用它们编辑文本框的文本?你能给我举一个另一门课的代码例子吗?@RonaDona,我很乐意。增加了一些例子。从我脑子里抽出来,所以不能保证语法。。。但是你会发现有几种不同的方法可以剥猫皮;-)根据您在其他类中使用信息的方式,选择对您最有意义的一个。图例!!!我用了第一个。我工作了,在被困了几个小时后,我将进入下一步。再次非常感谢。我真的很感激。@RonaDona Hi再次意识到您已经用属性封装了文本框,因此在示例中添加了一些额外的信息,因为您确实应该使用这种机制。不客气,感谢您接受。但是您的
依亲亲属
被实例化为
customerHistory:Form
的成员,因此您无论如何都无法独立于
customerHistory
访问
依亲亲属。您是否打算将这个新类实例化为
customerHistory
的对等对象?+1这是一个值得提出的观点。默认情况下,
表单
被明确定义为
公共
(至少在VisualStudio生成的代码中是这样)。它通常不同于
es,它的保护级别没有明确声明(由VS自动声明),因此对于未嵌套的类,它是隐式的
内部
,或者如果该类嵌套在另一个类中,它是
私有的
。但是,如果没有显式应用访问修饰符,那么从
表单继承的类将与任何其他类一样工作。