Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 格式化文本框以始终显示$符号_Vb.net_Winforms - Fatal编程技术网

Vb.net 格式化文本框以始终显示$符号

Vb.net 格式化文本框以始终显示$符号,vb.net,winforms,Vb.net,Winforms,我需要一个有美元符号的文本框。我不希望允许用户在文本框中不使用它 这是我的代码,它做得很好。现在我只需要在开始时硬编码$a Private Sub txtPriceAmount_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtPriceAmount.KeyDown If (e.KeyCode >= Keys.D0 And e.KeyCode <

我需要一个有美元符号的文本框。我不希望允许用户在文本框中不使用它

这是我的代码,它做得很好。现在我只需要在开始时硬编码$a

 Private Sub txtPriceAmount_KeyDown(ByVal sender As Object, ByVal e As  System.Windows.Forms.KeyEventArgs) Handles txtPriceAmount.KeyDown
    If (e.KeyCode >= Keys.D0 And e.KeyCode <= Keys.D9) OrElse (e.KeyCode >= Keys.NumPad0 And e.KeyCode <= Keys.NumPad9) OrElse e.KeyCode = Keys.Back Then
        acceptableKey = True
    Else
        acceptableKey = False
    End If

End Sub

Private Sub txtPriceAmount_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPriceAmount.KeyPress


    ' Check for the flag being set in the KeyDown event.
    If acceptableKey = False Then
        ' Stop the character from being entered into the control since it is non-numerical.
        e.Handled = True
        Return
    Else
        'must be in first position


        If e.KeyChar = Convert.ToChar(Keys.Back) Then
            If strCurrency.Length > 0 Then
                strCurrency = strCurrency.Substring(0, strCurrency.Length - 1)
            End If
        Else
            strCurrency = strCurrency & e.KeyChar
        End If

        If strCurrency.Length = 0 Then
            txtPriceAmount.Text = ""
        ElseIf strCurrency.Length = 1 Then
            txtPriceAmount.Text = "0.0" & strCurrency
        ElseIf strCurrency.Length = 2 Then
            txtPriceAmount.Text = "0." & strCurrency
        ElseIf strCurrency.Length > 2 Then
            txtPriceAmount.Text = strCurrency.Substring(0, strCurrency.Length - 2) & "." & strCurrency.Substring(strCurrency.Length - 2)
        End If
        txtPriceAmount.Select(txtPriceAmount.Text.Length, 0)

    End If
    e.Handled = True
End Sub
Private Sub txtPriceAmount_KeyDown(ByVal sender作为对象,ByVal e作为System.Windows.Forms.KeyEventArgs)处理txtPriceAmount.KeyDown
如果(e.KeyCode>=Keys.D0和e.KeyCode=Keys.NumPad0和e.KeyCode“9”),并且
e、 KeyChar ControlChars,那时候
e、 已处理=真
如果结束
'使用此选项添加任何特殊字符………..e.KeyChar.''和
端接头
私有子按钮1\u单击(发送者作为对象,e作为事件参数)处理btnSearchDescription。单击
'搜索inventoryProduct的ProductIDString属性
'数组,以查看ProductIDTextBox的值是否与ID匹配
“在数组中
'启动变量以控制搜索
Dim FoundBoolean As Boolean=False“控制搜索时间
Dim RowInteger作为整数=0'搜索中的当前行
'循环以执行搜索
直到FoundBoolean=True或RowInteger>NumberProductsInTager
'将文本框与数组进行比较
如果txtDescription.Text=InventoryProduct(RowInteger).DescriptionString然后“如果txtProductID.Text=InventoryProduct(RowInteger).ProductIDString然后
'找到匹配项-向只读文本框显示其他数据
txtProductID.Text=InventoryProduct(RowInteger).ProductIDString
cboProductIDLookup.SelectedItem=txtProductID.Text
txtDescription.Text=InventoryProduct(RowInteger).DescriptionString
txtQuantityAmount.Text=InventoryProduct(RowInteger).QuantityInteger.ToString
txtPriceAmount.Text=InventoryProduct(RowInteger).PriceDecimal.ToString(“C2”)
'更改变量以指示我们有匹配项
FoundBoolean=True
其他的
“还没有对手
行整数+=1
如果结束
环
'在搜索之后,确定是否找到ProductID
如果FoundBoolean=False,则“未找到匹配项”
'清除显示产品信息的文本框控件
'除了ProductID文本框之外
txtDescription.Clear()
txtQuantityAmount.Clear()
txtPriceAmount.Clear()
cboProductIDLookup.SelectedIndex=-1
'显示ProductID无效的消息
MessageBox.Show(“重新输入有效的产品ID.”、“无效标识符”、MessageBoxButtons.OK、MessageBoxIcon.叹号)
txtProductID.Focus()
txtProductID.SelectAll()
如果结束
端接头
私有子txtPriceAmount_KeyDown(ByVal sender作为对象,ByVal e作为System.Windows.Forms.KeyEventArgs)处理txtPriceAmount.KeyDown

如果(e.KeyCode>=Keys.D0和e.KeyCode=Keys.NumPad0和e.KeyCode查看MaskedTextBox控件。您应该能够使用类似于
$00.00
的掩码

试试这个

 <wpf:TextBox Text="{Binding YourPropertie, StringFormat='$\{0:\}' }" />


我希望它能起作用:有几种方法可以解决您的问题,但首先您需要在表单中添加一个控件

完成此操作后,您可以将控件的“掩码”属性(在F4属性中)设置为您希望的任何值,如“$00.00”。或者,您可以通过以下操作将其绑定到事件,如按钮单击:

private void someButton_Click(object sender, EventArgs e)
{
   maskedTextBox1.Mask = "$00.00";
}

在任何一种情况下,这都应该为您提供您希望的结果。

您可以尝试使用RichTextBox控件:

RichTextBox1.Text = "$"
RichTextBox1.SelectAll()
RichTextBox1.SelectionProtected = True
虽然更简单的解决方案可能是在文本框前面放置一个带有美元符号的标签,或者如其他人所提到的,但MaskedTextBox控件是为这种情况而设计的。

代码(程序)应该是为了让用户的生活更轻松,而不是更困难。如果您将
$
强制输入文本框,这只是对美国用户有效的方法。其次,它将有效输入从数字(数字)转换为文本,如果需要将其保存到数据库或用于计算,则需要另一个步骤

如果真正的问题是打印
$
,那么您只需要:

 decVar.ToString("C2")
对于用户的视觉提示,可以是标签或蒙面编辑控件。强制用户执行您的工作是不好的做法。格式化数据以适当的格式显示是您的工作,而不是用户的工作,即使只是一次按键

另一个解决方案,可能是你的拿手好戏,就是换掉显示器。在
文本框中\u Leave

   textAmount.Text = decVar.ToString("C2")
在文本框中输入:

    textAmount.Text = decVar.ToString

因此,当他们不编辑文本框时,文本框中有$,并将打印在装箱单或任何东西上。我应该注意,如果您是从变量中打印
decVar.ToString(“C2”)
比打印
textAmount更灵活。Text
因为你的方式需要订单或其他任何东西!你为什么要为自己设置这样奇怪的障碍?在标签旁边加上“$”,这样就可以清楚地看到美元是条目。重要的是,用户了解比例是多少或者条目的格式是。如果他们不输入美元符号,你真的会抛出错误消息吗?不会,但文本框内的内容会被打印出来并形成一个材料列表。有一些总价加上美元,而其他的没有,这看起来很不专业。我想我是肛门,但我相信老师会为它计数。但是h保留$label是一种安慰,我可能会求助于Thanks,在打印时将文本格式化为货币-这与给用户带来噩梦般的GUI相比是一件非常琐碎的事情。确保有一个“$”应该在哪里是代码的工作,而不是用户的工作。即使使用蒙面文本编辑,您也需要格式化它…我有一个按钮可以做到这一点。TxtTotalDueAumount.Text=TotalDueDecimal.ToString(“C2”)但是由于某种原因,当$不在文本框中时,它不会显示它为什么它需要在文本框中?使用MAskedEdit向用户显示预期的美元金额
    textAmount.Text = decVar.ToString