leocb / MaterialSkin

Theming .NET WinForms, C# or VB.Net, to Google's Material Design Principles.
MIT License
438 stars 132 forks source link

Toggling MaterialTextBox2's Enabled Property Doesn't Propagate to the Inner Control #289

Closed VolatilePulse closed 2 years ago

VolatilePulse commented 2 years ago

One of the scenarios at work is using a switch to toggle between using a local DB or server DB. If switching to a server DB, the user is required to provide a connection string which set's the text box's enable state to true. The outer control can receive focus, but the inner text box is still disabled.

Starting Disabled State with a Text value: image

Toggled Enabled State with a Text Value: image

If the control starts enabled, everything works as expected at first. Once the Enabled state is toggled to false, you get the first image. Toggling the Enabled state to true again will get you the second image.

valimaties commented 2 years ago

Sorry @VolatilePulse , I have tried your scenario and mine works as expected. Did you inherit MaterialTextBox2 in a new class? Do you have some extra code inside that class to check for textbox's value? Maybe there is a problem...

Demo: starting disabled, switched to enabled and everything works fine... MaterialTextbox2EnabledChangeIssue

orapps44 commented 2 years ago

I can't reproduce that also. Could you make a test project and share it ?

VolatilePulse commented 2 years ago

This shows two issues, the inner control stays disabled while the outer control is enabled and the Hint text is overlapped by the Text value.

using MaterialSkin.Controls;
using System;
using System.Windows.Forms;

namespace WinFormsApp1
{
    internal static class Program
    {
        [STAThread]
        private static void Main()
        {
            Application.SetHighDpiMode(HighDpiMode.SystemAware);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new ExampleForm("Sample Text"));
        }
    }

    public partial class ExampleForm : MaterialForm
    {
        public ExampleForm(string textBoxString)
        {
            InitializeComponent();
            ExampleTextBox.Text = textBoxString;
        }

        public MaterialTextBox2 ExampleTextBox = new();
        public MaterialSwitch ExampleSwitch = new();

        private void InitializeComponent()
        {
            ClientSize = new System.Drawing.Size(800, 450);
            Text = "Example Form";

            ExampleTextBox.Hint = "Test";
            ExampleTextBox.Text = string.Empty;
            ExampleTextBox.Enabled = false;
            ExampleTextBox.Location = new System.Drawing.Point(32, 100);
            ExampleTextBox.Size = new System.Drawing.Size(200, 60);

            ExampleSwitch.Text = "TextBox Enabled";
            ExampleSwitch.Location = new System.Drawing.Point(264, 100);
            ExampleSwitch.Size = new System.Drawing.Size(200, 20);
            ExampleSwitch.Click += ExampleSwitch_Click;

            Controls.Add(ExampleTextBox);
            Controls.Add(ExampleSwitch);
        }

        private void ExampleSwitch_Click(object? sender, EventArgs e) => ExampleTextBox.Enabled = ExampleSwitch.Checked;
    }
}
valimaties commented 2 years ago

Hi @VolatilePulse , I've tried your code and works fine for me for Enabled issue. Yes, there is a problem with baseTextBox position when Hint is visible and not empty.

Edit: The problem is from initialization of control in your example, because the text is initially set as string.Empty, and Hint is not empty, and the Text value is set after form initialization. But calling UpdateRects() method on the overridden Text property of MaterialTextBox2, the location of baseTextBox will be set correctly.

Demo: VolatileIssue

valimaties commented 2 years ago

Hi @VolatilePulse Try my PR #292 and see if it solve your problem. See my Edit from post above, too...

Demo: VolatileIssue_PRSolve

orapps44 commented 2 years ago

Overlapping fix has been merged into master. Could you try again and tell us if everything is fixed ?

VolatilePulse commented 2 years ago

Part of the confusion was because I was using an older commit. I misunderstood the usage of git submodule. After pulling the latest version, all issues have been resolved. Thank you.