给ListBox添加DoubleClick事件,然后通过ListBox的SelectedItem就知道用户双击了哪一个Item
创新互联专注于宜城网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供宜城营销型网站建设,宜城网站制作、宜城网页设计、宜城网站官网定制、小程序定制开发服务,打造宜城网络公司原创品牌,更为您提供宜城网站排名全网营销落地服务。
补充:
晕,我还以为是WinForm呢。楼主你说清楚是ASP.NET啊!
给你个思路,给listbox(也就是HTML的select)添加一个ondoubleclick属性,调用JavaScript代码,设法标记双击的对象并通知页面提交。如果有困难我再给你写代码
===================
补充:例子做好喽
Default.aspx
***********
%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="WebApplication1._Default" %
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""
html xmlns=""
head runat="server"
title演示/title
script type="text/javascript"
function Move() {
var myForm = document.getElementById("% = Me.form1.ClientId %");
var iptHidden = document.getElementById("% = Me.iptHidden.ClientId %");
var listbox1 = document.getElementById("% = Me.ListBox1.ClientId %");
iptHidden.value = listbox1.selectedIndex;
myForm.submit();
}
/script
/head
body
form id="form1" runat="server"
div
asp:ListBox ID="ListBox1" ondblclick="javascript:Move();" runat="server" Height="238px"
Width="338px"
/asp:ListBox
asp:ListBox ID="ListBox2" runat="server" Height="235px" Style="margin-top: 0px"
Width="339px"/asp:ListBox
input type="hidden" id="iptHidden" runat="server" /
/div
/form
/body
/html
**********
Default.aspx.vb
**********
Public Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
Me.ListBox1.Items.Add("选项1")
Me.ListBox1.Items.Add("选项2")
Me.ListBox1.Items.Add("选项3")
Me.ListBox1.Items.Add("选项4")
Me.ListBox1.Items.Add("选项5")
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Me.IsPostBack Then
Dim indexToMove = CInt(Request.Form("iptHidden"))
If indexToMove = 0 Then
Dim itemToMove = Me.ListBox1.Items(indexToMove)
itemToMove.Selected = False
Me.ListBox1.Items.Remove(itemToMove)
Me.ListBox2.Items.Add(itemToMove)
End If
End If
End Sub
End Class
**********
Default.aspx.designer.vb
**********
'------------------------------------------------------------------------------
' 自动生成
' 此代码由工具生成。
'
' 对此文件的更改可能会导致不正确的行为,并且如果
' 重新生成代码,这些更改将会丢失。
' /自动生成
'------------------------------------------------------------------------------
Option Strict On
Option Explicit On
Partial Public Class _Default
'''summary
'''form1 控件。
'''/summary
'''remarks
'''自动生成的字段。
'''若要进行修改,请将字段声明从设计器文件移到代码隐藏文件。
'''/remarks
Protected WithEvents form1 As Global.System.Web.UI.HtmlControls.HtmlForm
'''summary
'''ListBox1 控件。
'''/summary
'''remarks
'''自动生成的字段。
'''若要进行修改,请将字段声明从设计器文件移到代码隐藏文件。
'''/remarks
Protected WithEvents ListBox1 As Global.System.Web.UI.WebControls.ListBox
'''summary
'''ListBox2 控件。
'''/summary
'''remarks
'''自动生成的字段。
'''若要进行修改,请将字段声明从设计器文件移到代码隐藏文件。
'''/remarks
Protected WithEvents ListBox2 As Global.System.Web.UI.WebControls.ListBox
'''summary
'''iptHidden 控件。
'''/summary
'''remarks
'''自动生成的字段。
'''若要进行修改,请将字段声明从设计器文件移到代码隐藏文件。
'''/remarks
Protected WithEvents iptHidden As Global.System.Web.UI.HtmlControls.HtmlInputHidden
End Class
**********
'把图片的声明放在外面,类里面过程外面,而且要用As,不然等会儿没法用
Dim myPicture As New System.Windows.Forms.PictureBox()
'动态生成的控件,加一行
Private Sub UserControl1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.Panel3.Controls.Add(myPicture)
myPicture.Size =New System.Drawing.Size(115, 160)
myPicture.TabStop =False
myPicture.Name ="p"
myPicture.Cursor = Cursors.Hand
AddHandler myPicture.Click, AddressOf mypic_Click '添加事件
AddHandler returnid, AddressOf idReturned '绑定事件和过程
End Sub
'再来个事件,放在声明的地方,就是类里面、过程外面
Private Event returnid(ByVal id As String) '这个事件可以传递值哦~
'普通的单击事件
Private Sub mypic_Click()
RaiseEvent returnid(myPicture.Name)
End Sub
'会传值的事件过程
Private Sub idReturned(ByVal id As String)
Dim bookInfo As New BookInfo
bookInfo.Show()
Me.Parent.Enabled = False
'用id变量做点什么吧
End Sub
如果要改事件的签名(参数一类的)的话,得重写控件。你可以这样自己弄一个事件。
动态生成的控件:
Private Sub UserControl1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim myPicture = New System.Windows.Forms.PictureBox()
Me.Panel3.Controls.Add(myPicture)
myPicture.Size = New System.Drawing.Size(115, 160)
myPicture.TabStop = False
myPicture.Name = "p"
myPicture.Cursor = Cursors.Hand
AddHandler myPicture.Click, AddressOf mypic_Click '添加事件
End Sub
处理事件的过程:
Private Sub mypic_Click() 'ByVal id As String
Dim bookInfo As New BookInfo
bookInfo.Show()
Me.Parent.Enabled = False
首先: textbox里没有显示,是因为SerialPort1和TextBox2不是同一线程创建的,需要跨线程操作。需要用到委托,这样才能显示出来。 其次: 我觉得用串口的接收数据事件更好一些。
两种方法:
1. 用WithEvents声明变量,这样你就可以像对待拖上去的控件一样操作:
Public Class Form3
WithEvents btn As New Button
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles Me.Load
Controls.Add(btn)
End Sub
Private Sub btn_Click(sender As Object, e As EventArgs) Handles btn.Click
MsgBox(Now)
End Sub
End Class
2. 使用AddHandler 语句:
Public Class Form3
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim btn As New Button
AddHandler btn.Click, AddressOf btn_Click
Controls.Add(btn)
End Sub
Private Sub btn_Click(sender As Object, e As EventArgs)
MsgBox(Now)
End Sub
End Class
WithEvents (Visual Basic):
AddHandler 语句:
常见事件有鼠的标单击、双击、移动;
有键盘的键按下、键松开、按键;
有窗体加载、窗体卸载等等。