From ff7e16d5c6cec43cfc029e6a65822ae3b98bf38e Mon Sep 17 00:00:00 2001 From: Jonathan Jenne Date: Tue, 30 Oct 2018 15:37:41 +0100 Subject: [PATCH] jj: dynamic controls depending on multiselect --- LookupGrid/Grid.Designer.vb | 26 +---------- LookupGrid/Grid.vb | 91 +++++++++++++++++++++++++++++++++++-- LookupGrid/frmLookupGrid.vb | 3 ++ 3 files changed, 91 insertions(+), 29 deletions(-) diff --git a/LookupGrid/Grid.Designer.vb b/LookupGrid/Grid.Designer.vb index a7840095..38a4ad37 100644 --- a/LookupGrid/Grid.Designer.vb +++ b/LookupGrid/Grid.Designer.vb @@ -22,41 +22,17 @@ Partial Class Grid 'Das Bearbeiten mit dem Code-Editor ist nicht möglich. Private Sub InitializeComponent() - Me.TextBox1 = New System.Windows.Forms.TextBox() - Me.btnOpenForm = New System.Windows.Forms.Button() Me.SuspendLayout() ' - 'TextBox1 - ' - Me.TextBox1.Location = New System.Drawing.Point(0, 0) - Me.TextBox1.Name = "TextBox1" - Me.TextBox1.Size = New System.Drawing.Size(231, 20) - Me.TextBox1.TabIndex = 0 - ' - 'btnOpenForm - ' - Me.btnOpenForm.Location = New System.Drawing.Point(237, 0) - Me.btnOpenForm.Name = "btnOpenForm" - Me.btnOpenForm.Size = New System.Drawing.Size(30, 20) - Me.btnOpenForm.TabIndex = 1 - Me.btnOpenForm.Text = "..." - Me.btnOpenForm.UseVisualStyleBackColor = True - ' 'Grid ' Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.AutoSize = True Me.BackColor = System.Drawing.Color.Transparent - Me.Controls.Add(Me.btnOpenForm) - Me.Controls.Add(Me.TextBox1) Me.Name = "Grid" - Me.Size = New System.Drawing.Size(270, 23) + Me.Size = New System.Drawing.Size(270, 35) Me.ResumeLayout(False) - Me.PerformLayout() End Sub - - Friend WithEvents TextBox1 As Windows.Forms.TextBox - Friend WithEvents btnOpenForm As Windows.Forms.Button End Class diff --git a/LookupGrid/Grid.vb b/LookupGrid/Grid.vb index b98006b9..ecbc8a36 100644 --- a/LookupGrid/Grid.vb +++ b/LookupGrid/Grid.vb @@ -1,5 +1,7 @@  Imports System.ComponentModel +Imports DevExpress.XtraEditors +Imports DevExpress.XtraEditors.Controls Public Class Grid @@ -13,16 +15,97 @@ Public Class Grid Public Property SelectedValues As New List(Of String) - Private Sub btnOpenForm_Click(sender As Object, e As EventArgs) Handles btnOpenForm.Click - Dim frm As New frmLookupGrid() With { + Private _lookupControlMulti As GridLookUpEdit + Private _lookupControlSingle As ButtonEdit + + Private Const TAG_BUTTON_LOOKUP_FORM = "openLookupForm" + Private Const TEXT_NO_RECORDS = "Keine Datensätze ausgewählt" + Private Const TEXT_N_RECORDS = "{0} Datensätze ausgewählt" + + Private Function GetLookupForm() As frmLookupGrid + Dim oForm As New frmLookupGrid() With { .MultiSelect = MultiSelect, .AddNewValues = AllowAddNewValues, .PreventDuplicates = PreventDuplicates, .DataSource = DataSource, - .SelectedValues = SelectedValues + .SelectedValues = SelectedValues, + .StartPosition = Windows.Forms.FormStartPosition.Manual, + .Location = PointToScreen(New System.Drawing.Point(Width, 0)) } - frm.ShowDialog() + Return oForm + End Function + + + + Private Sub Grid_Load(sender As Object, e As EventArgs) Handles Me.Load + If MultiSelect = False Then + _lookupControlSingle = New ButtonEdit With { + .Dock = Windows.Forms.DockStyle.Fill + } + + AddHandler _lookupControlSingle.ButtonClick, AddressOf lookupControlSingle_ButtonClick + + Controls.Add(_lookupControlSingle) + Else + _lookupControlMulti = New GridLookUpEdit With { + .Dock = Windows.Forms.DockStyle.Fill + } + + With _lookupControlMulti.Properties + .View.OptionsBehavior.ReadOnly = True + .View.OptionsBehavior.Editable = False + .View.OptionsView.ShowColumnHeaders = False + .PopupFormSize = New System.Drawing.Size(.PopupFormSize.Width, 100) + End With + + _lookupControlMulti.Properties.Buttons.Add(New EditorButton() With { + .Kind = ButtonPredefines.Ellipsis, + .Tag = TAG_BUTTON_LOOKUP_FORM + }) + + AddHandler _lookupControlMulti.ButtonClick, AddressOf lookupControlMulti_ButtonClick + AddHandler _lookupControlMulti.EditValueChanging, AddressOf lookupControlMulti_EditValueChanging + + Controls.Add(_lookupControlMulti) + End If + End Sub + + Private Sub lookupControlMulti_EditValueChanging(sender As Object, e As ChangingEventArgs) + e.Cancel = True + End Sub + + Private Sub lookupControlMulti_ButtonClick(sender As Object, e As ButtonPressedEventArgs) + If e.Button.Tag <> TAG_BUTTON_LOOKUP_FORM Then + Exit Sub + End If + + Dim oForm As frmLookupGrid = GetLookupForm() + Dim oResult = oForm.ShowDialog() + + If oResult = Windows.Forms.DialogResult.OK Then + Dim oValues = oForm.SelectedValues + _lookupControlMulti.Properties.DataSource = oValues + _lookupControlMulti.Properties.NullText = IIf(oValues.Count = 0, TEXT_NO_RECORDS, String.Format(TEXT_N_RECORDS, oValues.Count)) + + SelectedValues = oValues + End If + + oForm.Dispose() + End Sub + + Private Sub lookupControlSingle_ButtonClick(sender As Object, e As ButtonPressedEventArgs) + Dim oForm As frmLookupGrid = GetLookupForm() + Dim oResult = oForm.ShowDialog() + + If oResult = Windows.Forms.DialogResult.OK Then + Dim oValues = oForm.SelectedValues + _lookupControlSingle.Text = oValues.FirstOrDefault() + + SelectedValues = oValues + End If + + oForm.Dispose() End Sub End Class diff --git a/LookupGrid/frmLookupGrid.vb b/LookupGrid/frmLookupGrid.vb index ba0fd778..e0beba62 100644 --- a/LookupGrid/frmLookupGrid.vb +++ b/LookupGrid/frmLookupGrid.vb @@ -137,6 +137,9 @@ Public Class frmLookupGrid Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click SelectedValues.Clear() + + DialogResult = DialogResult.Cancel + Close() End Sub Private Sub gridLookup_KeyUp(sender As Object, e As KeyEventArgs) Handles gridLookup.KeyUp