1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
/*
* NumberTextBox.cs
* Copyright © 2009-2011 kbinani
*
* This file is part of org.kbinani.cadencii.
*
* org.kbinani.cadencii is free software; you can redistribute it and/or
* modify it under the terms of the GPLv3 License.
*
* org.kbinani.cadencii is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#if JAVA
package org.kbinani.cadencii;
import java.awt.*;
import javax.swing.event.DocumentEvent;
import org.kbinani.*;
import org.kbinani.windows.forms.*;
#else
#define COMPONENT_ENABLE_LOCATION
using System;
using org.kbinani;
using org.kbinani.java.awt;
using org.kbinani.windows.forms;
namespace org.kbinani.cadencii {
using boolean = System.Boolean;
#endif
#if JAVA
public class NumberTextBox extends BTextBox {
#else
public class NumberTextBox : BTextBox {
#endif
public enum ValueType {
Double,
Float,
Integer,
}
private ValueType m_value_type = ValueType.Double;
private Color m_textcolor_normal = Color.black;
private Color m_textcolor_invalid = Color.white;
private Color m_backcolor_normal = Color.white;
private Color m_backcolor_invalid = new Color( 240, 128, 128 );
#if !JAVA
/// <summary>
/// IDEでのデザイン用
/// </summary>
public ValueType Type {
get {
return getType();
}
set {
setType( value );
}
}
#endif
public ValueType getType() {
return m_value_type;
}
public void setType( ValueType value ) {
m_value_type = value;
}
#if JAVA
public void update( DocumentEvent e ){
super.updates( e );
validateText();
}
#endif
#if !JAVA
protected override void OnTextChanged( EventArgs e ) {
base.OnTextChanged( e );
validateText();
}
#endif
private void validateText() {
boolean valid = false;
String text = getText();
if ( m_value_type == ValueType.Double ) {
double dou;
try {
dou = str.tof( text );
valid = true;
} catch ( Exception ex ) {
valid = false;
}
} else if ( m_value_type == ValueType.Float ) {
float flo;
try {
flo = (float)str.tof( text );
valid = true;
} catch ( Exception ex ) {
valid = false;
}
} else if ( m_value_type == ValueType.Integer ) {
int inte;
try {
inte = str.toi( text );
valid = true;
} catch ( Exception ex ) {
valid = false;
}
}
if ( valid ) {
setForeground( m_textcolor_normal );
setBackground( m_backcolor_normal );
} else {
setForeground( m_textcolor_invalid );
setBackground( m_backcolor_invalid );
}
}
}
#if !JAVA
}
#endif
|