WIP: first pass of depending controls

This commit is contained in:
Jonathan Jenne
2020-02-10 17:09:14 +01:00
parent ee404100a4
commit acf61c9ca5
12 changed files with 275 additions and 211 deletions

View File

@@ -1,10 +1,15 @@
Imports System.Data.SqlClient
Imports Oracle.ManagedDataAccess.Client
Imports DigitalData.Controls.LookupGrid
Public Class ClassControls
Private Property Form As frmIndex
Private Property Panel As Panel
Public Class ControlMeta
Public Property IndexName As String
End Class
Public Sub New(Panel As Panel, Form As frmIndex)
Me.Form = Form
Me.Panel = Panel
@@ -17,6 +22,9 @@ Public Class ClassControls
chk.Name = "chk" & indexname
chk.Size = New Size(100, 27)
chk.Location = New Point(11, y)
chk.Tag = New ControlMeta() With {
.IndexName = indexname
}
If caption <> "" Then
chk.Text = caption
@@ -33,6 +41,8 @@ Public Class ClassControls
chk.Checked = value
End If
AddHandler chk.CheckedChanged, AddressOf Checkbox_CheckedChanged
Return chk
Catch ex As Exception
ClassLogger.Add("Unhandled Exception in AddCheckBox: " & ex.Message, True)
@@ -40,91 +50,43 @@ Public Class ClassControls
End Try
End Function
Public Sub Checkbox_CheckedChanged(sender As CheckBox, e As EventArgs)
PrepareDependingControl(sender)
End Sub
Public Function AddVorschlag_ComboBox(indexname As String, y As Integer, conid As Integer, sql_Vorschlag As String, Multiselect As Boolean, Optional Vorgabe As String = "", Optional AddNewValues As Boolean = False, Optional PreventDuplicateValues As Boolean = False) As Control
Try
Dim connectionString As String
Dim sqlCnn As SqlConnection
Dim sqlCmd As SqlCommand
Dim adapter As New SqlDataAdapter
Dim oSql As String = sql_Vorschlag
Dim oConnectionString As String
Dim oControl As New DigitalData.Controls.LookupGrid.LookupControl2 With {
.MultiSelect = Multiselect,
.AllowAddNewValues = AddNewValues,
.PreventDuplicates = PreventDuplicateValues,
.Location = New Point(11, y),
.Size = New Size(300, 27),
.Name = "cmbMulti" & indexname,
.Tag = New ControlMeta() With {
.IndexName = indexname
}
}
Dim oracleConn As OracleConnection
Dim oracleCmd As OracleCommand
Dim oracleadapter As New OracleDataAdapter
AddHandler oControl.
Dim NewDataset As New DataSet
Dim i As Integer
Dim sql As String
Dim runinLZ As Boolean = False
connectionString = ClassFormFunctions.GetConnectionString(conid)
If connectionString Is Nothing = False Then
'SQL Befehl füllt die Auswahlliste
sql = sql_Vorschlag
If Not sql.Contains("@") Then
If connectionString.Contains("Initial Catalog=") Then
sqlCnn = New SqlConnection(connectionString)
sqlCnn.Open()
sqlCmd = New SqlCommand(sql, sqlCnn)
adapter.SelectCommand = sqlCmd
adapter.Fill(NewDataset)
ElseIf connectionString.StartsWith("Data Source=") And connectionString.Contains("SERVICE_NAME") Then
oracleConn = New OracleConnection(connectionString)
' Try
oracleConn.Open()
oracleCmd = New OracleCommand(sql_Vorschlag, oracleConn)
oracleadapter.SelectCommand = oracleCmd
oracleadapter.Fill(NewDataset)
End If
oConnectionString = ClassFormFunctions.GetConnectionString(conid)
If oConnectionString IsNot Nothing Then
If ClassPatterns.HasComplexPatterns(oSql) Then
LOGGER.Debug(" >>sql enthält Platzhalter und wird erst während der Laufzeit gefüllt!", False)
Else
runinLZ = True
If LogErrorsOnly = False Then ClassLogger.Add(" >>sql enthält Platzhalter und wird erst während der Laufzeit gefüllt!", False)
End If
If runinLZ = True Then
'Die Standardcombobox anlegen
Dim oCombobox As ComboBox
oCombobox = AddCombobox(indexname, y)
oCombobox.Size = New Size(300, 27)
Return oCombobox
Else
Dim table As DataTable = NewDataset.Tables(0)
Dim oControl As New DigitalData.Controls.LookupGrid.LookupControl2 With {
.DataSource = table,
.MultiSelect = Multiselect,
.AllowAddNewValues = AddNewValues,
.PreventDuplicates = PreventDuplicateValues,
.Location = New Point(11, y),
.Size = New Size(300, 27),
.Name = "cmbMulti" & indexname
}
If connectionString.Contains("Initial Catalog=") Then
Try
adapter.Dispose()
sqlCmd.Dispose()
sqlCnn.Close()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
Else
Try
oracleadapter.Dispose()
oracleCmd.Dispose()
oracleConn.Close()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
End If
Return oControl
Dim oDatatable = ClassDatabase.Return_Datatable_Combined(oSql, oConnectionString, False)
oControl.DataSource = oDatatable
End If
End If
Return oControl
Catch ex As Exception
ClassLogger.Add(" - Unvorhergesehener Unexpected error in AddVorschlag_ComboBox - Indexname: " & indexname & " - Fehler: " & vbNewLine & ex.Message)
MsgBox(ex.Message, MsgBoxStyle.Critical, "Unvorhergesehener Unexpected error in AddVorschlag_ComboBox:")
Return Nothing
End Try
End Function
@@ -134,10 +96,12 @@ Public Class ClassControls
cmb.AutoSize = True
cmb.Size = New Size(300, 27)
cmb.Location = New Point(11, y)
'cmb.AutoCompleteMode = AutoCompleteMode.SuggestAppend
'cmb.AutoCompleteSource = AutoCompleteSource.ListItems
'AddHandler cmb.KeyUp, AddressOf AutoCompleteCombo_KeyUp
cmb.Tag = New ControlMeta() With {
.IndexName = indexname
}
AddHandler cmb.SelectedIndexChanged, AddressOf OncmbSIndexChanged
AddHandler cmb.GotFocus, AddressOf OncmbGotFocus
AddHandler cmb.LostFocus, AddressOf OncmbLostFocus
@@ -310,6 +274,9 @@ Public Class ClassControls
txt.Size = New Size(260, 27)
txt.Location = New Point(11, y)
txt.Tag = New ControlMeta() With {
.IndexName = indexname
}
If text <> "" Then
txt.Text = text
@@ -343,8 +310,86 @@ Public Class ClassControls
Public Sub OnTextBoxKeyUp(sender As System.Object, e As System.Windows.Forms.KeyEventArgs)
Dim oTextbox As TextBox = sender
If oTextbox.Text = String.Empty Then
Exit Sub
End If
If e.KeyCode = Keys.Return Or e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Tab Then
PrepareDependingControl(oTextbox)
End If
If (e.KeyCode = Keys.Return) Then
SendKeys.Send("{TAB}")
End If
End Sub
Private Sub PrepareDependingControl(Control As Control)
If TypeOf Control Is Label Then
Exit Sub
End If
Try
Dim oMeta = DirectCast(Control.Tag, ClassControls.ControlMeta)
Dim oIndexName As String = oMeta.IndexName
Dim oSQL = $"SELECT * FROM TBDD_INDEX_MAN WHERE SQL_RESULT LIKE '%{oIndexName}%'"
Dim oDatatable As DataTable = ClassDatabase.Return_Datatable(oSQL)
If Not IsNothing(oDatatable) Then
For Each oRow As DataRow In oDatatable.Rows
Dim oControlName As String = NotNull(oRow.Item("NAME"), "")
Dim oConnectionId As Integer = NotNull(oRow.Item("CONNECTION_ID"), -1)
Dim oControlSql As String = NotNull(oRow.Item("SQL_RESULT"), "")
If oConnectionId = -1 Or oControlSql = String.Empty Then
LOGGER.Warn("Missing SQL Query or ConnectionId for Control [{0}]! Continuing.", oControlName)
Continue For
End If
oControlSql = ClassPatterns.ReplaceUserValues(oControlSql, USER_PRENAME, USER_SURNAME, USER_SHORT_NAME, USER_LANGUAGE, USER_EMAIL, USER_ID, CURRENT_DOKART_ID)
oControlSql = ClassPatterns.ReplaceInternalValues(oControlSql)
oControlSql = ClassPatterns.ReplaceControlValues(oControlSql, Panel)
SetDependingControlResult(oControlName, oControlSql, oConnectionId)
Next
End If
Catch ex As Exception
LOGGER.Error(ex)
End Try
End Sub
Private Sub SetDependingControlResult(IndexName As String, SqlCommand As String, SqlConnectionId As Integer)
Try
Dim oConnectionString = ClassFormFunctions.GetConnectionString(SqlConnectionId)
Dim oDatatable As DataTable = ClassDatabase.Return_Datatable_CS(SqlCommand, oConnectionString)
Dim oFoundControl As Control = Nothing
For Each oControl As Control In Panel.Controls
If TypeOf oControl Is Label Then
Continue For
End If
Dim oMeta = DirectCast(oControl.Tag, ClassControls.ControlMeta)
Dim oIndex As String = oMeta.IndexName
If oIndex = IndexName Then
oFoundControl = oControl
Exit For
End If
Next
If oFoundControl IsNot Nothing Then
If TypeOf oFoundControl Is TextBox Then
DirectCast(oFoundControl, TextBox).Text = oDatatable.Rows.Item(0).Item(0)
ElseIf TypeOf oFoundControl Is LookupControl2 Then
DirectCast(oFoundControl, LookupControl2).DataSource = oDatatable
ElseIf TypeOf oFoundControl Is ComboBox Then
DirectCast(oFoundControl, ComboBox).DataSource = oDatatable
End If
End If
Catch ex As Exception
LOGGER.Error(ex)
End Try
End Sub
End Class