Dim statement question

mevets

Member
Joined
Jun 22, 2006
Messages
17
Location
Northern, VA
Programming Experience
Beginner
I have a a series of buttons that are handled by one subroutine. Once one of the buttons is pressed I want to create a new control on the form of whatever button was pressed. I've managed to put the type of control into a string:
VB.NET:
strCtrlType = sender.ToString()
From here I want to know if and how I can
VB.NET:
Dim newCtrl as new strCtrlType
or should I just do a large If then else like
VB.NET:
If strCtrlType = "Button" Then
Dim newCtrl as new Button
ElseIf strCtrlType = "Label"
Dim newCtrl as new Label
ElseIf....
etc
I'm not sure what to do, can someone point me in the right direction?
 
buttons, labels, it all can be handled in one sub:

VB.NET:
Private Sub Buttons (...) Handles Button1.Click, Button2.Click, Button3.Click
  Dim Button As Button = Ctype(sender, Button)
  Select Case Button.Name
    Case "Button1"
      'Add Button1 Object to form
    Case "Button2"
      'Add Button2 Object to form
    Case "Button3"
      'Add Button3 Object to form
  End Select
End Sub
 
VB.NET:
Dim newControl As Control

If sender Is Me.addTextBoxButton Then
    newControl = New TextBox
ElseIf sender Is Me.addComboBoxButton Then
    newControl = New ComboBox
...
End If

Me.Controls.Add(newControl)
The point of using a common event handler is so you don't have to write common code multiple times. If you're simply going to determine which button was clicked and then go a different direction for each one then there is no point using a common event handler. In fact, it is actually counter-productive. Unless you have code that needs to be executed regardless of the button that was clicked then you should use separate event handlers. You should also note that you can use more than one method to handle the same event and you can also call the same method from multiple event handlers if you have some common code but a lot of different code too.
 
Guys, I dont think he is asking this at all.. ?
He seems to be saying something like:

I have a form with 3 buttons
The buttons are labeled "Add A Label", "Add a TextBox" "Add A Checkbox"
When I click the "Add a Label" button, I want a new label to appear on the form etc
If I have 2 instances of the form open then I want the Label/TextBox/Checkbox only to be added to the form on which i clicked the button.. not both


mevsets, incidentally, if you'd asked your question like this, there would have been no doubt in the answer - when asking a question, state where you are, what you have and where you want to be :)

My response, if i understand your question to be as i describe:

VB.NET:
Sub AddALabel_Button_Click(sender..., eventargs...) Handles AddALabel_Button.Click
  Me.Controls.Add(New Label())
End Sub
Sub AddATextBox_Button_Click(sender..., eventargs...) Handles AddATextBox_Button.Click
  Me.Controls.Add(New TextBox())
End Sub
 
...
 
Maybe you're right, but why would a control appear on a form other than the one on which the button was pressed? That's like turning on your windscreen wipers and worrying that the windscreen wipers on every other car will turn on too.
 
true true... good analogy :) maybe its a throwback to old vb6 where there was a form instance that had the same name as the form type.. and you could accidentally put stuff on different forms by typing the wrong thing..

vb.net warns i think, about this, if you were to say

Form1.Controls.Add() it says something about static references etc..


ANyways.. we'll find out when the OP returns
 
Sorry guys, my problem is not that control are being added to the wrong form, it is that since many tasks are done multiple time I want it to be handled in one sub. It's not going to be as effective as I thought though since I was having each controls .Text, .Name, etc set this way. But it breaks down when it does things like assign the .Text of a DateTime as "DateTime1" because that isnt valid.

My resolution is that I'll have to use a large Case Select but it can't have conflictive properties after the cases like .Text, itll have to be inside each case.

Thanks for the Dim ctrlWhatever as Control, didn't realize I could do that.
 
There is something called Reflection that enables you to handle much advanced coding, so you could create a Button from the string "Button" and so on. You do sound as you still have some basics to learn, and the suggestions above will get you where you want. Anyway, below is an example of what can be done with instance creation through reflection.
VB.NET:
'Button1.Text is "Label"
'Button2.Text is "Button"
 
Private Sub evh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click, Button2.Click
  'reflective instance creation
  Dim a As Reflection.Assembly = Reflection.Assembly.GetAssembly(GetType(Form))
  Dim x As Control = a.CreateInstance("System.Windows.Forms." & sender.text)
  Static dc As Short 'static dynamic counter
  x.Text = "dynamic" & dc.ToString 'set Text
  x.Name = "dynamic" & dc.ToString 'set Name
  x.Location = New Point(0, dc * 20) 'set Location
  AddHandler x.Click, AddressOf dynamic_Click 'add click event handler
  Me.Controls.Add(x) 'add control to form
  dc += 1 'increment dynamic counter
End Sub
 
Private Sub dynamic_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
  Me.Text = sender.name
End Sub
By the way, DateTime is not a control.
 
Last edited:
mevets,


jmcilhinney's note about avoiding handling multiple types of control by one sub remains valid - it's something I avoid because it's inefficient and messy.. as an extreme example, you could assign every single control in your app, that has a keyDown event to be handled by the MyKeyDown sub..

Then in that sub you run a huge case statement to find out which control was keydowned, and then do some code just for that control - you end up with a huge sub (subs shouldnt really be longer than a couple of screens as they get hard to track) that if broken into smaller subs, one for each control, would be more readable and navigable..

addiitonally, the computer has the overhead of looking through X number of controls before it finds the one that was clicked whereas it wouldnt have that overhead if the setup was one-sub-per-control


perhaps though, if you present us exactly with the problem you face, we can makea suggestion that a) solves your problem, b)serves as a teaching point also
 
Last edited by a moderator:
Back
Top