Imports C1.C1DataExtender
Imports System.Data.OleDb
Imports System.IO

Public Class ConstraintsForm
    Private Sub ShowConstraint()
        If c1DataViewSet1.Views.Count = 0 Then
            Exit Sub
        End If
        Dim constraint As ConstraintExpression = GetShowingConstraint()
        langNamesComboBox.Items.Clear()
        langNamesComboBox.Items.AddRange( _
            constraint.Expression.GetAvailableLanguages())
        langNamesComboBox.Text = constraint.Expression.Language
        textBox1.Text = constraint.Expression.Text
        textBox2.Text = constraint.ErrorDescription
    End Sub

    Private Sub UpdateConstraintFromUI()
        Dim constraint As ConstraintExpression = GetShowingConstraint()
        constraint.Expression.Language = langNamesComboBox.Text
        constraint.Expression.Text = textBox1.Text
        constraint.ErrorDescription = textBox2.Text
    End Sub

    Private Function GetShowingConstraint() As ConstraintExpression
        If radioButton1.Checked Then
            Return c1DataViewSet1.Views(0).BaseSchemaTables(0).Columns("Quantity").ConstraintExpression
        ElseIf (radioButton2.Checked) Then
            Return c1DataViewSet1.Views(0).BaseSchemaTables(0).ConstraintExpression
        ElseIf (radioButton3.Checked) Then
            Return c1DataViewSet1.Views(0).Columns("Quantity").ConstraintExpression
        Else
            Return c1DataViewSet1.Views(0).ConstraintExpression
        End If
    End Function

    Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
        UpdateConstraintFromUI()
    End Sub

    Private Sub ConstraintsForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ShowConstraint()
    End Sub

    Private Sub radioButton_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radioButton1.CheckedChanged, radioButton4.CheckedChanged, radioButton3.CheckedChanged, radioButton2.CheckedChanged
        If CType(sender, RadioButton).Checked Then
            ShowConstraint()
        End If
    End Sub

    Private Sub c1DataViewSet1_ConnectionOpening(ByVal sender As System.Object, ByVal e As C1.C1DataExtender.ConnectionOpeningEventArgs) Handles c1DataViewSet1.ConnectionOpening
        Dim csb As New OleDbConnectionStringBuilder(e.Connection.ConnectionString)
        ' If connection string specified in application resources points to a non-existant database file
        ' then use a database from a default sample database location.
        If Not File.Exists(csb.DataSource) Then
            csb.DataSource = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), _
                     "ComponentOne Samples\Common\C1NWind.mdb")
            e.Connection.ConnectionString = csb.ToString()
        End If
    End Sub
End Class