DataGridView控件,放一個(gè)DataGridView1和Button1到窗體,下面是按鈕下代碼
建網(wǎng)站原本是網(wǎng)站策劃師、網(wǎng)絡(luò)程序員、網(wǎng)頁(yè)設(shè)計(jì)師等,應(yīng)用各種網(wǎng)絡(luò)程序開發(fā)技術(shù)和網(wǎng)頁(yè)設(shè)計(jì)技術(shù)配合操作的協(xié)同工作。創(chuàng)新互聯(lián)專業(yè)提供成都網(wǎng)站制作、成都網(wǎng)站建設(shè)、外貿(mào)營(yíng)銷網(wǎng)站建設(shè),網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站制作(企業(yè)站、響應(yīng)式網(wǎng)站建設(shè)、電商門戶網(wǎng)站)等服務(wù),從網(wǎng)站深度策劃、搜索引擎友好度優(yōu)化到用戶體驗(yàn)的提升,我們力求做到極致!
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.DataGridView1.AllowUserToAddRows = False
DataGridView1.RowTemplate.Height = 200
DataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None
For i = 1 To 3
Me.DataGridView1.Columns.Add("列" i.ToString, "列" i.ToString)
Me.DataGridView1.Rows.Add()
Next
Me.DataGridView1.Columns(0).Width = 100
Me.DataGridView1.Columns(1).Width = 500
Me.DataGridView1.Columns(0).Width = 300
End Sub
'自己設(shè)置相關(guān)需要的屬性即可
VB.NET的iPlotLibrary控件是VB.NET最常用也最簡(jiǎn)單的一種控件。下面介紹一下如何使用iPlotLibrary控件。
新建一個(gè)VB.NET新程序
左側(cè)工具箱選擇‘iPlotLibrary’控件
中間桌布上拖動(dòng),放在中間
iPlotLibrary控件最常用的兩個(gè)屬性:name。一般使用簡(jiǎn)寫的Btn來(lái)代表iPlotLibrary按鈕,IT業(yè)內(nèi)的規(guī)定。
另一個(gè)常用的屬性,Text。用來(lái)顯示我們當(dāng)前的按鈕文字,默認(rèn)是iPlotLibrary1。
雙擊iPlotLibrary控件屬性,自動(dòng)進(jìn)入click事件。click事件的意思是只要單擊iPlotLibrary控件,就會(huì)觸發(fā)事件,這個(gè)事件就是代碼中所寫的要彈出的對(duì)話框hello world!
使用按鍵F5,調(diào)試運(yùn)行。結(jié)果如下,這樣便是成功了。
Dim tab1 As String
Dim DS As New DataSet
Dim SqlStr As String
Dim DTS_A As New DataGridTableStyle
Dim Col_A1, Col_A2, Col_A3 As New DataGridTextBoxColumn
Dim Col_bo As New DataGridBoolColumn
'DS.Tables("tab1").Columns.Add("check", System.Type.GetType("System.Boolean"))
'DS.Tables("tab1").Columns("check").DefaultValue = False
Dim Col_A() As DataGridColumnStyle = {Col_bo, Col_A1, Col_A2, Col_A3}
'主窗口
Private Sub fICitem_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ProgressBar1.Hide()
grdStyles("tab1")
SQLConn(conn)
Dim i As Integer
'Dim arr As New ArrayList
SqlStr = "SELECT ID, diqu, dname FROM a_diqu Order by ID"
If Not SQLFill(conn, DA, DS, SqlStr, "diqu") Then
MsgBox("服務(wù)器連接失敗")
Else
Dim dr As DataRow
For Each dr In DS.Tables("diqu").Rows
'arr.Add(dr("diqu") "." dr("dname"))
cmbDiqu.Items.Add(dr("diqu") "." dr("dname"))
Next
End If
'cmbDiqu.DataSource = arr
cmbDiqu.SelectedIndex = -1
cmbDiqu.Text = ""
End Sub
'DataGrid格式
Private Sub grdStyles(ByVal tab As String)
Dim ii1 As Integer
Dim DTS_A1() As String = {"check", "fshortnumber", "fnumber", "fplanprice"}
Dim DTS_A2() As String = {"檢查框", "貨號(hào)", "上市月份", "單價(jià)"}
Dim DTS_A3() As Integer = {50, 120, 120, 120} '長(zhǎng)度
Dim DTS_A4() As Integer = {2, 0, 2, 1} '對(duì)齊方式
DTS_A.MappingName = tab
DTS_A.GridColumnStyles.Clear()
DTS_A.AllowSorting = True
DTS_A.ReadOnly = True
DTS_A.BackColor = Color.DarkGray
DTS_A.AlternatingBackColor = Color.LightGray
For ii1 = 0 To DTS_A1.Length - 1
Col_A(ii1).MappingName = DTS_A1(ii1)
Col_A(ii1).HeaderText = DTS_A2(ii1)
Col_A(ii1).Width = DTS_A3(ii1)
Col_A(ii1).NullText = ""
Col_A(ii1).Alignment = DTS_A4(ii1)
Next
DTS_A.GridColumnStyles.AddRange(Col_A)
grdItem.TableStyles.Add(DTS_A)
grdItem.ReadOnly = True
End Sub
Private Sub fICitem_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
SQLClose(conn)
End Sub
'顯示表單
Private Sub Show1()
grdStyles("tab1")
txtNum.Text = Trim(txtNum.Text)
grdItem.DataSource = Nothing
SQLStr = "select * from t_icitem_new"
SQLStr = " where fshortnumber like '" txtNum.Text "%'"
SQLStr = " order by fshortnumber"
SQLFill(conn, DA, DS, SQLStr, tab1)
grdItem.CaptionText = "貨號(hào)資料"
grdItem.DataSource = DS.Tables(tab1)
ShowRow()
End Sub
'數(shù)據(jù)同步顯示
Private Sub ShowRow()
Dim dr As DataRow
If DS.Tables(tab1).Rows.Count 0 Then
If grdItem.CurrentRowIndex DS.Tables(tab1).Rows.Count Then
grdItem.CaptionText = grdItem.CurrentRowIndex + 1 "/" DS.Tables(tab1).Rows.Count
dr = DS.Tables(tab1).Rows(grdItem.CurrentRowIndex)
txtNum.Text = dr.Item("fshortnumber")
txtPrice.Text = dr.Item("fplanprice")
cmbMonth.Text = dr.Item("fnumber")
Else
txtNum.Text = ""
txtPrice.Text = ""
cmbMonth.Text = ""
End If
Else
txtNum.Text = ""
txtPrice.Text = ""
cmbMonth.Text = ""
End If
End Sub
Private Sub grdItem_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdItem.CurrentCellChanged
ShowRow()
End Sub
'鼠標(biāo)選定行
Private Sub grdItem_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdItem.CurrentCellChanged
ShowRow()
End Sub
Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnSelect.Click, btnUpdate.Click, btnInsert.Click, btnDelete.Click
Select Case sender.name
Case "btnSelect" '查詢
Show1()
Case "btnUpdate" '修改
SUpdate()
Case "btnInsert" '新增
SInsert()
Case "btnDelete" '刪除
SDelete()
End Select
End Sub
'修改
Private Sub SUpdate()
txtNum.Text = Trim(txtNum.Text)
txtPrice.Text = Trim(txtPrice.Text)
If MsgBox("確定更新?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
SQLStr = "UPDATE t_icitem_new SET "
SQLStr = "fnumber='" cmbMonth.SelectedItem "', "
SQLStr = "fplanprice='" txtPrice.Text "' "
SQLStr = "WHERE fshortnumber='" txtNum.Text "'"
If Not SQLFill2(conn, SQLStr) Then
MsgBox("抱歉,更新失敗:(")
End If
Show1()
End If
End Sub
'新增
Private Sub SInsert()
txtNum.Text = Trim(txtNum.Text)
txtPrice.Text = Trim(txtPrice.Text)
If MsgBox("確定新增?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
SQLStr = "SELECT fshortnumber "
SQLStr = "FROM t_icitem_new "
SQLStr = "WHERE fshortnumber = '" txtNum.Text "' "
SQLFill(conn, DA, DS, SQLStr, "Tmp")
If DS.Tables("Tmp").Rows.Count = 0 Then '如果沒有資料則新增
SQLStr = "INSERT INTO t_icitem_new (fshortnumber,fnumber,fplanprice) VALUES("
SQLStr = "'" txtNum.Text "', "
SQLStr = "'" cmbMonth.SelectedItem "', "
SQLStr = "'" txtPrice.Text "')"
If Not SQLFill2(conn, SQLStr) Then
MsgBox("抱歉,新增失敗:(")
End If
Show1()
Else
MsgBox("資料已存在")
End If
End If
End Sub
'刪除
Private Sub SDelete()
txtNum.Text = Trim(txtNum.Text)
txtPrice.Text = Trim(txtPrice.Text)
If MsgBox("確定刪除" txtNum.Text "?", MsgBoxStyle.YesNo + MsgBoxStyle.DefaultButton2) = MsgBoxResult.Yes Then
SQLStr = "DELETE FROM t_icitem_new "
SQLStr = "WHERE fshortnumber='" txtNum.Text "'"
If Not SQLFill2(conn, SQLStr) Then
MsgBox("抱歉,刪除失敗:(")
End If
Show1()
End If
'鍵盤順序
Private Sub txt_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) _
Handles txtFshop.KeyDown, txtItem.KeyDown
Select Case e.KeyCode
Case Keys.Return
SendKeys.Send("{TAB}")
Case Keys.Right
SendKeys.Send("{TAB}")
Case Keys.Left
SendKeys.Send("+{TAB}")
Case Keys.Down
SendKeys.Send("{TAB 10}")
Case Keys.Up
SendKeys.Send("+{TAB 10}")
End Select
End Sub
首先,你已經(jīng)完成的步驟是:
1.新建一個(gè)用戶控件[注意,用戶控件(UserControl)不是自定義控件(CustomControl)]
2.給用戶控件起個(gè)名字(我在此處起名叫 RadioList)
3.在用戶控件上畫一個(gè) GroupBox,命名為 RadioGroup,
并將其 Dock 屬性設(shè)置為 Fill
然后,你需要做的是動(dòng)態(tài)增減控件。
如果你以前用過(guò) VB 6,你可能會(huì)想到控件數(shù)組,
但在此處,你可以直接增刪控件。
具體實(shí)現(xiàn)如下:
4.在 GroupBox 里畫一個(gè) FlowLayoutPanel,命名為 RadioPanel,
將其 AutoScroll 屬性設(shè)置為 True,即自動(dòng)顯示滾動(dòng)條,
并設(shè)置其 FlowDirection 屬性(例如設(shè)置為 TopDown)
這樣就省去了手動(dòng)調(diào)整 RadioButton 位置的麻煩
5.實(shí)現(xiàn)選項(xiàng)的動(dòng)態(tài)增減(以下只是我的思路,你可以發(fā)揮一下)
(十分簡(jiǎn)潔,注釋除外):
''' summary
''' 創(chuàng)建一個(gè)新的 RadioButton。
''' /summary
Private Function CreateRadio() As RadioButton
Dim NewRadio As New RadioButton
components.Add(NewRadio)
'components 字段由控件設(shè)計(jì)器自動(dòng)創(chuàng)建,
'此代碼目的是使控件在銷毀(Dispose)時(shí)能自動(dòng)銷毀 RadioButton
'詳情參見 RadioList.Designer.vb
NewRadio.Parent = RadioPanel
'設(shè)置容器
AddHandler NewRadio.CheckedChanged, AddressOf RadioButtons_CheckedChanged
'設(shè)置事件處理程序
Return NewRadio
End Function
''' summary
''' 移除已存在的 RadioButton。
''' /summary
Private Sub RemoveRadio(ByVal dest As RadioButton)
components.Remove(dest)
dest.Dispose()
End Sub
'無(wú)中生有的 RadioButton 的事件處理程序
Private Sub RadioButtons_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
'在此處設(shè)置選中項(xiàng)的屬性,通過(guò) sender 來(lái)確定不同的 OptionButton
'你可以通過(guò)將 OptionButton 放在一個(gè)列表,
'如 List(Of OptionButton) 中來(lái)像數(shù)組一樣維護(hù)選項(xiàng)的次序
'注意,此處需要判斷 sender 的 Checked 屬性是否為 True
'因?yàn)?Changed 是“改變”,而不是“選中”
'例如:
If DirectCast(sender, RadioButton).Checked Then
SelectedIndex = ...
End If
End Sub