简体   繁体   English

如何将多个文本框与 ProgressBar 连接起来?

[英]How can I connect multiple TextBoxes with a ProgressBar?

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.我一周前刚开始使用编程语言 C#,并使用 Visual Studio 程序和 win Forms。我已经遇到了几天的问题。

I want to connect a ProgressBar to different TextBoxes.我想将 ProgressBar 连接到不同的 TextBoxes。 So that with each filled textBox the ProgressBar increases.这样随着每个填充的文本框 ProgressBar 增加。 When the text is removed, the progressBar should go down again.删除文本后,progressBar 应该再次下降 go。 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文本框是 Vorname,Nachname,PLZ,Wohnort,Hausnummer,Straße

ProgressBar is Fortschrittsanzeige ProgressBar 是 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;
    }
}

You can handle the TextChanged event on each TextBox like so您可以像这样处理每个 TextBox 上的 TextChanged 事件

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并在您的 class 中添加私有属性

private bool _textbox1IsEmpty = true;

You can make a function to optimize it and don't have duplicate code你可以制作一个 function 来优化它并且没有重复的代码

Here are a few tips to get you started with WinForms and make it easier to connect multiple TextBoxes with a ProgressBar .这里有一些提示,可帮助您开始使用 WinForms 并使将多个 TextBox 与 ProgressBar 连接起来变得更加容易。


  • The textboxes (and other controls) that are on a Form can be found in the Controls collection of the form.窗体上的文本框(和其他控件)可以在FormControls集合中找到。
  • 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.例如,在 Constructor 表单中,您可以通过所有文本框 go 并为每个文本框附加一个TextChanged处理程序。

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.多个文本框都可以指向一个共同的事件处理程序。
  • System.Linq reduces the amount of code needed for things like matching and sorting. System.Linq减少了匹配和排序等操作所需的代码量。

What we're able to do is perform a validation based on all the textboxes whenever any textbox changes.我们能够做的是在任何文本框发生变化时基于所有文本框执行验证。

进步

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 BackColor).
    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.处理程序允许“特殊情况”,如果更改的值不再有效,将使Fortschrittsanzeige go 倒退。

逆行


When all textboxes are valid hide Fortschrittsanzeige and enable Sendebutton .当所有文本框都有效时,隐藏Fortschrittsanzeige并启用Sendebutton

康普莱特

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM