Skip to content

Commit

Permalink
Add validation to numeric input.
Browse files Browse the repository at this point in the history
  • Loading branch information
bgsamm committed Feb 7, 2022
1 parent 68bbc18 commit 12a1839
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 17 deletions.
8 changes: 4 additions & 4 deletions PBRHex Setup/PBRHex Setup.vdproj
Original file line number Diff line number Diff line change
Expand Up @@ -3141,15 +3141,15 @@
{
"Name" = "8:Microsoft Visual Studio"
"ProductName" = "8:PBRHex"
"ProductCode" = "8:{343B4450-5F9D-4AF3-8DA0-3FA3A1F3B6F6}"
"PackageCode" = "8:{0BEE662A-A756-4A71-8FF2-A623EBB38CC8}"
"ProductCode" = "8:{D34F3B6D-8EB0-46F0-8DAE-4F4D73C5BCAD}"
"PackageCode" = "8:{6C8B60FD-8849-47DA-ABB5-EE8281838E8A}"
"UpgradeCode" = "8:{D48A0932-A217-4C66-91CB-5BF77D165DBB}"
"AspNetVersion" = "8:2.0.50727.0"
"RestartWWWService" = "11:FALSE"
"RemovePreviousVersions" = "11:TRUE"
"DetectNewerInstalledVersion" = "11:TRUE"
"InstallAllUsers" = "11:FALSE"
"ProductVersion" = "8:1.1.0"
"ProductVersion" = "8:1.1.1"
"Manufacturer" = "8:PBRHex"
"ARPHELPTELEPHONE" = "8:"
"ARPHELPLINK" = "8:"
Expand Down Expand Up @@ -3629,7 +3629,7 @@
{
"{5259A561-127C-4D43-A0A1-72F10C7B3BF8}:_FFFCF4EE22884D0D8F8380EC9CCA4EDD"
{
"SourcePath" = "8:..\\PBRHex\\obj\\Release\\PBRHex.exe"
"SourcePath" = "8:"
"TargetName" = "8:"
"Tag" = "8:"
"Folder" = "8:_5092C41DA61F4D24B1B9ADB71B4CCD91"
Expand Down
2 changes: 1 addition & 1 deletion PBRHex/CodeEditorWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
switch (keyData) {
case Keys.Control | Keys.G:
var input = new HexInputDialog("Enter address:");
if (input.ShowDialog() == DialogResult.OK) {
if (input.ShowDialog() == DialogResult.OK && input.Response != null) {
uint addr = (uint)input.Response;
if (!IsAddressInbounds(addr))
new AlertDialog("Address out of bounds.").ShowDialog();
Expand Down
15 changes: 10 additions & 5 deletions PBRHex/Dialogs/HexInputDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ public partial class HexInputDialog : Form
{
public readonly string Prompt;
public string Default { get; set; }
public int Response
{
public int? Response
{
get {
if(decimalRadioButton.Checked)
return int.Parse(textBox1.Text);
return HexUtils.HexToInt(textBox1.Text);
try {
if (decimalRadioButton.Checked)
return int.Parse(textBox1.Text);
return HexUtils.HexToInt(textBox1.Text);
} catch {
new AlertDialog("Invalid input.").ShowDialog();
return null;
}
}
}

Expand Down
13 changes: 8 additions & 5 deletions PBRHex/HexEditor/HexEditorWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,9 @@ private bool PromptFillRange(out byte[] bytes) {
{
Default = "00"
};
if(input.ShowDialog() == DialogResult.OK) {
if(input.Response < 0 || input.Response > 0xff) {
if(input.ShowDialog() == DialogResult.OK && input.Response != null) {
int value = (int)input.Response;
if(value < 0 || value > 0xff) {
new AlertDialog( "Invalid fill value." ).ShowDialog();
return false;
}
Expand All @@ -368,7 +369,7 @@ private bool PromptFillRange(out byte[] bytes) {
bytes = new byte[size];
for(int i = 0; i < size; i++) {
if(IsCellSelected(address + i))
bytes[i] = (byte)input.Response;
bytes[i] = (byte)value;
else
bytes[i] = range[i];
}
Expand All @@ -385,8 +386,10 @@ protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
return true;
case Keys.Control | Keys.G:
var input = new HexInputDialog( "Enter address:" );
if(input.ShowDialog() == DialogResult.OK)
GoTo(input.Response, true);
if (input.ShowDialog() == DialogResult.OK && input.Response != null) {
int address = (int)input.Response;
GoTo(address, true);
}
return true;
case Keys.Control | Keys.S:
Save();
Expand Down
4 changes: 2 additions & 2 deletions PBRHex/StringEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
switch(keyData) {
case Keys.Control | Keys.G:
var input = new HexInputDialog("Enter string ID:");
if(input.ShowDialog() == DialogResult.OK)
GoTo(input.Response);
if(input.ShowDialog() == DialogResult.OK && input.Response != null)
GoTo((int)input.Response);
return true;
case Keys.Control | Keys.S:
Save();
Expand Down

0 comments on commit 12a1839

Please sign in to comment.