在插入位置验证TextBox输入

时间:2017-08-16 10:19:17

标签: vb.net validation textbox decimal

我有以下代码将十进制输入格式化为public class WaveWriter { private static final int OUTPUT_STREAM_BUFFER = 16384; private File mOutFile; private BufferedOutputStream mOutputStream; private int mSampleRate; private int mChannels; private int mSampleBits; private int mBytesWritten; /** * Constructor; initializes WaveWriter * * @param path output file path * @param name output file name * @param sampleRate output sample rate * @param channels number of channels * @param sampleBits number of bits per sample (S8LE, S16LE) */ public WaveWriter(String path, String name, int sampleRate, int channels, int sampleBits) { this.mOutFile = new File(path + File.separator + name); this.mSampleRate = sampleRate; this.mChannels = channels; this.mSampleBits = sampleBits; this.mBytesWritten = 0; } /** * Constructor; Initializes WaveWriter * * @param file output file handle * @param sampleRate output sample rate * @param channels number of channels * @param sampleBits number of bits per sample */ public WaveWriter(File file, int sampleRate, int channels, int sampleBits) { this.mOutFile = file; this.mSampleRate = sampleRate; this.mChannels = channels; this.mSampleBits = sampleBits; this.mBytesWritten = 0; } /** * Create output wav file * * @return whether file creation succeded * * @throws IOException if file I/O error occurs allocating header */ public boolean createWaveFile() throws IOException { if (mOutFile.exists()) { mOutFile.delete(); } if (mOutFile.createNewFile()) { FileOutputStream fileStream = new FileOutputStream(mOutFile); mOutputStream = new BufferedOutputStream(fileStream, OUTPUT_STREAM_BUFFER); //write 44 bytes of space for the header mOutputStream.write(new byte[44]); return true; } return false; } /** * write audio data to ooutput file (mono). Does nothing if output * file is not mono channel * * @param src audo data input buffer * @param offset offset into src buffer * @param length buffer size in number of samples * * @throws IOException if file I/O error occurs */ public void write(short[] src, int offset, int length) throws IOException { if (mChannels != 1) return; if (offset > length) throw new IndexOutOfBoundsException( String.format("offset %d is greater than length %d", offset, length)); for (int i = offset; i<length; i++) { writeUnsignedShortLE(mOutputStream, src[i]); mBytesWritten += 2; } } /** * write audio data to output file (stereo). Does nothing if output file is not * stereo channel * * @param left left channel audio data buffer * @param right right channel audio data buffer * @param offset offset into left/right buffers * @param length buffer size in number of samples * * @throws IOException if file I/O error occurs */ public void write(short[] left, short[] right, int offset, int length) throws IOException { if (mChannels != 2) { return; } if (offset > length) { throw new IndexOutOfBoundsException(String.format("offset %d is greater than length %d", offset, length)); } for (int i = offset; i < length; i++) { writeUnsignedShortLE(mOutputStream, left[i]); writeUnsignedShortLE(mOutputStream, right[i]); mBytesWritten += 4; } } /** * close output Wav file and write WAV header. WaveWriter cannot * be used anymore after this method has been called * * @throws IOException if file I/O error occurs writing WAV header */ public void closeWaveFile() throws IOException { if (mOutputStream != null) { this.mOutputStream.flush(); this.mOutputStream.close(); } writeWaveHeader(); } private void writeWaveHeader() throws IOException { // go back to the beginning of file RandomAccessFile file = new RandomAccessFile(this.mOutFile, "rw"); file.seek(0); int bytesPerSec = (mSampleBits + 7) / 8; file.writeBytes("RIFF"); // WAV chunk header file.writeInt(Integer.reverseBytes(mBytesWritten + 36)); // WAV chunk size file.writeBytes("WAVE"); // WAV format file.writeBytes("fmt "); // format subchunk header file.writeInt(Integer.reverseBytes(16)); // format subchunk size file.writeShort(Short.reverseBytes((short) 1)); // audio format file.writeShort(Short.reverseBytes((short) mChannels)); // number of channels file.writeInt(Integer.reverseBytes(mSampleRate)); // sample rate file.writeInt(Integer.reverseBytes(mSampleRate * mChannels * bytesPerSec)); // byte rate file.writeShort(Short.reverseBytes((short) (mChannels * bytesPerSec))); // block align file.writeShort(Short.reverseBytes((short) mSampleBits)); // bits per sample file.writeBytes("data"); // data subchunk header file.writeInt(Integer.reverseBytes(mBytesWritten)); // data subchunk size file.close(); file = null; } private static void writeUnsignedShortLE(BufferedOutputStream stream, short sample) throws IOException { // write() already writes the lower order byte of this short stream.write(sample); stream.write((sample >> 8)); } }

TextBox

但是,如果我在If Char.IsDigit(e.KeyChar) OrElse e.KeyChar = "."c Then If e.KeyChar = "."c Then If txtPrice.Text.Contains("."c) Then e.Handled = True End If ElseIf Char.IsDigit(e.KeyChar) Then If txtPrice.Text.Contains("."c) Then Dim value As String = txtPrice.Text Dim units As String = value.Substring(value.IndexOf("."c) + 1) If units.Length >= 2 Then e.Handled = True End If End If End If ElseIf e.KeyChar <> Chr(8) Then e.Handled = True End If 中输入一个值,那么一切正常。 10.50,当我然后尝试删除第一个0并将值更改为11.50时,它将不允许我,因为小数点后面有2个数字取消所有其他用户输入,以确保只有2个小数位可以输入。

有没有办法可以对此进行验证,以检查是否在小数点之前或之后添加了数字?或者它是否可以根据插入符位置进行检查?

可能有一种更简单的方法来验证TextBox中的小数,但是此代码也用于验证网格和其他控件,因此需要使用此格式的内容。

1 个答案:

答案 0 :(得分:0)

检查SelectionStart属性。

If units.Length >= 2 AndAlso txtPrice.SelectionStart > txtPrice.Text.IndexOf(".") Then
    e.Handled = True
End If

以上代码解释:

如果.您尝试添加另一个文字之后的文字已有2个字符,请禁止按键。如果在.之前添加,则允许。