Dim obj As Object, objName As String, i As Integer
創(chuàng)新互聯(lián)專注于東興網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供東興營銷型網(wǎng)站建設(shè),東興網(wǎng)站制作、東興網(wǎng)頁設(shè)計、東興網(wǎng)站官網(wǎng)定制、小程序制作服務(wù),打造東興網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供東興網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
i = 0
For Each obj In Me.Controls '枚舉窗體所有控件
Debug.Print TypeName(obj)
If TypeName(obj) = "TextBox" Then '判讀是否為文本框
i = i + 1
objName = obj.Name '獲取文本控件名稱,這里可以根據(jù)名稱來判讀賦值,也可以根據(jù)其他屬性復(fù)制
obj.Text = "文本" i '文本框賦值
End If
Next
SqlConnection myconn = new SqlConnection(server=(local),uid=sa,pwd=sa,database=db);//連接數(shù)據(jù)庫語句自己修改
myconn.Open();
SqlCommand mycmd = new SqlCommand();
mycmd.CommandText = "insert into abd(d,e,f) values (@a,@b,@c)";
mycmd.Parameters.AddWithValue("@a", a.Text);
mycmd.Parameters.AddWithValue("@b", b.Text);
mycmd.Parameters.AddWithValue("@c", c.Text);
mycmd.Connection = myconn;
mycmd.ExecuteNonQuery();
myconn.Close();
是這個辦法的,只是要加一句:
TextBox1.SelectionLength = 0
TextBox1.SelectionStart = TextBox1.Text.Length
TextBox1.ScrollToCaret()
另外,文本框的內(nèi)容修改后要再次執(zhí)行上面的三句代碼
Dim mycon As New SqlConnection(connstrs)
Dim sqlStr As String = "SELECT * FROM [Column] FROM [Table]"
Dim cmd As New SqlCommand(sqlStr, mycon)
Dim reader = cmd.ExecuteReader()
Dim i As Integer = 1
While reader.Read()
Dim txt As New TextBox()
txt.Name = "txt" + i
txt.Size = New Size(100, 20) '文本框大小
txt.Location = New Point(50, i * 20 + 20)'left:50 top:隨便寫的,自己看著調(diào)
txt.Text = reader.GetValue(0).ToString()
Me.Controls.Add(txt) 'Me 可以改為你要添加上去的對象
End While
'未經(jīng)過運行,自己調(diào)試看看能否OK
1、首先,點擊保存按鈕后應(yīng)檢查文本框內(nèi)容。
2其次,如果在文本框4中輸入的值以400開頭則保存反之更改為400。
3、最后,再次檢查文本框5如果文本框5中的值為29或31則保存反之則更改即可。
呵呵,就當(dāng)練手了
Add_VB.aspx 代碼
%@ Page Language="VB" AutoEventWireup="false" CodeFile="Add_VB.aspx.vb" Inherits="測試代碼_Add_VB" %
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""
html xmlns=""
head runat="server"
title實現(xiàn)加運算/title
/head
body
form id="form1" runat="server"
div
asp:TextBox ID="TextBox1" runat="server" Width="66px"/asp:TextBox
asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="必須為數(shù)字" ValidationExpression="(\d+(\.\d)?){1,1}"/asp:RegularExpressionValidator
br /
asp:TextBox ID="TextBox2" runat="server" Width="66px"/asp:TextBox
asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ControlToValidate="TextBox2" ErrorMessage="必須為數(shù)字" ValidationExpression="\d+(\.\d)?"/asp:RegularExpressionValidator
br /
asp:TextBox ID="TextBox3" runat="server" Width="66px"/asp:TextBox
asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" ControlToValidate="TextBox3" ErrorMessage="必須為數(shù)字" ValidationExpression="\d+(\.\d)?"/asp:RegularExpressionValidator
br /
asp:TextBox ID="TextBox4" runat="server" Width="66px"/asp:TextBox
asp:RegularExpressionValidator ID="RegularExpressionValidator4" runat="server" ControlToValidate="TextBox4" ErrorMessage="必須為數(shù)字" ValidationExpression="\d+(\.\d)?"/asp:RegularExpressionValidator
br /
asp:TextBox ID="TextBox5" runat="server" ReadOnly="True"/asp:TextBox
asp:Button ID="Button1" runat="server" Text="求和" Width="78px" /
/div
/form
/body
/html
Add_VB.aspx.vb 代碼
Partial Class 測試代碼_Add_VB
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim a As Double = TextBox1.Text
Dim b As Double = TextBox2.Text
Dim c As Double = TextBox3.Text
Dim d As Double = TextBox4.Text
TextBox5.Text = a + b + c + d
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If TextBox1.Text = "" Then
TextBox1.Text = 0
End If
If TextBox2.Text = "" Then
TextBox2.Text = 0
End If
If TextBox3.Text = "" Then
TextBox3.Text = 0
End If
If TextBox4.Text = "" Then
TextBox4.Text = 0
End If
End Sub
End Class