Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting a reference to a textbox from its menu item .....

Status
Not open for further replies.

DarkConsultant

Programmer
Dec 4, 2007
156
0
0
GB
Hi All,

I have a form with hundreds of cms/menuitems, one for each textbox. They are all generated dynamically and when clicked all go to the same routine. I can find out which textbox was clicked by this -

MyText = DirectCast(DirectCast(DirectCast(sender, ToolStripMenuItem).Owner, ContextMenuStrip).SourceControl, TextBox).Text

but this is very ugly ... is there a better way?

Regards


DarkConsultant

Live long and prosper \\//
 
Can you not use something like:
Code:
			If TypeOf sender Is TextBox Then
				sender.text = "Textbox String"
			End If

--------------------------------------------------
“Crash programs fail because they are based on the theory that, with nine women pregnant, you can get a baby a month.” --Wernher von Braun
--------------------------------------------------
 
Hi ousoonerjoe,

Nah cos the sender is a ToolStripMenuItem and not a text box. I need the text box that the menu item is clicked from.

As I say I have a working solution its just a bit yuk.

Regards

DarkConsultant

Live long and prosper \\//
 
Code:
	Private Sub mnuSearchField_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSearchField.Click
		txtMsg.Text = sender.name
	End Sub

--------------------------------------------------
“Crash programs fail because they are based on the theory that, with nine women pregnant, you can get a baby a month.” --Wernher von Braun
--------------------------------------------------
 
Just make a function?
Code:
Friend GetText(tsm As Object) As String
Dim s As String
s =  DirectCast(DirectCast(DirectCast(tsm, ToolStripMenuItem).Owner, ContextMenuStrip).SourceControl, TextBox).Text
End Function

Then call it from your event handler
Code:
MessageBox.Show(GetText(sender))
 
Hi All,

As I said I have a working solution but would like to improve it.

I have always felt this is a bad omission for VB (mainly cos I need it all the time). RiverGuy, this is basically what I am doing at present.

OK thanks for wracking your considerable craniums for me.

Last question (I always ask ...).

I am currently using VB2005 and produce an app a month (ish) I have no problems except ...

Sometimes when I create a project or solution it is created wrong ... every time I run the project I get several errors (usually different) that occur before the first line of code runs. I know everyone wants to know which errors I am getting, well, just picture every error that you have ever seen in VB then this is what I get. They are absolutely random and often complain about things I am not using. I get ASP errors when not working on an ASP project, use you imagination ...

The solution is to delete the project and start again exactly the same and now I have a 75% chance of creating a good project. I am service packed up and running on a development PC that is guaranteed to be virus free (I write AV software).

I also get the dreaded "Visual Basic has stopped working ... what would you like to do?" {scream} message about once a day. Yes, I am very used to backing up and never lose work.

So my question is this ... should I upgrade to the latest version?

I downloaded and installed VB2010 and created a test project but really could not see any advantages.

DO YOU KNOW BETTER?

Sorry to be a pain but when I upgraded from vb6 to vb2005 I wnt through this process of asking everyone and I finished up making the correct decision so am looking to repeat the trick.

Thanks to all

DarkConsultant

Live long and prosper \\//
 
We migrated from 2005 to 2008 and then to 2010. 90% of those "random errors" you mentioned have stopped. I still get the occasional "VB has stopped," but not as often as before. Overall, I would have to say the jump to 2010 is well worth it.

--------------------------------------------------
“Crash programs fail because they are based on the theory that, with nine women pregnant, you can get a baby a month.” --Wernher von Braun
--------------------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top