如何使用TextBox更改ProgressBar值

时间:2017-03-27 14:15:20

标签: c# vb.net

我一直在使用ProgressBar,我想知道如何使用ProgressBar更改TextBox的值。因此,如果您在TextBox中键入0到99之间的值,然后按Enter键,则ProgressBar的值会更改为您输入的值。

我该怎么做? VS说TextBox1.Text不能用作方法。

这是我目前的代码:

if (e.KeyCode == Keys.Enter)
        {
            progressBar1.Value = textBox1.Text();
        }

我试过这个(这不起作用):

int i = Convert.ToInt32(textBox1.Text);
        i = 1;
if (e.KeyCode == Keys.Enter)
        {
            progressBar1.Value = textBox1.Text();
        }

在VB.NET中,我只需输入以下代码,然后就可以了:

If e.KeyCode = Keys.Enter Then
        ProgressBar1.Value = Val(TextBox1.Text)
End If

有谁知道如何解决这个问题?另外,我怎么能说c#val(something)

是否可以在C#中使用我在VB.NET中使用的相同代码?

2 个答案:

答案 0 :(得分:0)

因为它不是方法,而是属性!所以只是

progressBar1.Value = Int32.Parse(textBox1.Text);

与班级相关的方法:

objectOfSomeClass.MethodName();

班级属性:

objectOfSomeClass.propertyName;

要了解的重要事项:
当您将鼠标移到变量上时,您将看到此元素的类型。如果你到目前为止,我知道你知道什么类型:) 每个对象都可以具有属性和方法。像textbox一样,字符串属性称为“Text”。对象还可以包含方法。要使用方法,您需要括号,因为其中一些可能会处理参数:)

第二个问题:
将其添加到表单(您定义button_click等事件的位置):

private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
    if(e.keyCode == ...
}

检查这个 https://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress(v=vs.110).aspx

答案 1 :(得分:0)

int i = Convert.ToInt32(textBox1.Text);
if (e.KeyCode == Keys.Enter)
        {
            progressBar1.Value = i;
        }

删除text1.text并放入i。你的问题会解决。