Databinding TextBoxu - změna datasource při změně textboxu a ne až po změně focusu   otázka

WinForms

Dobrý den,

zkoušel jsem googlit, ale bezvýsledně.

Mám 400 instancí objektu s 20 vlastnostmi

Chtěl bych mít ale pouze jeden (dva tři) uživatelsky aktivní.

Uživatel vyplňuje nějaký formulář a mění se např. 358 instance

Já bych ale velice rád, pokud by se ta instance měnila okamžitě, když měním například textbox a ne až po tom, co ztratí focus.

Mohl by mě někdo někam nasměrovat?

Případně řešit jiným způsobem, než databindingem?

Jednoduchý příklad:

Imports System.Runtime.CompilerServices
Imports System.ComponentModel

Public Class Form1

    Dim WithEvents TXT As Binding
    Dim WithEvents CHb As Binding

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        TXT = New Binding("Text", UserControl11, "Textik")
        TextBox1.DataBindings.Clear()
        TextBox1.DataBindings.Add(TXT)
        TextBox1.DataBindings.DefaultDataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged

        CHb = New Binding("Checked", UserControl11, "Checknuto")
        CheckBox1.DataBindings.Clear()
        CheckBox1.DataBindings.Add(CHb)
        CheckBox1.DataBindings.DefaultDataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        TXT = New Binding("Text", UserControl12, "Textik")
        TextBox1.DataBindings.Clear()
        TextBox1.DataBindings.Add(TXT)
        TextBox1.DataBindings.DefaultDataSourceUpdateMode = DataSourceUpdateMode.OnValidation

        CHb = New Binding("Checked", UserControl12, "Checknuto")
        CheckBox1.DataBindings.Clear()
        CheckBox1.DataBindings.Add(CHb)
        CheckBox1.DataBindings.DefaultDataSourceUpdateMode = DataSourceUpdateMode.OnValidation

    End Sub

    Public Sub New()
        InitializeComponent()
    End Sub
End Class




Public Class UserControl1
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents Label4 As System.Windows.Forms.Label


    Property Textik As String
        Get
            Return Label2.Text
        End Get
        Set(value As String)
            Label2.Text = value
        End Set
    End Property

    Property Checknuto As Boolean
        Get
            If Label4.Text = "True" Then
                Return True
            Else
                Return False
            End If
        End Get
        Set(value As Boolean)
            If value = True Then
                Label4.Text = "True"
            Else
                Label4.Text = "False"
            End If
        End Set
    End Property
End Class

Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.TextBox1 = New System.Windows.Forms.TextBox()
        Me.CheckBox1 = New System.Windows.Forms.CheckBox()
        Me.Button1 = New System.Windows.Forms.Button()
        Me.Button2 = New System.Windows.Forms.Button()
        Me.UserControl12 = New DatabindingPOKUS.UserControl1()
        Me.UserControl11 = New DatabindingPOKUS.UserControl1()
        Me.SuspendLayout()
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(47, 53)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(100, 20)
        Me.TextBox1.TabIndex = 0
        '
        'CheckBox1
        '
        Me.CheckBox1.AutoSize = True
        Me.CheckBox1.Location = New System.Drawing.Point(47, 79)
        Me.CheckBox1.Name = "CheckBox1"
        Me.CheckBox1.Size = New System.Drawing.Size(81, 17)
        Me.CheckBox1.TabIndex = 1
        Me.CheckBox1.Text = "CheckBox1"
        Me.CheckBox1.UseVisualStyleBackColor = True
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(213, 17)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(75, 23)
        Me.Button1.TabIndex = 2
        Me.Button1.Text = "Button1"
        Me.Button1.UseVisualStyleBackColor = True
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(294, 17)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(75, 23)
        Me.Button2.TabIndex = 3
        Me.Button2.Text = "Button2"
        Me.Button2.UseVisualStyleBackColor = True
        '
        'UserControl12
        '
        Me.UserControl12.Checknuto = False
        Me.UserControl12.Location = New System.Drawing.Point(430, 142)
        Me.UserControl12.Name = "UserControl12"
        Me.UserControl12.Size = New System.Drawing.Size(200, 74)
        Me.UserControl12.TabIndex = 5
        Me.UserControl12.Textik = "..."
        '
        'UserControl11
        '
        Me.UserControl11.Checknuto = False
        Me.UserControl11.Location = New System.Drawing.Point(430, 22)
        Me.UserControl11.Name = "UserControl11"
        Me.UserControl11.Size = New System.Drawing.Size(200, 74)
        Me.UserControl11.TabIndex = 4
        Me.UserControl11.Textik = "..."
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(652, 262)
        Me.Controls.Add(Me.UserControl12)
        Me.Controls.Add(Me.UserControl11)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.CheckBox1)
        Me.Controls.Add(Me.TextBox1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents CheckBox1 As System.Windows.Forms.CheckBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents UserControl11 As DatabindingPOKUS.UserControl1
    Friend WithEvents UserControl12 As DatabindingPOKUS.UserControl1

End Class
Partial Class UserControl1
    Inherits System.Windows.Forms.UserControl

    'UserControl overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    Public Sub New()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.Label3 = New System.Windows.Forms.Label()
        Me.Label4 = New System.Windows.Forms.Label()
        Me.SuspendLayout()
        '
        'Label1
        '
        Me.Label1.AutoSize = True
        Me.Label1.Location = New System.Drawing.Point(47, 16)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(28, 13)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "Text"
        '
        'Label2
        '
        Me.Label2.AutoSize = True
        Me.Label2.Location = New System.Drawing.Point(117, 16)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(16, 13)
        Me.Label2.TabIndex = 1
        Me.Label2.Text = "..."
        '
        'Label3
        '
        Me.Label3.AutoSize = True
        Me.Label3.Location = New System.Drawing.Point(25, 44)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(50, 13)
        Me.Label3.TabIndex = 2
        Me.Label3.Text = "Checked"
        '
        'Label4
        '
        Me.Label4.AutoSize = True
        Me.Label4.Location = New System.Drawing.Point(117, 44)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(32, 13)
        Me.Label4.TabIndex = 3
        Me.Label4.Text = "False"
        '
        'UserControl1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.Controls.Add(Me.Label4)
        Me.Controls.Add(Me.Label3)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.Label1)
        Me.Name = "UserControl1"
        Me.Size = New System.Drawing.Size(200, 74)
        Me.ResumeLayout(False)
        Me.PerformLayout()
    End Sub
End Class
nahlásit spamnahlásit spam 0 odpovědětodpovědět

Nechce se mi tím prokousávat, tak jen z úvodu dotazu.

Pokud chcete, aby se zdroj bindingu měnil při např. změně textu v textboxu:

public partial class Form1 : Form
{

    private Model_model1;
    private Binding _b;


    public Form1()
    {
        InitializeComponent();

        //instance zdroje pro binding
        _model1 = new Model();

        //inicializace bindingu důležité je: DataSourceUpdateMode.OnPropertyChanged
        _b = new Binding("Text", _model1, "Text", true, DataSourceUpdateMode.OnPropertyChanged);

        //binding textboxu
        textBox1.DataBindings.Add(_b);
    }
}

public class Model
{
    private string _text;

    public string Text
    {
        get { return _text; }
        set
        {
            _text = value;
            Debug.Print("Text changed");
        }
    }
}
nahlásit spamnahlásit spam 1 / 1 odpovědětodpovědět
                       
Nadpis:
Antispam: Komu se občas házejí perly?
Příspěvek bude publikován pod identitou   anonym.
  • Administrátoři si vyhrazují právo komentáře upravovat či mazat bez udání důvodu.
    Mazány budou zejména komentáře obsahující vulgarity nebo porušující pravidla publikování.
  • Pokud nejste zaregistrováni, Vaše IP adresa bude zveřejněna. Pokud s tímto nesouhlasíte, příspěvek neodesílejte.

přihlásit pomocí externího účtu

přihlásit pomocí jména a hesla

Uživatel:
Heslo:

zapomenuté heslo

 

založit nový uživatelský účet

zaregistrujte se

 
zavřít

Nahlásit spam

Opravdu chcete tento příspěvek nahlásit pro porušování pravidel fóra?

Nahlásit Zrušit

Chyba

zavřít

feedback