C# this.owner 返回NULl的解决方法

本文介绍了一种解决C#中Form2窗口this.owner返回NULL的方法。通过在Form1中创建并显示Form2时指定owner参数,确保Form2能够正确获取到其父窗体Form1的引用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C# this.owner 返回NULl的解决方法

form1主窗口

form2是子窗体

 

在form1中://打开form2窗口的位置修改

form2 f2 = new form2();

f2.show(this);//一定要加上这个this

 

在form2中://需要修改form1控件的地方

form1 f1 = (form1)this.owner;

f1.textbox1.text = "OK";

public partial class Choose_Wall_And_Frame : Window { Cable_drilling_syste_Main cable_Drilling_Syste_Main_using = null; RevitLinkInstance revitLinkInstance = null; PickLinkInstanceHandler pickLinkInstanceHandler = null; WallHandler wallHandler = null; UIDocument uIDocument = null; ExternalCommandData commandData = null; public Wall wall { get; set; } public Choose_Wall_And_Frame(Cable_drilling_syste_Main cable_Drilling_Syste_Main, ExternalCommandData commandData, PickLinkInstanceHandler pickLinkInstanceHandler,WallHandler wallHandler) { this.pickLinkInstanceHandler = pickLinkInstanceHandler; cable_Drilling_Syste_Main_using = cable_Drilling_Syste_Main; this.wallHandler = wallHandler; this.commandData = commandData; uIDocument = commandData.Application.ActiveUIDocument; this.ContentRendered += OnWindowLoaded; InitializeComponent(); UdText("请选择链接模型"); // 在 InitializeComponent 之后调用 WindowClassFinder windowClassFinder = new WindowClassFinder(); // 获取 Revit 主窗口句柄 IntPtr revitMainWindowHandle = windowClassFinder.GetRevitMainWindowHandle(); // 查找绘图区句柄 IntPtr drawingAreaHandle = windowClassFinder.FindDrawingAreaClass(commandData); if (drawingAreaHandle != IntPtr.Zero) { // 获取绘图区的位置 RECT drawingAreaRect; GetWindowRect(drawingAreaHandle, out drawingAreaRect); // 设置 WPF 窗口的位置为绘图区的左上角 this.Left = drawingAreaRect.Left; this.Top = drawingAreaRect.Top; } else { MessageBox.Show("未找到 Revit 绘图区句柄!"); } // 设置 WPF 窗口的 Owner,使其始终位于 Revit 窗口之上 WindowInteropHelper helper = new WindowInteropHelper(this); helper.Owner = revitMainWindowHandle; } private void OnWindowLoaded(object sender, EventArgs e) { RadioButton defaultRadioButton = this.FindNam
03-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值