Vb.net GUI没有正常移动
我正在使用Binance API制作一个显示比特币价格的小部件 我不使用Json格式,因为我只需要解析一个字符串,尽管我知道你们中的许多人会说使用Json。无论如何,我想让软件尽可能简单,但有一个小问题。 我正在使用webclient下载源代码,并使用计时器进行更新。 我认为每次创建新的webclient时我都犯了一个错误,因为当我想要移动表单时,即使表单没有冻结,也没有正确地进行mooving。 我使用的代码是:Vb.net GUI没有正常移动,vb.net,webclient,Vb.net,Webclient,我正在使用Binance API制作一个显示比特币价格的小部件 我不使用Json格式,因为我只需要解析一个字符串,尽管我知道你们中的许多人会说使用Json。无论如何,我想让软件尽可能简单,但有一个小问题。 我正在使用webclient下载源代码,并使用计时器进行更新。 我认为每次创建新的webclient时我都犯了一个错误,因为当我想要移动表单时,即使表单没有冻结,也没有正确地进行mooving。 我使用的代码是: Private Sub webclientbtc() Dim wc
Private Sub webclientbtc()
Dim wc As New Net.WebClient
Dim WBTC As IO.Stream = Nothing
wc.Encoding = Encoding.UTF8
WBTC = wc.OpenRead("https://api.binance.com/api/v1/ticker/24hr?symbol=BTCEUR")
Dim btc As String
Using rd As New IO.StreamReader(WBTC)
btc = rd.ReadToEnd
End Using
'---------BTC PRICE---------'
Dim textBefore As String = """lastPrice"":"""
Dim textAfter As String = ""","
Dim startPosition As Integer = btc.IndexOf(textBefore)
startPosition += textBefore.Length
Dim endPosition As Integer = btc.IndexOf(textAfter, startPosition)
Dim textFound As String = btc.Substring(startPosition, endPosition - startPosition)
Dim dNumber As Double = Val(textFound.ToString)
Label1.Text = dNumber.ToString("n2")
'-------------------------------------'
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
webclientbtc()
End Sub
计时器的间隔是1000毫秒,这是伟大的,让我更新。
关于如何避免每次更新时创建新的webclient,有什么想法吗?
感谢简化,并使用点击:
Private wc as New WebClient()
Private Async Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim s = Await wc.DownloadStringTaskAsync("https://api.binance.com/api/v1/ticker/24hr?symbol=BTCEUR")
Dim d = JsonConvert.DeserializeObject(Of Dictionary(Of String, String))(s)
Label1.Text = d("lastPrice")
End Sub
您需要引用newtonsoft json包并导入它,以及导入system.collections.generic如果太好,您可以使用正则表达式避免使用json反序列化程序:
Imports System.Net
Imports System.Text.RegularExpressions
Public Class Form1
Dim tim As New Timer()
Private Async Sub UpdateBtc(sender As Object, e As EventArgs)
' temporarily disable the timer in case the web request takes a long time
tim.Enabled = False
' using New Uri() makes sure it is a proper URI:
Dim url = New Uri("https://api.binance.com/api/v1/ticker/24hr?symbol=BTCEUR")
Dim rawJson As String
Using wb As New WebClient()
rawJson = Await wb.DownloadStringTaskAsync(url)
End Using
Dim re = New Regex("""lastPrice"":\s*""([0-9.-]+)""")
Dim lastPrice = re.Match(rawJson)?.Groups(1)?.Value
Dim p As Decimal
lblLastPrice.Text = If(Decimal.TryParse(lastPrice, p), p.ToString("N2"), "Fetch error.")
tim.Enabled = True
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
UpdateBtc(Nothing, EventArgs.Empty)
tim.Interval = 3000
AddHandler tim.Tick, AddressOf UpdateBtc
tim.Start()
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
If tim IsNot Nothing Then
tim.Stop()
RemoveHandler tim.Tick, AddressOf UpdateBtc
tim.Dispose()
End If
End Sub
End Class
- 无需重新使用WebClient,创建它并不占用时间
- 我更喜欢自己实例化计时器:不需要这样做
- 最好对控件使用描述性名称:“Label1”不会告诉您任何信息
WebClient方法中使用Using
块。结束使用
处理。Tick
事件不包含对WebClient
的引用,因此无法在那里进行处理。您好,Caius,谢谢,但正如我所说,我希望尽可能简单,现在不想使用Json。感谢这并不难:在最新的VS2019中,粘贴代码,指向JsonConvert下的红色摇摆线,单击灯泡并选择菜单底部的“Install Package Newtonsoft…”。结束。在旧版VS中,右键单击项目,选择“管理Nuget软件包”,单击浏览,键入“Newtonsoft”,单击下载量接近10亿次的条目,单击安装,然后粘贴代码。如果你得到任何其他红色摇摆线,你指向他们,让VB为你做导入。这是更难(更糟糕的是,需要更长的时间)做你的错综复杂的字符串切分方法!嗨,凯斯,我知道你完全正确。Json是最快的方法,也是唯一的“逻辑”解决方案。无论如何,因为这对我来说是一个简单的小部件,所以我正在做一个简单的方法,因为我想解析高价和低价,更改和更改百分比数据。出于这个原因,我的字符串方法正在发挥作用,我对此很有信心。我只是认为问题在于我没有处理webclient。我只是在做简单的事情-不。关于你的字符串切割方式,没有什么比说“嘿,世界头号JSON解析器,帮我解析这个,谢谢”更简单的了。也因为我想分析高价和低价,改变和改变百分比数据——所以你承认你会想分析更多的数据。。怎么了,还要剪绳子?当我已经向你展示了路径,你可以再写一行代码,说d(“价格变化”)
或d(“高价”)
然后得到你想要的数据?好啊如果你没有从中得到任何其他东西,至少制作一个webclient并使用TAPPerhaps还值得一提的是,你正在避免使用JSON解析,而是有效地编写自己的,但你不会通过编写自己的HTTP客户端来手动下载此数据/弹出一个TCP套接字,连接它,手动推送字节,将响应读入缓冲区,将其从字节数组转换为字符串。。因此,您很乐意使用一个高级抽象助手类来完成下载部分,但坚决反对使用另一个高级JSON解析库来理解您下载的内容-这没有任何逻辑意义Hi Andrew,谢谢您的回答。但正如我所说,我不打算使用Json,因为这对我来说是一个简单的工具,Json对我来说仍然不是很清楚。@23rfwefaf这个答案不使用Json。