English 中文(简体)
VB. NET 积极/消极的分类账的最大宽度
原标题:VB.NET textbox maxlength for positive/negative integers

问题很简单: 我的表格有文字箱,在其中一个文本箱中,我想用户要进入一个正面或负面的2-数字号。 我期望以简单的方式执行这一限制,即不必压缩数量并检查其绝对价值是否低于100。

If I set the textbox maxlength to 2, they cannot enter numbers below -9. If the maxlength is 3, they can enter numbers above 99.

A masked textbox has no solution, I cannot make the "-" literal optional, or at least not that I know of.

What would be the simplest solution to this restriction: "An empty textbox will accept 2 input characters if no "-" is typed, otherwise it accepts 3"? Handling the change event to see if a "-" was typed and resetting maxlength seems a bit overkill...

最佳回答

You actually want a NumericUpDownControl. Use that, and set its Minimum and Maximum properties. No validation code required at all, and easily solves your problem.

如果你重新使用<代码>TextBox,那么,简而言之,你不会获得所希望的功能,而不必实际核对数字。 您可以撰写,以便照此办理,而且您可以重新使用,但您仍须撰写一些确认书。

只是利用伙伴关系验证愤怒。 NET/Javascript作鉴定。 举例而言,使用红色的标签显示验证信息。

private void numberTextBox_TextChanged(object sender, EventArgs e) {
    int number;

    bool isValid = int.TryParse(numberTextBox.Text, out number);

    if (!isValid) {
        validationLabel.Text = "Must be a two-digit number.";
        validationLabel.Visible = true;
        return;
    }

    if (number < -99 || number > 99) {
        validationLabel.Text = "Must be between -99 and 99";
        validationLabel.Visible = true;
        return;
    }

    if (isValid) {
        validationLabel.Visible = false;
        // Do something else with your number
        // if you need to.
    }
}

没有必要写成法典来进行验证,这没有任何错误。 否则,你可能利用现有设计师的特性获得良好结果。

问题回答

核对文本框的关键活动,如果特性为“-”

该法典只允许Digit和负面签名,如果该数字大于100,你可以检查。

private Sub Textbox1_KeyPress (ByVal sender as object, ByVal e as KeyPressEventArgs)

  If (e.IsDigit(e.KeyChar) OR e.KeyChar =  - ) Then
         e.Handled = false
  End If

End Sub




相关问题
Is Shared ReadOnly lazyloaded?

I was wondering when I write Shared ReadOnly Variable As DataType = New DataType() Or alternatively Shared ReadOnly Variable As New DataType() Is it lazy loaded or as the instance initializes? ...

Entertaining a baby with VB.NET

I would like to write a little application in VB.NET that will detect a baby s cry. How would I get started with such an application?

Choose Enter Rather than Pressing Ok button

I have many fields in the page and the last field is a dropdown with list of values. When I select an item in a dropdown and press Enter, it doesn t do the "Ok". Instead I have to manually click on Ok ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

Set Select command in code

On button Click I want to Set the Select command of a Gridview. I do this and then databind the grid but it doesn t work. What am i doing wrong? protected void bttnView_Click(object sender, ...

Hover tooltip on specific words in rich text box?

I m trying to create something like a tooltip suddenly hoovering over the mouse pointer when specific words in the richt text box is hovered over. How can this be done?

热门标签