|
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.
|
|
|
|
|
Let's assume you have a method similar to this one:
public void SomeMethod(string myParam) {
if (string.IsNullOrEmpty(myParam) {
throw new Exception();
}
}
When writing unit tests do you write tests that cover all of the variations of what could trip the exception?
i.e. one test each where
myParam = null
myParam = string.empty
myParam = " "
Or do you just write one of the above tests?
I tend to write all 3 tests. And, similarly, if a positive integer is required I'll have a test for the integer being 0 and one for it being < 0 since the operators are potentially different.
Just wondered what you guys thought, and if I'm overdoing the testing.
Andrew
"My days of not taking you seriously are certainly coming to a middle."
|
|
|
|
|
What you are "testing" is the IsNullOrEmpty function; I'll test with either one, fully confident that the method will behave as documented. When I test, I test my own code, not the frameworks' methods.
|
|
|
|
|
I guess my thought was that I testing to make sure someone didn't change the actual call to IsNullOrWhiteSpace . In theory a future dev could change my IsNullOrWhiteSpace to IsNullOrEmpty and effectively break the method by allowing it to accept an string of whitespace.
I might be being too much of a control freak though.
|
|
|
|
|
Andrew Stoute wrote: In theory a future dev could change my IsNullOrWhiteSpace to IsNullOrEmpty and effectively break the method by allowing it to accept an string of whitespace.
In that case, the only good test would be to compare the methods' body to the actual source-code you have now.
Don't let people change your code if they don't know what they're doing. Any nitwit could guess that if the method behaves different (translate to 'unexpected') that things will break. On a class-level, this is the L[^] from the SOLID principle. Simplest example; don't throw any exceptions that weren't already in the base-class (as the applications' try..catch constructs will not expect it, and break).
|
|
|
|
|
Andrew Stoute wrote: I might be being too much of a control freak though.
Not at all. The backup to this is documenting that it cannot accept whitespace and you may even rewite the code to strip leading and trailing whitespace characters to defend against it.
|
|
|
|
|
You're also testing what would happen based on the types of data that you're sending it. If, somewhere down the road, the data you're sending the method changes, you may need to document that the method can handle those changes as expected, such as sending a string of space and tab characters.
Personally, I would be checking to see if an actual string was passed in, THEN stripping the data of leading and trailing whitespace if required and then checking for an empty string. String.IsNullOrEmpty doesn't check to see if the string contains non-whitespace characters.
|
|
|
|
|
Dave Kreskowiak wrote: Personally, I would be checking to see if an actual string was passed in, THEN
The parameter only accepts strings, making this test superfluous.
Dave Kreskowiak wrote: stripping the data of leading and trailing whitespace if required and then checking for an empty string. String.IsNullOrEmpty doesn't check to see if the string contains non-whitespace characters.
Personally, I don't care for "whitespace"; if a user wants to store three spaces, a tab and a newline, then that's what gets stored. The only place where you "might" want to check it, is when validating input. Remember the NT4-server shutdown dialog? Required a non-null "reason". Did it help? No, a single period was "reason" enough.
|
|
|
|
|
Eddy Vluggen wrote: The parameter only accepts strings, making this test superfluous.
You would check for mull before trying to work with the string?? I don't see how that's superfluous.
Eddy Vluggen wrote: Personally, I don't care for "whitespace"; if a user wants to store three
spaces, a tab and a newline, then that's what gets stored. The only place where
you "might" want to check it, is when validating input.
User's aren't the only source of input that makes your code go "WTE!?" Foreign systems, poor XML files, non-normallized data in a database you inherit, corruption from a poor communications channel, ... the list goes on. You can get a blank or string formatted in some way your code doesn't expect from any number of sources. I was always taught that a method should be written to defend itself against pontentially bad data.
|
|
|
|
|
Dave Kreskowiak wrote: You would check for mull before trying to work with the string?? I don't see how that's superfluous.
"NullOrEmpty".
Dave Kreskowiak wrote: poor XML files
XML doesn't care much for whitespace.
Dave Kreskowiak wrote: I was always taught that a method should be written to defend itself against pontentially bad data.
I'm not putting an XML-validator in there
|
|
|
|
|
Hi!I want to give choice to users to view product image in list or in gallerie depending of how he want to view the product.As many e-commerce webite.How to do that?
|
|
|
|
|
If a question is this abstract, I can only suggest to hire a developer.
|
|
|
|
|
Hi, im trying to develop a program for win98, i work with 2.0 VS 2008, in the develop machine all runs ok. at client i receive
application has generated an exception that could not be handled 0xffe31701(-1894655), Thread ID=0xffe31395(1895531)
if someane knows wath can i do, please help.
and please don't start with the Win98. replace it it's not an option, its on board in a big machine.
Thanks.
|
|
|
|
|
I don't think you will get anywhere with this, Windows 98 has been dead a long time. The .NET framework was never designed to work under such an old version of Windows so it is unlikely that any .NET application will work.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Did you install the .NET Framework? Keep in mind that there's no EventLog , those classes will not work.
caradri wrote: and please don't start with the Win98. replace it it's not an option
It's also no longer a supported platform.
|
|
|
|
|
caradri wrote: and please don't start with the Win98. replace it it's not an option, its on
board in a big machine.
Sorry, but that is your problem. .NET is not designed to run on Windows 98; the minimum spec from Microsoft is Windows XP.
|
|
|
|
|
.NET's 2.0 minimum is Win2000, with Service Packs.
|
|
|
|
|
When I looked this up before answering, this[^] page indicated the min version was XP.
|
|
|
|
|
Fair enough. Only remembered it since I had a Win2k machine here somewhere with .NET 2. Wikipedia has more details than MSDN;
Version 2.0 without any Service Pack is the last version with support for Windows 98 and Windows Me. Version 2.0 with Service Pack 2 is the last version with official support for Windows 2000 although there have been some unofficial workarounds published online to use a subset of the functionality from Version 3.5 in Windows 2000.[5] Version 2.0 with Service Pack 2 requires Windows 2000 with SP4 plus KB835732 or KB891861 update, Windows XP with SP2 or later and Windows Installer 3.1 (KB893803-v2)
|
|
|
|
|
Never mind, I found the SP1 page which indicates the minimum is Win2K. Still not Windows 98 though.
|
|
|
|
|