问题描述
我在文本框内的textchanged事件中有一个漫长的过程,我想使其更轻松,更轻便。 因此,我正在尝试检查剪贴板中是否包含剪贴板,并且如果它尊重某些参数,它可以继续进行下去。 我基本上想创建一个逐步的东西。 因此,我正在尝试清除代码:
在主文本框的文本更改事件下
If (My.Settings.Dfolder = "") Then
Label2.Text = "Download folder is missing "
Else
If Clipboard.GetText.Contains("youtube") = False Then
Label2.Text = "It doesn't contain a youtube link "
Else
If TextBox1.Text = Clipboard.GetText Then
Label2.Text = "You are trying to use the same link"
Else
TextBox1.Text = Clipboard.GetText
End If
End If
End If
因此,对我来说,它可以完美地工作,因为它使它一步步地进行,并且如果不遵守第一个if语句,它就不会继续进行。
当我想前进时,问题就来了。
在“ TextBox1.Text = Clipboard.GetText”之后,程序必须执行以下操作:
-
如果文件不存在,则webbrowser(幸运的是唯一的一个..)将开始导航至页面,在该页面中,文档完成事件将获得“下载链接”(我正在从已经存在的在线服务),并且它正在使用该链接通过Google Api创建QR码,这将使我获得二维图像。
-
然后我通过POST请求将标题名称添加到在线记事本中(我知道它不是专业的,但是我喜欢它,因为它可以正常工作,并且比管理(支付数据库)还容易)
我已经编写了所有这些代码,但是我几乎在一个文本框textchanged事件中使用了所有代码,这完全是一团糟。例如,如果连接有点慢或错过一秒钟的信号,则即使一切正常且连接良好,也会使所有此过程变得混乱。 另外,下载是通过从Webbrowser中提取的链接进行的,但是我必须执行“哦,我的天哪”程序。我正在使用下载链接填充辅助文本框,并且textchanged事件正在开始异步下载。 这基本上是一个非常脆弱的过程,因为如果某些事情无法正常工作,或者Web浏览器没有在合理的时间内加载,则会造成延迟,并且这里的延迟意味着无法进行下一步。例如,一段时间后,我意识到有时文件名不会更改,因此将歌曲X下载为SongY,因为标题提取花费的时间比“正常”时间还多。 我的问题是:如何逐步组织一切? 一切都应该从逻辑开始。如果Web浏览器要花费更多的负载,而不是所有进程都必须“意识到”这种情况。”就像Webbrowser需要1个小时来获取下载链接,然后下一步准备就绪并可以正常工作。我将其可视化作为一个7×100米的继电器。 我写的代码是:
'Under main textBox textchanged event
Try
'GETTING FILENAME FROM YOUTUBE LINK
Dim x As New WebClient()
x.Encoding = Encoding.UTF8
Dim source As String = x.DownloadString(TextBox1.Text.Replace(" - YouTube",""))
Dim title As String = Regex.Match(source,"\<title\b[^>]*\>\s*(?<Title>[\s\S]*?)\</title\>",RegexOptions.IgnoreCase).Groups("Title").Value
Dim a As String = title.Replace(" - YouTube","")
Dim webdecode As String = WebUtility.HtmlDecode(a)
Label13.Text = String.Join("-",webdecode.Split(IO.Path.GetInvalidFileNameChars))
Label2.Text = "Getting title "
Label2.ForeColor = Color.ForestGreen
Dim folderPatha = TextBox4.Text
Dim filePatha = Directory.GetFiles(folderPatha,Label13.Text + ".mp3",SearchOption.AllDirectories).FirstOrDefault()
If filePatha Is nothing Then
Webbrowser1.Navigate("Download website" + TextBox1.Text.Replace("https://www.youtube.com/watch?v=",""))
If RichTextBox1.Text.Contains(Label13.Text) = False Then
'ADD TITLE TO RTB1
RichTextBox1.AppendText(vbLf + Label13.Text + Environment.NewLine)
'extract the content of betterlistBox and update the notepad online with this last title
Dim hey As String = (String.Join(vbLf,BetterListBox1.Items.Cast(Of String)) + vbLf + "Inga - " + Label13.Text + vbLf)
Dim ok As Boolean = SaveData(hey,"011hk762","63958th7q")
End If
Else
Dim risposta As String
risposta = MsgBox("File existing in " + vbLf + textBox4.text + "\" + Label13.Text + ".mp3" + vbLf + "Would you like to open the folder?",vbYesNo)
If risposta = vbYes Then
Process.Start("explorer.exe","/select," & filePatha)
End If
End If
Else
End If
Catch ex As Exception
End Try
从(在线记事本)更新BetterListBox的计时器为:
Private Async Sub Timer1_Tick(sender As Object,e As EventArgs) Handles Timer1.Tick
Dim task1 As Task = Task.Run(Sub()
Try
Dim res As String
Dim client As WebClient = New WebClient()
client.Encoding = Encoding.UTF8
client.Headers.Add(HttpRequestHeader.UserAgent,"")
Dim stream = client.OpenRead("notepadonlinelink")
Dim sr As StreamReader = New StreamReader(stream)
res = (sr.ReadToEnd)
'FILTER HTML TAGS
Dim sourcestring As String = res
Dim matchpattern As String = "<(?:[^>=]|='[^']*'|=""[^""]*""|=[^'""][^\s>]*)*>"
Dim replacementstring As String = ""
res = ((Regex.Replace(sourcestring,matchpattern,replacementstring)))
Dim clientdecode As String = WebUtility.HtmlDecode(res)
Label15.Text = clientdecode
Catch ex As Exception
End Try
Try
Me.Invoke(Sub()
Dim visibleItems As Integer = BetterListBox1.ClientSize.Height / BetterListBox1.ItemHeight
BetterListBox1.Items.Clear()
' I add the value of notepad online in betterlistBox
BetterListBox1.Items.AddRange(Split(Label15.Text,vbLf))
BetterListBox1.TopIndex = Math.Max(BetterListBox1.Items.Count - visibleItems + 1,0)
End Sub)
Catch ex As Exception
End Try
End Sub)
Await task1
End Sub
当网络浏览器文档完成事件发生时:
Try
'extracting download link
Dim collection As HtmlElementCollection = Webbrowser1.Document.All
For Each element As HtmlElement In collection
If element.TagName = "A" Then
Dim link As String = element.GetAttribute("HREF")
If link.Length > 70 Then
link = link.ToString.Replace(" - YouTube","")
TextBox3.Text = link
End If
End If
Next
'=====================================================================
'and I'm gonna use it to create a qrcode
Dim sitoGoogleQrCode As String = "http://chart.googleapis.com/chart?chs={WIDTH}x{HEIGHT}&cht=qr&chl={TESTO}"
sitoGoogleQrCode = sitoGoogleQrCode.Replace("{WIDTH}",PictureBox1.Width.ToString()).Replace("{HEIGHT}",PictureBox1.Height.ToString()).Replace("{TESTO}",WebUtility.UrlEncode(TextBox3.Text))
Dim client As WebClient = New WebClient()
Dim bytes As Byte() = client.DownloadData(sitoGoogleQrCode)
client.dispose()
Dim memStream As MemoryStream = New MemoryStream(bytes)
Dim bmpQrCode As Bitmap = New Bitmap(memStream)
PictureBox1.Image = bmpQrCode
Catch ex As Exception
End Try
在另一个文本框的textchanged事件中:
Dim Path As String = TextBox4.Text
Dim filepath As String = (Path + "\" + Label13.Text + ".mp3")
mclient.Encoding = Encoding.UTF8
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
mclient.Headers.Add(HttpRequestHeader.UserAgent,"")
mclient.DownloadFileAsync(New Uri(TextBox3.Text),filepath)
感谢您一直阅读到这里。我想说的是,由于无法访问该对象,我多次使变量声明混乱。代码必须非常干净,但是目前,我如何组织所有这些? 我需要拆分这些东西并将其添加到类中并在需要时调用它们吗?
非常感谢
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)