我有一个包含许多文本框的网页。现在我需要遍历它们,总是先输入第二个和第二个输入,计算并将输出放到第三个和第四个... 5 + 6输入7 + 8输出等。
逻辑不是问题,仅使用例如:
for(i=1;i=<13/4;i+4)
现在我更多的是C#程序员,但这是旧项目。我得到了,代码在VB.net。现在,我必须在VB.net中编写它。所以,任何人都可以帮助我或指出我去哪里,为了成功而阅读什么?
编辑,C#伪代码:
protected void Button1_Click(object sender, EventArgs e)
{
int i;
for(i=1;i<=13;i+=4)
{
double value1 = Convert.ToDouble("TextBox" + i.ToString().Text);
double value2 = Convert.ToDouble("TextBox" + (i+1).ToString().Text);
double value3 = value1 + value2;
("txtTotalAmount" + (i+2).ToString().Text = (value1 + value2).ToString();
("txtPercent" +(i+3).ToString().Text = (value1/value3 * 100).ToString();
}
}
VB转换器输出:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim i As Integer
For i = 1 To 13 Step 4
Dim value1 As Double = Convert.ToDouble("TextBox" & i.ToString().Text)
Dim value2 As Double = Convert.ToDouble("TextBox" & (i + 1).ToString().Text)
Dim value3 As Double = value1 + value2
(__InlineAssignHelper("txtTotalAmount" & (i + 2).ToString().Text, (value1 + value2).ToString()))
(__InlineAssignHelper("txtPercent" & (i + 3).ToString().Text, (value1 / value3 * 100).ToString()))
Next
End Sub
编辑:最后我做到了这样,它的确有效:
Protected Sub btCalculate_Click(sender As Object, e As EventArgs) Handles btCalculate.Click
Dim tb1 As TextBox
Dim tb2 As TextBox
Dim tb3 As TextBox
Dim i As Integer
For i = 37 To 53 Step 4
Dim value1 As String = "textbox" + i.ToString()
Dim value2 As String = "textbox" + (i+1).ToString()
Dim value3 As String = "textbox" + (i+2).ToString()
tb1 = PlaceHolder1.FindControl(value1)
tb2 = PlaceHolder1.FindControl(value2)
tb3 = PlaceHolder1.FindControl(value3)
tb3.Text = Integer.Parse(tb1.Text) + Integer.Parse(tb2.text)
Next
End Sub
PlaceHolder1的位置
<asp:PlaceHolder ID="PlaceHolder1" runat="server">
感谢大家的帮助。
答案 0 :(得分:1)
我更正了你的c#代码,将它们用于你的工作。
protected void Button1_Click(object sender, EventArgs e)
{
int i;
for (i = 1; i <= 13; i += 4)
{
double value1 = Convert.ToDouble(((TextBox)this.FindControl("TextBox" + i.ToString())).Text);
double value2 = Convert.ToDouble(((TextBox)this.FindControl("TextBox" + (i+1).ToString())).Text);
double value3 = value1 + value2;
TextBox txt = (TextBox)this.FindControl(("txtTotalAmount" + (i + 2).ToString()));
txt.Text = (value1 + value2).ToString();
TextBox txt1 = (TextBox)this.FindControl(("txtPercent" + (i + 3).ToString()));
txt1.Text = (value1 / value3 * 100).ToString();
}
}
答案 1 :(得分:0)
double value1 = Convert.ToDouble("TextBox" + i.ToString().Text);
不能在C#或VB中编译。这是你的意图吗?
Dim value1 As Double = Convert.ToDouble("TextBox" & i.ToString() & ".Text")
相应地修复其他事件,但我认为字符串不会转换为控件。
答案 2 :(得分:0)
只是一个刺,但你在寻找这个...... 我认为你需要调用实际控件,所以使用this.controls(或vb.net me.controls)。我已经为您重新创建了代码,希望这对您有所帮助。
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim i As Integer
For i = 1 To 13 Step 4
Dim value1 As Double = Convert.ToDouble(TryCast(Me.Controls(("TextBox" & i).ToString()), TextBox).Text)
Dim value2 As Double = Convert.ToDouble(TryCast(Me.Controls(("TextBox" & i + 1).ToString()), TextBox).Text)
Dim value3 As Double = value1 + value2
TryCast(Me.Controls(("txtTotalAmount" & (i + 2)).ToString()), TextBox).Text = (value1 + value2).ToString()
TryCast(Me.Controls(("txtPercent" & (i + 3)).ToString()), TextBox).Text = (value1 / value3 * 100).ToString()
' (__InlineAssignHelper("txtTotalAmount" & (i + 2).ToString().Text, (value1 + value2).ToString()))
' (__InlineAssignHelper("txtPercent" & (i + 3).ToString().Text, (value1 / value3 * 100).ToString()))
Next
End Sub
我已经将inlineassignhelper评论为tbh我对他们一无所知或他们做了什么!
我使用http://converter.telerik.com/转换为您提供C#代码。
protected void Button1_Click(object sender, EventArgs e)
{
int i;
for (i = 1; i <= 13; i += 4)
{
double value1 = Convert.ToDouble(this.Controls(("TextBox" + i).ToString()) as TextBox.Text);
double value2 = Convert.ToDouble(this.Controls(("TextBox" + i + 1).ToString()) as TextBox.Text);
double value3 = value1 + value2;
this.Controls(("txtTotalAmount" + (i + 2)).ToString()) as TextBox.Text = (value1 + value2).ToString();
this.Controls(("txtPercent" + (i + 3)).ToString()) as TextBox.Text = (value1 / value3 * 100).ToString();
}
}
//=======================================================
//Service provided by Telerik (www.telerik.com)
//Conversion powered by Refactoring Essentials.
//Twitter: @telerik
//Facebook: facebook.com/telerik
//=======================================================
H个 鸡
----编辑 添加了trycast到程序,我不明白为什么它会有所不同,但在看了一些其他论坛之后,其他人在根据字符串名称获取控件时遇到了类似问题。