如何在消息框中获取文本和变量

时间:2011-12-25 15:06:55

标签: vb.net messagebox

我只需要知道如何在消息框中包含纯文本和变量。

例如:

我可以这样做:MsgBox(variable)

我可以这样做:MsgBox("Variable = ")

但我不能这样做:MsgBox("Variable = " + variable)

5 个答案:

答案 0 :(得分:18)

正如所建议的那样,使用string.format方法很简单,而且非常易读。

在vb.net中,“+”用于添加,“&”用于字符串连接。

在你的例子中:

MsgBox("Variable = " + variable)

变为:

MsgBox("Variable = " & variable)

我可能有点快速回答这个问题,因为看起来这些运算符都可以用于连接,但建议使用的是“&”,source http://msdn.microsoft.com/en-us/library/te2585xw(v=VS.100).aspx

也许打电话

variable.ToString()

更新

使用字符串插值(vs2015以后我相信):

MsgBox($"Variable = {variable}")

答案 1 :(得分:5)

为什么不使用:

Dim msg as String = String.Format("Variable = {0}", variable)

有关String.Format

的更多信息

答案 2 :(得分:0)

我遇到了同样的问题。我希望我的消息框显示消息和vendorcontractexpiration。这就是我所做的:

Dim ab As String
Dim cd As String

ab = "THE CONTRACT FOR THIS VENDOR WILL EXPIRE ON "
cd = VendorContractExpiration


If InvoiceDate >= VendorContractExpiration - 120 And InvoiceDate < VendorContractExpiration Then

MsgBox [ab] & [cd], vbCritical, "WARNING"

End If

答案 3 :(得分:0)

MsgBox("Variable {0} " , variable)

答案 4 :(得分:0)

应用过滤器选项后,我想在excel工作表中显示行数。

因此,我将最后一行的计数声明为可以添加到Msgbox的变量

Sub lastrowcall()
Dim hm As Worksheet
Dim dm As Worksheet
Set dm = ActiveWorkbook.Sheets("datecopy")
Set hm = ActiveWorkbook.Sheets("Home")
Dim lngStart As String, lngEnd As String
lngStart = hm.Range("E23").Value
lngEnd = hm.Range("E25").Value
Dim last_row As String
last_row = dm.Cells(Rows.Count, 1).End(xlUp).Row

MsgBox ("Number of test results between the selected dates " + lngStart + " 
and " + lngEnd + " are " + last_row + ". Please Select Yes to continue 
Analysis")


End Sub
相关问题