private void UpdateCapability(CapabilityContainer capContainer) { try { if (capContainer is CapabilityContainerOneValueFloat) { CapabilityContainerOneValueFloat capContainerFloat = capContainer as CapabilityContainerOneValueFloat; float newFloatValue; if (float.TryParse(ValueTextBox.Text, out newFloatValue)) { capContainerFloat.Value = newFloatValue; } } else if (capContainer is CapabilityContainerOneValueString) { CapabilityContainerOneValueString capContainerString = capContainer as CapabilityContainerOneValueString; capContainerString.Value = ValueTextBox.Text; } else if (capContainer is CapabilityContainerEnum) { CapabilityContainerEnum capContainerEnum = capContainer as CapabilityContainerEnum; CapabilityContainerOneValue capCurrentValue = capContainerEnum.CurrentValue; if (capCurrentValue is CapabilityContainerOneValueFloat) { CapabilityContainerOneValueFloat capCurrentValueFloat = capCurrentValue as CapabilityContainerOneValueFloat; float newFloatValue; if (float.TryParse(CurrentValueTextBox.Text, out newFloatValue)) { capCurrentValueFloat.Value = newFloatValue; } } else if (capCurrentValue is CapabilityContainerOneValueString) { CapabilityContainerOneValueString capCurrentValueString = capCurrentValue as CapabilityContainerOneValueString; capCurrentValueString.Value = CurrentValueTextBox.Text; } } else if (capContainer is CapabilityContainerRange) { CapabilityContainerRange capContainerRange = capContainer as CapabilityContainerRange; float newMaximumValue; if (float.TryParse(MaximumTextBox.Text, out newMaximumValue)) { capContainerRange.Maximum = newMaximumValue; } float newMinimumValue; if (float.TryParse(MinimumTextBox.Text, out newMinimumValue)) { capContainerRange.Minimum = newMinimumValue; } float newValue; if (float.TryParse(ValueTextBox.Text, out newValue)) { capContainerRange.Value = newValue; } float newStepValue; if (float.TryParse(StepTextBox.Text, out newStepValue)) { capContainerRange.Step = newStepValue; } } twainDevice.SetCapability(capContainer); } catch (TwainException ex) { MessageBox.Show(String.Format(Constants.twainErrorString, (int)ex.ConditionCode, ex.ConditionDescription), "Error setting capabilities.", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void ExtractCapabilityFromContainer(CapabilityContainer capContainer, TextBox textBox, Label label) { if (capContainer is CapabilityContainerOneValueFloat) { CapabilityContainerOneValueFloat capContainerFloat = capContainer as CapabilityContainerOneValueFloat; if (textBox != null) { textBox.Text = capContainerFloat.Value.ToString(); textBox.Visible = label.Visible = true; } else { ValuesListBox.Items.Add(capContainerFloat.Value); ValuesListBox.Visible = ValuesLabel.Visible = true; } } else if (capContainer is CapabilityContainerOneValueString) { CapabilityContainerOneValueString capContainerString = capContainer as CapabilityContainerOneValueString; if (textBox != null) { textBox.Text = capContainerString.Value; textBox.Visible = label.Visible = true; } else { ValuesListBox.Items.Add(capContainerString.Value); ValuesListBox.Visible = ValuesLabel.Visible = true; } } else if (capContainer is CapabilityContainerEnum) { CapabilityContainerEnum capContainerEnum = capContainer as CapabilityContainerEnum; CapabilityContainerOneValue capCurrentValue = capContainerEnum.CurrentValue; CapabilityContainerOneValue capDefaultValue = capContainerEnum.DefaultValue; CapabilityContainerOneValueCollection capValues = capContainerEnum.Values; ExtractCapabilityFromContainer(capCurrentValue, CurrentValueTextBox, CurrentValueLabel); ExtractCapabilityFromContainer(capDefaultValue, DefaultValueTextBox, DefaultValueLabel); foreach (CapabilityContainerOneValue capValue in capValues) { ExtractCapabilityFromContainer(capValue, null, null); } } else if (capContainer is CapabilityContainerArray) { CapabilityContainerArray capContainerArray = capContainer as CapabilityContainerArray; CapabilityContainerOneValueCollection capValues = capContainerArray.Values; foreach (CapabilityContainerOneValue capValue in capValues) { ExtractCapabilityFromContainer(capValue, null, null); } UpdateButton.Enabled = false; } else if (capContainer is CapabilityContainerRange) { CapabilityContainerRange capContainerRange = capContainer as CapabilityContainerRange; DefaultValueTextBox.Text = capContainerRange.Default.ToString(); ValueTextBox.Text = capContainerRange.Value.ToString(); MaximumTextBox.Text = capContainerRange.Maximum.ToString(); MinimumTextBox.Text = capContainerRange.Minimum.ToString(); StepTextBox.Text = capContainerRange.Step.ToString(); DefaultValueTextBox.Visible = DefaultValueLabel.Visible = ValueTextBox.Visible = ValueLabel.Visible = MaximumTextBox.Visible = MaximumLabel.Visible = MinimumTextBox.Visible = MinimumLabel.Visible = StepTextBox.Visible = StepLabel.Visible = true; } }