Note: Similar Question can be found here.
How can I pass values from multiple User Controls? The question in the link above provides an answer however I find the answer very tedious in my situation and there is a delay in passing of the values. (I have to cycle to and from UserControl1 and UserControl2 multiple times while in UserControl1 committing a change of a textbox or label to see any passing of values in UserControl2.)
Either way, since I have multiple UserControls in which each has many textboxes, labels, and comboboxes, I would very much not like having to create separate Sub Routines and EventHandlers for each and every control with a value that I would like to pass.
Is there a better way? I was thinking something like...
'In UserControl10
Dim UserControl1 As New UserControl1
Dim UserControl2 As New UserControl2
Dim UserControl3 As New UserControl3
UC10Label1.Text = UserControl1.Label1.Text
UC10TextBox1.Value = UserControl2.TextBox1.Value
UC10ComboBox1.Text = UserControl3.ComboBox1.SelectedItem
The code above obviously does not work the way I would imagine, how can I achieve something similar with the least amount of code?
Edited: I have multiple custom UserControls in which I use as 'views'. In each UserControl there are labels, textboxes, & comboboxes. I have a Panel1 in which on a triggered event, will display a UserControl(1-9) in the panel; each UserControl is displayed one at a time and is contingent on an event. I want to be able to pass values from each UserControl(1-9) to UserControl10's labels, textboxs, or comboboxs etc..
I'm guessing that the last three lines aren't in a Sub. They're probably not working because they're executing before the form has been shown.
If you want to update them automatically in your program, you should put them in a sub, but suspend the form layout while they're updating and then resume layout when the code has finished. Like this
Private Sub UpdateUserControls()
Me.SuspendLayout()
UC10Label1.Text = UserControl1.Label1.Text
UC10TextBox1.Value = UserControl2.TextBox1.Value
UC10ComboBox1.Text = UserControl3.ComboBox1.SelectedItem
Me.ResumeLayout()
End Sub
Depending on when you want to update these controls, you can do it each time the form is shown by placing the above Sub in the Form's .Shown event.
Or you could do it automatically every so often by placing it in a Timer's .tick event.
Or you could choose to update them at certain points in your program by placing the sub somewhere in your code.
Related
I'm trying to add items to listbox from another form but the listbox doesn't seem to update.
I have 3 form : frm1, frm2, frm3
this my code in frm1 to open frm2:
Using frm As New frm2
frm.ShowDialog()
End Using
frm2 has a listbox named list_xxx
code in frm3:
Private Sub add_item()
frm2.list_xxx.add("aaaa")
End Sub
i want to add item to frm2 from frm3. but no success the listbox still empty.
how to fix it?
You are almost certainly referring to the default instance of frm2 in that second code snippet but you aren't displaying the default instance in the first code snippet. If you display one form and then add items to a ListBox on another form, there should be no surprise that you can't see those items. Either use the default instance everywhere or not at all. The more correct option is not at all, but that would require other changes too. The easier option is everywhere, which means changing the first code snippet to this:
frm2.ShowDialog()
No Using statement because you're not creating an object.
I have a problem with the snippet of the code I have got to extend and improve. It is not my original code and I cannot change the logic that much of it, just to be clear.
I have one main form called MDIServer, which has a timer set for every second. In Timer.Tick I have some other code which works fine (timer is running okay). Newly, I had to check there, if one form is Active and if so, change some stuff (labels text and tags) in that form and refresh it.
I add there this code:
If IsActiveForm("frmName") Then
frmName.ChangeSomething()
End If
The Sub ChangeSomething is, how you can see, located in the form I want to refresh and do the changes. In that function I simply change the label text and tags of few controls.
My question is: Form is not refreshing => labels are not visible changed, why?
I think I tried already almost anything with Refresh() function in the ChangeSomething() function or in the timer after I called this function. Also I tried to add there new timer (in frmName) and do the changes there, which works perfectly with
Label.Text = "something new"
Label.Refresh()
So I guess problem is somewhere with the refreshing form from Timer in different form. I also tried to do it with my own InvokeReguired() function etc...
P.S. When I am debugging the code, labels and tags are changing and every single function which has to be called, is called, but it is just not visible on the form itself.
EDIT Info
formName is not declared in MDIServer explicitely and in this case and many other cases, forms are used as default instances. Timer is from System.Windows.Forms.Timer. Also MDIServer is not a MDIParent of the formName and I cannot use Me.ActiveMdiChild Is. Lets just say, these two forms are not dependent on each other in any way.. and everything is done through name of the form (default instance, so nothing like Dim frm As Form and frm = frmName).
I would be really glad for any tip or anything :D
Thanks guys,
Vojta
So, I fixed my problem after some research and the problem was (expected) that I am not calling the subroutine ChangeSOmething() for one specific instance of the form frmName. So I had to change my code, that I will call it exactly for the instance which is active and visible.
New code looks like this:
Dim frmCollection = Windows.Forms.Application.OpenForms
Dim listfrmname = frmCollection.OfType(Of frmName).ToList()
If listfrmName.Count > 0 Then
Dim tmpFrm As frmName = listVZT15.Last()
tmpFrm.ChangeSomething()
End If
I also could not use combination of frmCollection.OfType(Of frmName).Any and frmCollection.Item("frmName"), because when I was closing the form and opening again, it created new and new instances (I dont know, why it is not closing the old one, but like I said, it is not my code). So the logic is, to list all open forms of the needed type, and then take the last instance from that list and for that instance call the subroutine. Also Me.Refresh() is placed in the subroutine ChangeSomething() itself.
Thanks everyone for help, it surely helped me to understand, how the instances works here.
I just started rewriting an application from vba (Access) to vb.net + SQLServer so not very experienced in .net.
I am creating custom controls (Form + form controls) with a number of extra properties PrevValue, Modified (similar then the one of Textbox), Dirty, DirtyEnabled, SQLColumnName, SQLTableName to enable AutoUpdating and undoing in my forms the form exposes IsDirty, Initialising and Isready properties and an undo method.
Doing so it occurs that I have to write 3 times the same iteration code in different places:
For each Ctrl as Control in frm.Controls ' frm being a reference to the form
if typeOf Ctrl is MyTextBox
with DirectCast(Ctrl, MyTextBox)
' here comes the variable code depending what needs to be done
end with
elseif TypeOf Ctrl is MyComboBox
' etc.... for MyListBox, MyCheckBox etc....
I also have a number of custom controls MyNumBox and MyDateBox that inherit from MyTextBox but with some modified behavior (Formula evaluation, date manipulation, calendar...) how do I avoid doing an extra test on them.
One version of this Iteration is in the SQLProcessClass where the modified controls are added as SQLParameter and after iteration calling the SQLProcessClass Update or Insert, but ... after successful SQL activity I need to iterate through the controles again to reset the modified flag for each control. Elsewhere I need it to implement a form undo to reset all the controls to their previous values.
It seems to me I have two options
1. repeating that iteration code everywhere I need to iterate through the forms controls. I don't like it as every time I would need to create a new custom control I have to add some lines X times in different modules/classes ... very bad programming
2. Creating one form iteration procedure containing all the different activities that normally belong to another class within that "centralised" procedure, that could be better then (1) but I don't like it that much either.
Is there a better way of doing it using some .net functionality I don't master yet ?
Thanks for any advise.
Iterating through from controls can be tricky since controls are often nested. A more controlled approach would be to add another collection object to your form where you keep references to your added controls.....
e.g
Dim My_Widgets as New List(of Your-Control-Class-Name)
Then when you create the controls to the form also add them to that list.
My_Widgets.Add(Widget_Object)
After that it is a simple matter to iterate through that list.
For Each Widget as My_Widget_CLass in My_Widgets
' do what you need to do to Widget
Next
If you need to reference individual controls directly, use a dictionary object instead..
e.g.
Dim My_Named_Widgets as new Dictionary(of String, Your-Control-Class-Name)
Then add your control references to the dictionary by name
My_Named_WIdgets.add("<Whatever_You_USe_To_Identify_It>", Widget_Object)
You can then reference the specific control by the ID or name
My_Names_Widgets("ID").Property = Whatever '... etc
You seem to be indicating you have other controls for other purposes, as such it would be prudent to create similar collections for each type.
In Vb.net I have a button on one form (call it button_abc), changes color, the text changes, has a click event, etc. I want to have the SAME button duplicated on another form so that it can be used from 2 different places (one of the forms might not be visible). When the text or color gets changed on one, it need to appear in both. So if both forms are open, the buttons always appear to match exactly in appearance and action. is there a way to "link" them together automatically?
Copying & pasting a button, simply creates a new (separate) button---not what I want.
I would be inclined to go down the path of using an adapter to allow you to update the buttons.
Something like this:
Public Class ButtonList
Inherits List(Of Button)
Public Property Color() As Color
Get
Return Me.Select(Function (b) b.Color).FirstOrDefault()
End Get
Set(ByVal Value As Color)
For Each b In Me
b.Color = Value
Next
End Set
End Property
' + all other relevant properties.
End Class
Since it inherits from List(Of Button) you just add all the buttons you need to this class using .Add(button) and then put in all of the properties that you want to update. Now the code appears very much the same, but you now will update many buttons at once.
This is along the lines of what I'm describing, except I'm using VB
Duplicate WebControls at Runtime
Since you always want the buttons to match, it doesn't matter if one form is open/visible or not. You simply create 2 buttons, 1 on each form and make them do the same thing on both forms. So some pseudo-code may look like this:
Form 1:
Button.ClickEvent
Me.Color = UglyGreen
Form2.Button.Color = UglyGreen
End Button.ClickEvent
Form 2:
Button.ClickEvent
Me.Color = UglyGreen
Form1.Button.Color = UglyGreen
End Button.ClickEvent
It's not a very pretty solution, but it works for the specified task.
First things first. There's a good chance what I want to do should really be done with VB and not VBA. But as long as it is possible I would rather use VBA.
I have a userform of essentially a big diagram made of hundreds of labels. I want to separate these labels into groups. And then separate these groups into subsystems. The idea being I have some form of heirarchy to work with. The label groups need to change color based on what I have selected in a combo box, and if I click on one of these labels I want to bring up a user form showing details of the subsystem using click events.
I'm pretty sure I need to use multiple classes to do what I want but am fairly new to using class modules. Though I get the concept.
Basically I want some functionality that goes subsystem -> label group( or part) -> color with click events for the whole subsystem and combo box events for changing label group colors.
I saw a thread online about grouping labels or text boxes but it only works to trigger the even for a group, not change the properties of the whole group once the event is triggered. I would like to set this up in classes as well so I can export the system for use in other future userforms.
I was able to create groups of labels and change them together like I wanted:
CPart (Class Module 1):
*This is meant to handle the event triggering of the labels and includes some color code that I used to test functionality of the groups changing together and functionality of changing colors.
Public WithEvents trigger As MSForms.Label
Dim pLabels As Collection
Property Set triggers(c As Collection)
Set pLabels = c
End Property
Private Sub trigger_Click()
For Each obj In pLabels
obj.BackColor = RGB(255, 0, 0)
Next obj
End Sub
CTrigger (Class Module 2):
*This took a collection of labels which were passed in through a collection variable in the userform and then stored each label as a trigger in a separate class variable, along with the whole collection of labels in that group. This way when any trigger fires the event, all of the labels change.
Dim CTrigger() As New CPart
Dim pLabels As Collection
Dim i As Integer
Property Set Labels(c As Collection)
Set pLabels = c
For i = 1 To pLabels.Count
ReDim Preserve CTrigger(1 To i)
Set CTrigger(i).trigger = pLabels.Item(i)
Set CTrigger(i).triggers = pLabels
Next i
End Property
Property Get Labels() As Collection
Labels = pLabels
End Property
I really don't like the way it works, partly because I am losing myself in the logic of it constantly, and partly because it means that in order to use this I have to make collections of labels in the userform module anyway just to run it. It is very inefficient code, but I am putting it up so you get an idea of what I am trying to accomplish.
What I would much rather do instead is have one class variable to hold my custom collection of labels (a "LabelGroup"). Another class variable is likely required to hold the labels themselves (I think). And then all I would have to do is go through and write methods for the LabelGroup class such as changecolor, and it could handle that. But I can handle that part, for now what I really need help with is setting up the class framework in a neat way, so that the module I will eventually run could just say things like:
LabelGroup1.Add Label1
LabelGroup2.Add Label2
or
Private Sub button_click()
LabelGroup1.ChangeColor(RGB(...))
End Sub
These two articles have been helping me along:
http://www.databaseadvisors.com/newsletters/newsletter200503/0503usingcustomcollections/using%20custom%20collections%20in%20microsoft%20access.asp
http://j-walk.com/ss/excel/tips/tip44.htm
I was just looking at something similar but not quite so detailed. I'm trying to improve the look of a complex userform by making it look more modern and was going to try to fake mouseOver highlighting or at least active/inactive shading for labels placed overtop of graphical buttons.
Anyway, have you considered just changing the names of the label objects so that they are prefixed/suffixed with some kind of group or subsystem ID?
That way when you pass them to a sub to change their colour, you can check the prefix or suffix.