VB.NET中UserControl使用BunifuSnackbar报类型转换错误求助
解决VB.NET中UserControl使用BunifuSnackbar的类型转换错误
错误原因
你遇到的Value of type 'PgStockin' cannot be converted to 'Form'错误,是因为BunifuSnackbar2.Show方法的第一个参数要求传入Form类型的实例,但你的PgStockin继承自UserControl,代码里的Me指代的是UserControl实例,类型不匹配导致转换失败。而在Form中使用时,Me本身就是Form实例,自然符合参数要求。
解决方案
只需要将传入的Me替换为当前UserControl所属的顶级父窗体即可,最可靠的方式是使用FindForm()方法:
Partial Public Class PgStockin Inherits UserControl BunifuSnackbar2.Show(Me.FindForm(), "Please delete item detail first ", Bunifu.UI.WinForms.BunifuSnackbar.MessageTypes.Error, 5000, "", Bunifu.UI.WinForms.BunifuSnackbar.Positions.MiddleCenter, Bunifu.UI.WinForms.BunifuSnackbar.Hosts.FormOwner) End Class
FindForm()方法会自动定位到当前UserControl所在的顶级Form容器,完全满足BunifuSnackbar.Show方法的参数类型要求。如果你的控件嵌套在多层容器中,这个方法也能准确找到最终的父窗体,比手动通过Parent属性逐层查找更稳定。
内容的提问来源于stack exchange,提问作者user22579796
相关产品推荐
相关产品推荐

