提问人:MiningChamber 提问时间:2/12/2023 最后编辑:MiningChamber 更新时间:2/12/2023 访问量:83
如何将标签文本从函数类更新为 Form1 vb.net
How to update label text from functions class to Form1 vb.net
问:
我正在尝试重写我的代码,我正在学习如何用不同的方式实现代码,例如......Form1
classes
Functions
Properties
现在我已经创建了一个视频的 YouTube 标题,我知道我可以使用 API 顺便说一句,但由于我正在测试很多,我的 API 限制将立即关闭。Sub
问题是,即使我在 Functions 中声明或设置了 for 标签,我仍然收到错误:Public Shared Property Frm As New Form1
invoke sub
System.NullReferenceException: '对象引用未设置为 对象的实例。
这是我的代码:RegularExpressions.vb
Imports System.Text.RegularExpressions
Imports Leaf.xNet
Public Class RegularExpressions
Public Shared Property Frm As New Form1
Friend Shared Mainform As Form1
Public Shared Property txtTitle As Label
Public Shared Sub getValues_Offline_Title()
Using req As New HttpRequest()
Dim Offline_Title As String
Dim url As String
Dim _videoID As String = Functions.VideoURL_link.ToString
req.AddHeader("Host", "www.youtube.com")
req.AddHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36")
req.AddHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8")
req.AddHeader("Accept-Language", "en-US,en;q=0.9,cs;q=0.8,de;q=0.7,fr;q=0.6,hu;q=0.5")
req.AddHeader("Accept-Encoding", "gzip, deflate")
req.AddHeader("Referrer", $"https://www.youtube.com/watch?v={_videoID}")
Dim strData As String = req.[Get](String.Concat("https://www.youtube.com/watch?v=", _videoID), Nothing).ToString()
Dim url2 = strData.ToString
url = strData.ToString().Split({"twoColumnWatchNextResults"":"}, StringSplitOptions.None)(1).Split("]"c)(0)
Offline_Title = url.Split(New String() {"text"":"""}, StringSplitOptions.None)(1).Split(""""c)(0)
'MessageBox.Show(Offline_Title)
Frm.Invoke(Sub() txtTitle.Text = Offline_Title.ToString)
'Mainform.lbl_Views_sent.Text = Offline_Title.ToString
End Using
End Sub
End Class
此方法在 Form1 加载时调用,并且我有 2 个单选按钮。 如果选中 Radiobutton1,则它应该显示离线视频标题,否则选中 radiobutton2,然后显示实时视频标题。
Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
If RadioButton1.Checked Then
RegularExpressions.getValues_Offline_Title()
Else
RegularExpressions.getValues_Live_Title()
End If
End Sub
答:
0赞
tuyau2poil
2/12/2023
#1
必须基于 form1 创建一个名为 Frm 的窗体类,并将 Form1 类的现有实例分配给该类。 (如果该解释不完美,请感谢评论)
这工作原理:
Public Class RegularExpressions
Public Shared Sub getValues_Offline_Title()
Dim Frm As Form1 = Form1 'create and assign
Using req As New HttpRequest()
Dim Offline_Title As String
Dim url As String
Dim _videoID As String = Functions.VideoURL_link.ToString
req.AddHeader("Host", "www.youtube.com")
req.AddHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36")
req.AddHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8")
req.AddHeader("Accept-Language", "en-US,en;q=0.9,cs;q=0.8,de;q=0.7,fr;q=0.6,hu;q=0.5")
req.AddHeader("Accept-Encoding", "gzip, deflate")
req.AddHeader("Referrer", $"https://www.youtube.com/watch?v={_videoID}")
Dim strData As String = req.[Get](String.Concat("https://www.youtube.com/watch?v=", _videoID), Nothing).ToString()
Dim url2 = strData.ToString
url = strData.ToString().Split({"twoColumnWatchNextResults"":"}, StringSplitOptions.None)(1).Split("]"c)(0)
Offline_Title = url.Split(New String() {"text"":"""}, StringSplitOptions.None)(1).Split(""""c)(0)
'MessageBox.Show(Offline_Title)
Frm.txtTitle.Text = Offline_Title.ToString
'Mainform.lbl_Views_sent.Text = Offline_Title.ToString
End Using
End Sub
End Class
评论
0赞
MiningChamber
2/12/2023
感谢您的帮助,但对我来说,调用 Form1.txtTitle 不起作用,这就是我尝试调用的原因,但最终我制作了函数而不是 Sub 并从 Form1 调用 txtTitle 以返回函数字符串
0赞
tuyau2poil
2/13/2023
您的解决方案甚至更好:我可能过于关注表单声明......
0赞
MiningChamber
2/12/2023
#2
因为我的代码一团糟,所以我选择重写它,我知道我会很挣扎,但与此同时,我喜欢提高我的技能并学习更好的编码。
所以解决方案是忘记 in 并从正则表达式中制作一个所需的标题,我只需要调用Sub
class
Function
Return
string
Form1
txtTitle.Text = MyFunction.Function.ToString
评论
Label