//
// Copyright (c) jindongfang. All rights reserved.
//
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using System;
using System.Windows.Forms;
using static WellWorkDataUI.FrmDataViewer;
namespace WellWorkDataUI
{
///
/// FrmDataFormula
///
public partial class FrmDataFormula : DevExpress.XtraEditors.XtraForm
{
///
/// FormulaData
///
public FormulaData FormulaData { get; private set; }
private TextEdit currentEdit;
private int selectionStart = 0;
private TextEdit CurrentEdit => this.currentEdit ?? this.textFormula1;
///
/// Initializes a new instance of the class.
/// FrmDataFormula
///
/// formulaData
public FrmDataFormula(FormulaData formulaData)
{
this.InitializeComponent();
this.FormulaData = formulaData;
this.listBoxVariable.DataSource = formulaData.ColumnList;
foreach (var item in formulaData.ColumnList)
{
this.comboFormula1.Properties.Items.Add(new ImageComboBoxItem(item.Caption, item));
this.comboFormula2.Properties.Items.Add(new ImageComboBoxItem(item.Caption, item));
this.comboFormula3.Properties.Items.Add(new ImageComboBoxItem(item.Caption, item));
this.comboFormula4.Properties.Items.Add(new ImageComboBoxItem(item.Caption, item));
}
this.FormulaData = formulaData;
}
private void textEdit_Enter(object sender, EventArgs e)
{
this.currentEdit = sender as TextEdit;
}
private void textEdit_Leave(object sender, EventArgs e)
{
this.selectionStart = (sender as TextEdit).SelectionStart;
}
private void listBoxOp_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (this.listBoxOp.HotItemIndex >= 0)
{
var op = this.listBoxOp.Items[this.listBoxOp.HotItemIndex].ToString();
this.CurrentEdit.Text = this.CurrentEdit.Text.Insert(this.selectionStart, op);
this.CurrentEdit.Focus();
this.selectionStart += op.Length;
this.CurrentEdit.SelectionStart = this.selectionStart;
}
}
private void listBoxVariable_CustomItemDisplayText(object sender, CustomItemDisplayTextEventArgs e)
{
if (e.Item is ColumnData cd)
{
e.DisplayText = $"{cd.Variable}: {cd.Caption}";
}
}
private void listBoxVariable_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (this.listBoxVariable.HotItemIndex >= 0)
{
var cd = this.listBoxVariable.GetItem(this.listBoxVariable.HotItemIndex) as ColumnData;
this.CurrentEdit.Text = this.CurrentEdit.Text.Insert(this.selectionStart, cd.Variable);
this.CurrentEdit.Focus();
this.selectionStart += cd.Variable.Length;
this.CurrentEdit.SelectionStart = this.selectionStart;
}
}
private void comboFormula_ButtonClick(object sender, ButtonPressedEventArgs e)
{
if (e.Button.Kind == ButtonPredefines.Delete)
{
(sender as ImageComboBoxEdit).EditValue = null;
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnOK_Click(object sender, EventArgs e)
{
this.CaclFormula(this.comboFormula1, this.textFormula1);
this.CaclFormula(this.comboFormula2, this.textFormula2);
this.CaclFormula(this.comboFormula3, this.textFormula3);
this.CaclFormula(this.comboFormula4, this.textFormula4);
this.FormulaData.RefreshView();
MessageBox.Show("计算完成!");
}
private void CaclFormula(ImageComboBoxEdit combo, TextEdit text)
{
if (combo.EditValue is ColumnData cd)
{
this.FormulaData.CalcFormula(cd, text.Text, this.FormulaData.ColumnList);
}
}
}
}