Home > Software design >  How can I connect multiple TextBoxes with a ProgressBar?
How can I connect multiple TextBoxes with a ProgressBar?

Time:02-01

I just started with the programming language C# a week ago and used the program Visual Studio with win Forms. I've had a problem for a few days.

I want to connect a ProgressBar to different TextBoxes. So that with each filled textBox the ProgressBar increases. When the text is removed, the progressBar should go down again. So far I've only managed to get the progressBar to increase in general or that the progress bar increases with each letter in a textBox.

Textboxes are Vorname,Nachname,PLZ,Wohnort,Hausnummer,Straße

ProgressBar is Fortschrittsanzeige

private void button1_Click(object sender, EventArgs e)
{

    Fortschrittsanzeige.Dock = DockStyle.Bottom;
    Fortschrittsanzeige.Maximum = 60;
    Fortschrittsanzeige.Minimum = 0;
    Fortschrittsanzeige.Style = ProgressBarStyle.Continuous;


    if  (
        Vorname.Text.Length <= 0 ||
        Nachname.Text.Length <= 0 ||
        PLZ.Text.Length < 4 ||
        Wohnort.Text.Length <= 0 ||
        Hausnummer.Text.Length <= 0 ||
        Straße.Text.Length <= 0
    )
    {
        textBox7.Text = ("Bitte überprüfe deine Eingabe");
    }
    else
    {
        Sendebutton.Text = "Gesendet";
        textBox7.Text = "Vielen Dank"   Vorname.Text   " "   Nachname.Text   ", wir
        haben deine Daten erhalten.";
    }

    if (Vorname.Text.Length <= 0)
    {
        Vorname.BackColor = Color.IndianRed;
    }
    else
    {
    Vorname.BackColor = Color.White;
    Fortschrittsanzeige.Value  = 10;
    }

    if (Nachname.Text.Length <= 0)
    {
        Nachname.BackColor = Color.IndianRed;
    }
    else
    {
        Nachname.BackColor = Color.White;
        Fortschrittsanzeige.Step  = 10;
    }

    if (PLZ.Text.Length < 4)
    {
        PLZ.BackColor = Color.IndianRed;
    }
    else
    {
        PLZ.BackColor = Color.White;
        Fortschrittsanzeige.Step  = 10;
    }

    if (Wohnort.Text.Length <= 0)
    {
        Wohnort.BackColor = Color.IndianRed;
    }

    else
    {
        Wohnort.BackColor = Color.White;
        Fortschrittsanzeige.Step  = 10;
    }

    if (Hausnummer.Text.Length <= 0)
    {
        Hausnummer.BackColor = Color.IndianRed;
    }

    else
    {
        Hausnummer.BackColor = Color.White;
        Fortschrittsanzeige.Step  = 10;
    }

    if (Straße.Text.Length <= 0)
    {
        Straße.BackColor = Color.IndianRed;
    }

    else
    {
        Straße.BackColor = Color.White;
        Fortschrittsanzeige.Step  = 10;
    }
}

CodePudding user response:

You can handle the TextChanged event on each TextBox like so

private void textBox1_TextChanged(object sender, EventArgs e)
    {
        if (textBox1.Text.Length > 0 && _textbox1IsEmpty)
        {
            progressBar1.Value  = 10;
            _textbox1IsEmpty = false;
        }
        else if (textBox1.Text.Length <= 0)
        {
            progressBar1.Value -= 10;
            _textbox1IsEmpty = true;
        }

    }

and add a private property in your class

private bool _textbox1IsEmpty = true;

You can make a function to optimize it and don't have duplicate code

CodePudding user response:

Here are a few tips to get you started with WinForms and make it easier to connect multiple TextBoxes with a ProgressBar.


  • The textboxes (and other controls) that are on a Form can be found in the Controls collection of the form.
  • All of the textboxes on the form can be obtained with a simple query.

For example, in the form Constructor you could go though all the textboxes and attach a TextChanged handler to each.

public MainForm()
{
    InitializeComponent();
    foreach (TextBox textBox in Controls.OfType<TextBox>()) 
    {
        textBox.TextChanged  = onAnyTextChanged;
        onAnyTextChanged(textBox, EventArgs.Empty); // Initialize
    }
    ActiveControl = Fortschrittsanzeige;
}

  • Multiple text boxes can all point to a common event handler.

What we're able to do is perform a validation based on all the textboxes whenever any textbox changes.

progress

const int TEXTBOX_COUNT = 6;
private void onAnyTextChanged(object? sender, EventArgs e)
{
    if(sender is TextBox textbox)
    {
        bool isValid;
        if(textbox.PlaceholderText == "PLZ")
        {
            isValid = textbox.TextLength > 3;
        }
        else
        {
            isValid = !string.IsNullOrWhiteSpace(textbox.Text);
        }
        textbox.BackColor = isValid ? Color.White : Color.LightSalmon;
    }

    // Use System.Linq to count the number of valid
    // textboxes based on the BackColor set;
    float countValid = Controls.OfType<TextBox>().Count(_=>_.BackColor== Color.White);

    var pct = countValid / TEXTBOX_COUNT;
    Fortschrittsanzeige.Value = (int)(pct * Fortschrittsanzeige.Maximum);
    Sendebutton.Enabled = countValid.Equals(TEXTBOX_COUNT);
    Fortschrittsanzeige.Visible = !Sendebutton.Enabled;        
}

The handler allows for "special cases" and will make the Fortschrittsanzeige go backwards if the changed value is no longer valid.

retrograde


When all textboxes are valid hide Fortschrittsanzeige and enable Sendebutton.

complete

  • Related