如何在运行时在 vb.net 中将标签名称从 1 重命名为 75? [英] how to do rename the label name from 1 to 75 at run time in vb.net?

查看:23
本文介绍了如何在运行时在 vb.net 中将标签名称从 1 重命名为 75?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Public Sub Rename(ByVal labelno As Integer)
    Try
        Dim lbl As New Label
        lbl = Controls("Label" & labelno.ToString())
        lbl.Text = ReceivedFrame.ToString()
    Catch ex As Exception
        MessageBox.Show(ex.Message.ToString())
    End Try

我想将标签文本从 1 更改为 50.未指定标签作为输入.我试过上面的代码.它在其他项目中工作.但在我当前的项目中它不起作用错误显示如下(跨线程操作无效.从线程访问的控制标签1.除了创建它的线程),我只想更改标签文本.label&Label没有工作正常我验证.有没有其他方法可以在 vb.net 中重命名标签?

i want to change Label Text from 1 to 50. Label no given as input. i tried the above code. its working in other project. but in my current project it is not working Error is showing like this (cross Thread operation not valid.control label1 accessed from a thread.other than the thread it was created on)and i want to change the label text only.label&Label no is working properly i verified. is there any other way to rename label in vb.net?

推荐答案

您正试图修改驻留在与您调用 Rename() 的线程不同的线程上的控件的属性.更新控件时,您应该检查是否需要将调用发送到适当的线程,或调用"调用.这应该有效.

You are trying to modify a property of a control which resides on a different thread than the thread from which you call Rename(). When updating controls you should check if it is required to send the call to the appropriate thread, or "invoke" the call. This should work.

方法一:总是调用

Public Sub Rename(ByVal labelno As Integer)
    Try
        Dim lbl As New Label
        lbl = Controls("Label" & labelno.ToString())
        lbl.Invoke(Sub() lbl.Text = ReceivedFrame.ToString())
    Catch ex As Exception
        MessageBox.Show(ex.Message.ToString())
    End Try
End Sub

它将总是调用该调用,当从控件的线程调用 Rename() 时,这是不必要的开销.但它应该始终有效.您也可以通过这种方式仅在需要时调用,以免对 UI 线程进行不必要的调用.

It will always invoke the call, which is unnecessarily costly when Rename() is called from the control's thread. But it should always work. You can also do it this way to only invoke when required as not to make unnecessary invocations to the UI thread.

方法二:只在需要时调用

Method 2: Invoke only if required

Public Sub Rename(ByVal labelno As Integer)
    Try
        Dim lbl As New Label
        lbl = Controls("Label" & labelno.ToString())
        changeLabelText(lbl, ReceivedFrame.ToString())
    Catch ex As Exception
        MessageBox.Show(ex.Message.ToString())
    End Try
End Sub

Private Delegate Sub changeLabelDel(lbl As Label, txt As String)

Private Sub changeLabelText(lbl As Label, txt As String)
    If lbl.InvokeRequired Then
        lbl.Invoke(New changeLabelDel(AddressOf ChangeLabelText), lbl, txt)
    Else
        lbl.Text = txt
    End If
End Sub

哦,还有一件事.您可以自动化整个调用所需的检查以备将来使用.将扩展方法放在一个模块中

Oh, one more thing. You can automate the whole invoke required check for future use. Put the extension method in a module

方法三:自动化调用所需模式,可重用代码

Method 3: Automate the invoke required pattern, reusable code

<Extension() _
Public Sub InvokeIfRequired(ByVal c As Control, mi As MethodInvoker)
    If c.InvokeRequired Then
        c.Invoke(mi)
    Else
        mi()
    End If
End Sub

然后这样称呼它:

Public Sub Rename(ByVal labelno As Integer)
    Try
        Dim lbl As New Label
        lbl = Controls("Label" & labelno.ToString())
        lbl.InvokeIfRequired(Sub() lbl.Text = ReceivedFrame.ToString())
    Catch ex As Exception
        MessageBox.Show(ex.Message.ToString())
    End Try
End Sub

此站点上有大量关于此主题的信息.见

There's a wealth of information on this site regarding this topic. See

.Net 中的InvokeRequired"和Invoke"是什么意思?

VB.net、调用、委托和线程.不知道如何跨类使用它们

这篇关于如何在运行时在 vb.net 中将标签名称从 1 重命名为 75?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆