雪人洞卡鲁拉:VB学习之路
来源:百度文库 编辑:九乡新闻网 时间:2024/04/30 13:46:26
VB常见问题点
1、 字符串和变量的拼接问题:
我们知道在C#中使用“+”可以把字符串和变量进行拼接。那在VB中要实现同样的效果需要怎样处理呢
使用特殊字符“&”可以解决此问题。看例题:
Dim addition As String =" and "& field & "=@" & field& " order by " & field& " " & order (field和order为变量)
说明:"&的作用是和前面的字符串拼接:" " & order
&"的作用是和后面的字符串拼接:order & " "
If txtCondition.Text <> "" Then '当查询条件不为空时增加相应的查询条件
' addition = String.Format(" and {0}='{1}' order by {0} {2}",field, txtCondition.Text.Trim(), order)
addition = " and " & field & "=@" & field & " order by " & field & " " & order
order = "" & order
order = order & ""
End If
注意:“&”与变量之间需要空格间隔开来。
2、 多行字符串的拼接问题:
当我们的sql语句很长而不得不按Enter进行换行,这时字符串的结果就发生了变化,系统无法自动进行识别。这时我们需要采用特殊字符 “& _”来处理。
Dim sqlstr As String = "selectcustID, custNum as 客户编号,custName as 客户名称,custLink as 联系人,phone as 联系电话," & _
"address as 公司地址,custaddress as 客户住址,email as 邮箱,recorduseras 记录人,recorddate as 记录时间,updateuser as 更新人," & _
"updatedateas 更新时间,custtype as 客户类型,currnum as 交易币别 from customer_test where 1=1 "
注意:两个字符“& _”之间需要空格间隔开来。
3、 时间格式转换问题:
在c#中的时间转换好像使用Format不是很复杂,在VB中就没有那么简单!看例题:
IfIsDate(cfg.Rows(cfg.Row).Item(9).ToString()) Then
recorddate = Format(CDate(cfg.Rows(cfg.RowSel).Item(9).ToString()), "yyyy-MM-dd HH:mm:ss")
End If
说明:IsDate ()是VB系统中的函数,功能是判断一个对象是否为日期返回一个Boolean值。在对一个对象进行格式化(Format)之前,需要
转换数据类型,此处字符串为时间数据类型所以采用CDate()进行转化。其中"yyyy-MM-ddHH:mm:ss"为转化之后的数据格式。月为大写M,24小时制为大写H。
4、 Sql语句中的单引号的处理问题:
由于sql语句本身的语法特点对输入的外部数据都是采用单引号或者双引号,所以当用户输入的值本身为单引号或双引号时就会对sql语句的结构造成冲突以致产生歧义。
Dim sql As String = " select *from customer where custName=' ' and custNo=' ' "当我们输入的值为两个“'”时整个sql语句的意思完全变了。
解决方案:使用SqlParameter
select custID, custNum as 客户编号,custNameas 客户名称,custLink as 联系人,phone as 联系电话,address as 公司地址,custaddressas 客户住址,email as 邮箱,recorduser as 记录人,recorddate as 记录时间,updateuser as 更新人,updatedate as 更新时间,custtype as 客户类型,currnum as 交易币别 from customer_test where1=1 and custName=@custName order by custName整个过程都不需要处理单引号的问题。
sqlcommand.parameters.Add(New sqlParameter(“@custName”, SqlDbType.NVarChar, 50)).value= txt.text.toTrim()。sqlParameter可以起到类型约束、长度限制的作用,并且回避了单引号做为值传入sql语句后改变sql语句结构的问题。
5、 如何判断文本框中输入的是汉字、字母、数字?(此例题只判断字母和数字,没有处理汉字啊)
解决方案:因为汉字、字母、数字的ASCII码落在65~90和97~122两个区间里面,所以采用判断ASCII码的方式比较简便。
在文本框改变事件里面实现相应的代码功能如下:
Private Sub TextBox1_TextChanged(ByValsender As System.Object, ByVal e AsSystem.EventArgs) Handles TextBox1.TextChanged
Dim i As Integer
Dim a As Integer
If TextBox1.Text.Length> 0 Then
Fori = 1 To TextBox1.Text.Length
a = Asc(Mid(TextBox1.Text, i,1))
SelectCase a
Case Is< 48, Is > 57 '数字0~9
Select Case a
CaseIs < 65, 91 To96, Is > 122 'A_Z a_z
MsgBox("非法字符")
TextBox1.Text =TextBox1.Text.Remove(i - 1, 1)
End Select
EndSelect
Next
End If
End Sub
说明:需要掌握好Select Case… Case Is… End Select 的用法,(此处嵌套了两个Select Case)以及区间之间的交集问题、Mid()函数、Asc()函数!
6、 繁简转化
7、 窗体之间的传值方式
需求场景:子窗体的Button按键要影响到主窗体的Flex Grid控件中的内容(通过实例化一个主窗体的方法达不到要求,因为得不到原始那个主窗体,实例化之后的对象为一个复制品。)
解决方案:我们在操作主窗体过程中,在实例化子窗体的时候把主窗体或主窗体的某个控件作为参数传递过去。然后在子窗体内部的构造函数New()中用相应的参数进行接收。这样一来子窗体在实例化的过程中就可以把主窗体的控件作为实参导入的子窗体内部进行操作。这过程也体现了引用类型的思想精髓!A传递到B,B传递到C,那么对C的操作等同于对A的操作。
代码部分:
主窗体
Private SubtsmiSearch_Click(ByVal sender As System.Object, ByVale As System.EventArgs) HandlestsmiSearch.Click
Dim searchForm As SearchForm = NewSearchForm(cfg, Me) '此處實例化的過程中傳遞一個flexGrid控件和主窗体本身
searchForm.ShowDialog()
End Sub
子窗体
Dim vsgrid As C1.Win.C1FlexGrid.C1FlexGrid '申明一个子窗体内部的flexgrid控件,在子窗体实例化时与外部传入的flexgrid进行对接
Dim mForm As System.Windows.Forms.Form '申明一个Form控件
Public Sub New(ByVal grid As C1.Win.C1FlexGrid.C1FlexGrid, ByVal mainForm AsSystem.Windows.Forms.Form)
' This callis required by the Windows Form Designer.
InitializeComponent()
vsgrid =grid '把主窗体和子窗体的flexgrid联系起来
mForm =mainForm '把主窗体和子窗体的form联系起来
' Add any initialization after the InitializeComponent() call.
End Sub
说明:通过这样一种处理模式才能保证在子窗体里面操作vsgrid就是在操作主窗体的cfg,保证了flex grid控件中数据的同步性!
8、