|
Hi,
perhaps the subject is not so explanatory but now I try to explain with an example that well represents my problem.
I have a UserControl with two NumericUpDown and a button, then I have a Form1 with a TabControl1 with just one page.
Loading the form I add some pages to the TabControl and I put in every page my UserControl (called "base") in this way:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim mypag As TabPage
Dim myuc As base
For i As Int32 = 0 To 10
mypag = New TabPage
myuc = New base
myuc.Parent = mypag
mypag.Text = "Pag" & i.ToString
mypag.Name = "Pag" & i.ToString
TabControl1.TabPages.Insert(1 + i, mypag)
Next
End Sub
End Class
Than with the button on the page of the TabControl I pass to the next page of the TabControl (and this is done) but I also want to put in the first NumericUpDown of the "next page" the sum of the value of the two NumericUpDown in the current page (the one with the button just pressed) and I don't know how to do this.
The code in the button is:
Public Class base
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim tmp, id As Int32
tmp = NUD1.Value + NUD2.Value
id = CType(Parent.Parent, TabControl).SelectedIndex
CType(Parent.Parent, TabControl).SelectedIndex += 1
End Sub
End Class
I don't know how to "poit" to the NumericUpDown of a page different from the one in witch I am, even if I can get the index of that page (I think...)
I tryed something like this:
CType(Parent.Parent, TabControl).TabPages(id + 1)
CType(Parent.Parent, TabControl).TabPages(id + 1).NUD1.Value
Can someone give me some advice?
Thanks in advance
|
|
|
|
|
Where are you creating the NumericUpDown controls? At that point, you'd like to keep a reference to it, at class-level.
Public Class Form1: Form
Private n1 as NumericUpDown
Sub New()
n1 = new NumericUpDown
Controls.Add(n1)
End Sub
End Class
|
|
|
|
|
The NumericUpDown controls are in the UserControl I called "base" (together with the button), not in Form1, and I created them in the design mode, not writing the code as in your example.
In Form1 there is the TabControl in witch at runtime I put 10 pages, each one with a copy of "base" inside.
I did this because I needed a series of screens all the same and this was the most convenient way because I had to design only one screen (and also the build is faster).
The final result should be a series of tabbed pages all the same, the user begins to fill in the first, then the second and at that moment some fields on the page are automatically filled with the data entered on the previous page, and so on page after page.
All the controls are in the UserControl so I think I have to manage this mechanism inside the UserControl (perhaps the button to pass to the next page is not the better place to put the code in because the user can go to the next page also by clicking the tab of the page but in the real page there are also other controls I can use like a CheckBox that must be checked to enable the other controls).
My problem is that when the user is in the page N, I don't know how to reach the controls in page N-1 or N+1.
If I simply design 10 pages in the TabControl, all the same, there is no problem because each control on each page is a single object with a unique name so I have no problem to address it, but this is not the most elegant solution..
|
|
|
|
|
steve_9496613 wrote: and I created them in the design mode
Topic title says "created at runtime", not the designer. If you're using the designer then you can give the controls a unique name indeed, and use that - which is the elegant solution; they'd have a reference on the form-level. There's no need to look them up through the "current" tabpage, as far as the form is concerned, all pages exist.
steve_9496613 wrote: My problem is that when the user is in the page N, I don't know how to reach the controls in page N-1 or N+1.
The same as you reach the tabpage;
NumericUpDown1.Value = 1
|
|
|
|
|
Sorry, I was not able to explain clearly.
Now I try again.
In the designer I create:
1 Form with 1 TabControl with 1 page
1 UserControl with 1 Button and 2 NumericUpDown
(in my first post there is the code of the Form and the code of the UserControl)
At runtime I add 10 pages to the TabControl with a copy of the UserControl in each page.
My problem is in these 10 pages, for this reason I wrote "at runtime" in the topic title.
As you said, I know that if I create all the pages in the designer there is no problem but if I create all the pages at runtime I have to "design" just one page and to write code just for this page (less code I write, fewer errors I do... and then, if I have to change something in the page, I have to change just one page, not 10)
Is there a way to do what I want to do (hoping it is clear what I want to do...)?
|
|
|
|
|
steve_9496613 wrote: In the designer I create:
1 Form with 1 TabControl with 1 page
1 UserControl with 1 Button and 2 NumericUpDown
(in my first post there is the code of the Form and the code of the UserControl)
At runtime I add 10 pages to the TabControl with a copy of the UserControl in each page.
My problem is in these 10 pages, for this reason I wrote "at runtime" in the topic title.
Good explanation
"NUD1", the name-property of the UpDown control, must be unique within the form. It'll generate a variable with that name, and that's what "NUD1" points to; it's not a property of the tabpage, it's the name of a single control.
Ideally, you'd have your own type of tabpage, but I don't know whether the designer would accept that. Might make it impossible to show the page at all in the designer.
The "easy" way is to loop trough all the controls on the current tabpage. There's a Controls -collection that you'd have to loop through, and check whether that's the control (of type numericupdown) that you're looking for.
Not very elegant at all, is it?
So, I'd suggest putting everything you want on that page in a single UserControl , and make it's values available over properties. That way you can instantiate the same control ten times, and still have all the logic and layout in a single place.
|
|
|
|
|
Thanks for your reply.
Try and try at the end I've found a solution that I'll show you, but I have some questions before.
Eddy Vluggen wrote: "NUD1", the name-property of the UpDown control, must be unique within the form. It'll generate a variable with that name, and that's what "NUD1" points to; it's not a property of the tabpage, it's the name of a single control.
...and that was the problem: I put a NumericUpDown control in a UserControl and I give it an unique name (NUD1). When, at runtime, I create 10 instances of the UserControl, what are the unique names of the 10 NumericUpDown controls originated from NUD1?
If I make the value of the NumericUpDown available over a property like this (if I'm not wrong):
Public Property NUDVal() As Int32
Get
Return NUD1.Value
End Get
Set(ByVal value As Int32)
NUD1.Value = value
End Set
End Property
also this property has a unique name... until the UserControl is one, but when I create 10 instances of the UserControl?
So I thought to lists of objects... and that is my solution.
In the UserControl I created my two NumericUpDown controls at runtime, not in the designer, and I added them to an ArrayList. I also added a CheckBox, in the designer, to use the event "Click" for calculations that serve.
In Form1, the main form, when I create the 10 instances of the UserControl, I also add the ArrayList of controls of every UserControl to another ArrayList declared in the form... here is the code:
Public Class base
Public Objs As ArrayList = New ArrayList
Private n1, n2 As NumericUpDown
Sub New()
Dim pos As Point
InitializeComponent()
n1 = New NumericUpDown
pos.X = 209
pos.Y = 90
n1.Location = pos
Controls.Add(n1)
n2 = New NumericUpDown
pos.X = 209
pos.Y = 132
n2.Location = pos
Controls.Add(n2)
Objs.Add(n1)
Objs.Add(n2)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
CType(Parent.Parent, TabControl).SelectedIndex += 1
End Sub
Private Sub CheckBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.Click
Dim val1, val2, id, curid As Int32
id = CType(Parent.Parent, TabControl).SelectedIndex
curid = id - 1
If curid > 0 Then
val1 = CType(CType(Form1.ObjsUC.Item(curid - 1), ArrayList).Item(0), NumericUpDown).Value
val2 = CType(CType(Form1.ObjsUC.Item(curid - 1), ArrayList).Item(1), NumericUpDown).Value
n1.Value = val1 + val2
End If
End Sub
End Class
Public Class Form1
Public ObjsUC As ArrayList = New ArrayList
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim mypag As TabPage
Dim myuc As base
For i As Int32 = 0 To 10
mypag = New TabPage
myuc = New base
myuc.Parent = mypag
ObjsUC.Add(myuc.Objs)
mypag.Text = "Pag" & i.ToString
mypag.Name = "Pag" & i.ToString
TabControl1.TabPages.Insert(1 + i, mypag)
Next
End Sub
End Class
In this way all the controls I need are inside a list, and I know what kind of object is every element of the list because I put them in the list, I don't need to loop in the list and check each element.
So in the "Click" event of the CheckBox I can "reach" the values of the NumericUpDown controls of the previous page (or of other pages) pointing to them in the list.
Well... it works... although I have some difficulty in evaluate the elegance of this code...
What do you think about this solution?
And I have another question about the pages of the TabControl, but for this I open another topic...
|
|
|
|
|
steve_9496613 wrote: ...and that was the problem: I put a NumericUpDown control in a UserControl and I give it an unique name (NUD1). When, at runtime, I create 10 instances of the UserControl, what are the unique names of the 10 NumericUpDown controls originated from NUD1?
Good assesment; the others wouldn't have a name.
steve_9496613 wrote: also this property has a unique name... until the UserControl is one, but when I create 10 instances of the UserControl?
You'd have 10 controls without a name (the usercontrol), but with a known property that points to the correct control.
steve_9496613 wrote: Well... it works... although I have some difficulty in evaluate the elegance of this code...
What do you think about this solution?
It'll do; seen the technique before, usually when ex-VB programmers want an "array" of controls. It adds a single pointer to the list for each control, so it's not very resource-intensive.
|
|
|
|
|