简体   繁体   English

如何在c#中创建自定义进度条

[英]How to make a customized progressbar in c#

I would like to learn how to make a progressbar like the one shown in this video. 我想学习如何制作像本视频中所示的进度条。

I tried to replicate it in VS C#, but I get the error: 我试图在VS C#中复制它,但是我得到了错误:

C# Property or indexer cannot be assigned to -- it is read only C#属性或索引器无法分配 - 它是只读的

If I try using if (txProgressBar.Text.Length == 85) , I will get this in the TextBox (txProgressBar) 如果我尝试使用if (txProgressBar.Text.Length == 85) ,我将在TextBox(txProgressBar)中得到它

System.Windows.Forms.TextBox, Text: System.Windows.Forms.TextBox, Text: Syst...██ System.Windows.Forms.TextBox,Text:System.Windows.Forms.TextBox,Text:Syst ...██

Textbox Progressbar Tutorial VB 2010 Textbox Progressbar Tutorial VB 2010

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CustomizedProgressBar
{
    public partial class Form1 : Form
    {
        int last = 1;

        public Form1()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (txProgressBar.Text.Length = "85")
            {

                timer1.Stop();
                MessageBox.Show("Counted!");

            }else
            {
                if (last == 1)
                {
                    txProgressBar.Text = txProgressBar + "█";
                    last = 2;
                }
                else
                {
                    txProgressBar.Text = txProgressBar.Text + "█";
                    last = 1;
                }
            }

        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txProgressBar.Text = "";
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            timer1.Start();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            timer1.Stop();
        }
    }
}

Your line: 你的路线:

txProgressBar.Text = txProgressBar + "█";

should be 应该

txProgressBar.Text = txProgressBar.Text + "█"; or txProgressBar.Text &= "█"; 或者txProgressBar.Text &= "█";

I realized what was the problem. 我意识到问题是什么。 The txProgressBar.Text = txProgressBar + "█"; txProgressBar.Text = txProgressBar +“█”; was missing the *.Text. 缺少* .Text。 That solved the problem and the message is shown as well. 这解决了问题并且也显示了消息。

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

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