简体   繁体   English

C# WPF 控制表格form2

[英]C# WPF controls form form2

I created the project for C# WPF (.NET Core 3.1).我为 C# WPF (.NET Core 3.1) 创建了项目。 I added one button and one richtextbox.我添加了一个按钮和一个richtextbox。 I created a new Bypass class file in the project.我在项目中创建了一个新的 Bypass class 文件。 I write code to Bypass class like access from form1 to form2.我编写代码绕过 class,就像从 form1 访问 form2。

BypassClass file code: BypassClass 文件代码:

   public void Start()
    {
        MainWindow Form1 = new MainWindow();
        RichTextBox LogBox = Form1.LogBox;

        LogBox.AppendText("Hello \rf");

    }

button code:按钮代码:

Bypass BypassClass = new Bypass();
BypassClass.Start();

I have tried this code.我试过这段代码。 Button1 I clicked the button. Button1 我点击了按钮。 RichTextBox 'Hello' did not come. RichTextBox 'Hello' 没有出现。 VB.NET encoding works using: VB.NET 编码使用:

MyProject.Forms.Form1.Richtextbox1.AppendText("Hello");

enter image description here在此处输入图像描述

I want coding like this picture.我想要像这张图片一样编码。 Waiting for help Thanks for reviewing the subject.等待帮助 感谢您审阅该主题。

You are creating a new instance of MainWindow in your Start() method.您正在Start()方法中创建MainWindow的新实例。 What you want to do is to get a reference to the window that is currently displayed on the screen:您要做的是获取对当前显示在屏幕上的 window 的引用:

public void Start()
{
    MainWindow Form1 = Application.Current.Windows.OfType<MainWindow>().FirstOrDefault();
    if (Form1 != null)
    {
        RichTextBox LogBox = Form1.LogBox;
        LogBox.AppendText("Hello \rf");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM