在vb.net中右键单击它时,获取任何文本框的名称 [英] Get the name of any textbox when i right click over it in vb.net
问题描述
我是VB.NET的初学者,这是我的第一篇文章:)
我使用了一些带有选项的上下文菜单条,这些选项在我的项目中启用了textbox1。但是,如果我有'n'文本框,那么我希望这个相同的自定义上下文菜单条自动启用所有这些。
我解决这个问题的逻辑是获取用户右键单击的文本框名称(n个文本框中),然后将相同的文本框名称作为字符串传输上下文菜单条代码,它将触发我的表单上任何文本框的上下文菜单条。那么我的逻辑是否正确?
欢迎任何建议或任何其他逻辑。感谢您阅读!
您必须使用ContextMenu设置所有TextBox的ContextMenuString属性。在菜单项单击事件中,您必须执行以下操作。
1.将发件人投射到ToolStripMenuItem
2.投射所有者属性到ContextMenuStrip
3.将此menuStrip的SourceControl属性转换为TextBox。这将是调用菜单的TextBox控件。
4.现在您可以使用此文本框的名称/文本属性。
私人 Sub ToolStripMenuItem1_Click(< span class =code-keyword> ByVal sender As System。 Object , ByVal e As System.EventArgs) Handles ToolStripMenuItem2。点击
Dim text As String = DirectCast ( DirectCast (sender,ToolStripMenuItem).Owner,ContextMenuStrip).SourceControl.Text
结束 Sub
或
私有 Sub ToolStripMenuItem1_Click ( ByVal sender As System。 Object , ByVal e As System.EventArgs)句柄 ToolStripMenuItem1.Click
Dim selectedItem As ToolStripMenuItem = DirectCast (sender,ToolStripMenuItem)
Dim contentMenu As ContextMenuStrip = DirectCast (selectedItem.Owner,ContextMenuStrip)
Dim sourceTextBox As TextBox = DirectCast (contentMenu.SourceControl, TextBox)
Dim text As String = sourceTextBox.Text
结束 Sub
我通过组合不同的代码做了一些R& D,最后得到了代码......
非常感谢你的帮助......
这是VB的代码
这是Akbar已经提到的先决条件
1)为表格上的每个文本框启用contextmenustip
2)在Handles之后提及它们
3)Textbox4仅用于确认,以识别您点击的TB,即1,2或3
4)现在您可以使用obj链接您右键单击的任何文本框
Dim obj As 对象
Dim txt As String
Dim 当前 As 字符串
私有 Sub RightClick( ByVal sender 作为 对象, ByVal e As System.Windows.Forms.MouseEventArgs)句柄 TextBox1.MouseDown,TextBox2.MouseDown,TextBox3.MouseDown
如果 e.Button = Windows.Forms.MouseButtons.Right 那么
如果 TypeOf sender TextBox 然后
txt = DirectCast (发件人,控制).Name
TextBox4.Text =( & txt)
对于 每个 elem 在 Me .Controls
如果 elem.Name = txt 然后
obj = elem
current = obj.Text
退出 对于
结束 如果
下一步
结束 如果
结束 如果
结束 sub
I am a beginner in VB.NET and this is my first post :)
I have used a context menu strip with some options, which get enabled for textbox1 in my project. But if I have 'n' textboxes, then i want this same custom context menu strip to get enabled to all of them automatically.
My logic to solve this problem was to get the textbox name (out of n textboxes) over which the user right clicks and then transfer the same textbox name as a string to context menu strip code, which would fire the context menu strip for any text box on my form. So is my logic correct?
Any suggestion or any other logic is welcomed. Thanks for reading!
You have to set all TextBox's ContextMenuString property with the ContextMenu. And in the menu item click event, you have to the following.
1. Cast the sender to ToolStripMenuItem
2. Cast its Owner property to ContextMenuStrip
3. Cast SourceControl property of this menuStrip to TextBox. This will be the TextBox control from where the menu invoked.
4. Now you can use the Name/Text properties of this textbox.
Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem2.Click Dim text As String = DirectCast(DirectCast(sender, ToolStripMenuItem).Owner, ContextMenuStrip).SourceControl.Text End Sub
or
Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem1.Click Dim selectedItem As ToolStripMenuItem = DirectCast(sender, ToolStripMenuItem) Dim contentMenu As ContextMenuStrip = DirectCast(selectedItem.Owner, ContextMenuStrip) Dim sourceTextBox As TextBox = DirectCast(contentMenu.SourceControl, TextBox) Dim text As String = sourceTextBox.Text End Sub
I did a bit of R&D by combining different codes and finally got the code working...
Thanks a lot for the help..
Here's the Code for VB
The prerequisite for this as Akbar already mentioned
1) Enable contextmenustip for every textbox on the form
2) Mention them after Handles
3) Textbox4 is just for confirmation, to identify over which TB you clicked i.e 1,2 or 3
4) Now you can use obj to link any textbox over which you right click
Dim obj As Object Dim txt As String Dim current As String Private Sub RightClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown, TextBox2.MouseDown, TextBox3.MouseDown If e.Button = Windows.Forms.MouseButtons.Right Then If TypeOf sender Is TextBox Then txt = DirectCast(sender, Control).Name TextBox4.Text = ("" & txt) For Each elem In Me.Controls If elem.Name = txt Then obj = elem current = obj.Text Exit For End If Next End If End If End sub
这篇关于在vb.net中右键单击它时,获取任何文本框的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!