1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.ComponentModel;
|
---|
4 | using System.Drawing;
|
---|
5 | using System.Data;
|
---|
6 | using System.Linq;
|
---|
7 | using System.Text;
|
---|
8 | using System.Threading.Tasks;
|
---|
9 | using System.Windows.Forms;
|
---|
10 | using System.Globalization;
|
---|
11 |
|
---|
12 | namespace HeuristicLab.JsonInterface.OptimizerIntegration.Shared {
|
---|
13 | public partial class NumericRangeControl : UserControl {
|
---|
14 |
|
---|
15 | private JsonItemVM vm;
|
---|
16 | public JsonItemVM VM {
|
---|
17 | get => vm;
|
---|
18 | set {
|
---|
19 | vm = value;
|
---|
20 | Init();
|
---|
21 | }
|
---|
22 | }
|
---|
23 | public bool IsDouble { get; set; }
|
---|
24 |
|
---|
25 |
|
---|
26 |
|
---|
27 | public NumericRangeControl() {
|
---|
28 | InitializeComponent();
|
---|
29 | Init();
|
---|
30 | }
|
---|
31 |
|
---|
32 | private void Init() {
|
---|
33 | textBoxFrom.Text = "";
|
---|
34 | textBoxTo.Text = "";
|
---|
35 | textBoxFrom.ReadOnly = true;
|
---|
36 | textBoxTo.ReadOnly = true;
|
---|
37 | checkBoxFrom.Checked = false;
|
---|
38 | checkBoxFrom.Checked = false;
|
---|
39 | }
|
---|
40 |
|
---|
41 | private void checkBoxFrom_CheckStateChanged(object sender, EventArgs e) {
|
---|
42 | textBoxFrom.ReadOnly = !checkBoxFrom.Checked;
|
---|
43 | textBoxFrom.Text = "";
|
---|
44 | if(!checkBoxFrom.Checked)
|
---|
45 | SetRange();
|
---|
46 | }
|
---|
47 |
|
---|
48 | private void checkBoxTo_CheckStateChanged(object sender, EventArgs e) {
|
---|
49 | textBoxTo.ReadOnly = !checkBoxTo.Checked;
|
---|
50 | textBoxTo.Text = "";
|
---|
51 | if (!checkBoxTo.Checked)
|
---|
52 | SetRange();
|
---|
53 | }
|
---|
54 |
|
---|
55 | private void textBoxFrom_Leave(object sender, EventArgs e) {
|
---|
56 | if (checkBoxFrom.Checked)
|
---|
57 | SetRange();
|
---|
58 | }
|
---|
59 |
|
---|
60 | private void textBoxTo_Leave(object sender, EventArgs e) {
|
---|
61 | if (checkBoxTo.Checked)
|
---|
62 | SetRange();
|
---|
63 | }
|
---|
64 |
|
---|
65 | private void SetRange() {
|
---|
66 | object[] range = new object[2];
|
---|
67 | if (checkBoxFrom.Checked && !string.IsNullOrWhiteSpace(textBoxFrom.Text))
|
---|
68 | range[0] = Parse(textBoxFrom.Text);
|
---|
69 | else
|
---|
70 | range[0] = IsDouble ? double.MinValue : int.MinValue;
|
---|
71 |
|
---|
72 | if (checkBoxTo.Checked && !string.IsNullOrWhiteSpace(textBoxTo.Text))
|
---|
73 | range[1] = Parse(textBoxTo.Text);
|
---|
74 | else
|
---|
75 | range[1] = IsDouble ? double.MinValue : int.MinValue;
|
---|
76 | VM.Item.Range = range;
|
---|
77 | }
|
---|
78 |
|
---|
79 | private object Parse(string s) {
|
---|
80 | if (IsDouble) {
|
---|
81 | return double.Parse(s.Replace(",", "."), NumberStyles.Any, CultureInfo.InvariantCulture);
|
---|
82 | }
|
---|
83 | return int.Parse(s);
|
---|
84 | }
|
---|
85 | }
|
---|
86 | }
|
---|