|
Richard MacCutchan wrote: Uninitialised variables generally contain garbage, and could quite conceivably contain some extremely large negative value.
That's true in C/C++, but not in .NET code. The variable will always be initialized to its default value.
However, it's still a good idea to explicitly initialize it. In C#, you'd get a compiler error if you didn't.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
that's true. it isn't the same as C/C++
Thanks Richard
|
|
|
|
|
Thank you; I should know this, and I am sure it has been explained more than once before. But at my advanced age there is so much rubbish rattling around in my head ...
|
|
|
|
|
You're welcome
Never mind. Everybody will atteind the advanced age Mr Richard
|
|
|
|
|
thanks Richard,
even when i initialise iRow to zero and use it as Integer, nothing changes.
and when i use an Integer, it will be automatically initialised to zero
|
|
|
|
|
OK, so that is a starting point. Now you need to step through the code with your debugger to find out why the loop never terminates.
|
|
|
|
|
Quote: Sascha wrote :
If a loop gets executed indefinitely it's because its condition to keep running keeps evaluating to true. So, if it's the while-loop that executes indefinitely
While (iRow <= (DataGridView_impot.Rows.Count - 1))
then it has to be because iRow always has a value lower than the amount of rows. That's not intended of course - so you would have to find out (using the debugger) why this is so in order to fix it.
And that's true and i'm trying to fix it
Any suggestions ?!
modified 26-Jan-16 9:45am.
|
|
|
|
|
We really can't step through the code by hand because we don't have all of the information your code does. You have to use the debugger to step through the code and inspect the contents of the variables involved.
Something doesn't have values you expect it to have and/or doesn't behave the way you think it does.
|
|
|
|
|
Thank you Dave, I will check that
|
|
|
|
|
Dorsaf Ouersighni wrote: Any suggestions Yes. Do what everyone is telling you, and use your debugger to find out why the loop does not terminate.
|
|
|
|
|
Thanks Richard,
I found the problem and I solve it without debugger. It was just an additional ( )
|
|
|
|
|
Hello everyone,
I've been looking for the code to print my full DataGridView with Visual Basic 2010 Express for a long period and I tested all codes that are displayed with searches on google but in vain.
But finally I find a C # code that I converted to VB which suits me perfectly but the only problem is that it runs infinitely (infinite loop at the number of pages)
NOTE: The code doesn't generate an error and it's an urgent case because I need it for my job.
Thanks,
Here is my code:
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Try
Dim iHeaderHeight As Double
Dim iLeftMargin As Integer = e.MarginBounds.Left
Dim iTopMargin As Integer = e.MarginBounds.Top
Dim bMorePagesToPrint As Boolean = False
Dim iTmpWidth As Integer = 0
If bFirstPage Then
For Each GridCol As DataGridViewColumn In DataGridView_impot.Columns
iTmpWidth = CType(Math.Floor(CType((CType(GridCol.Width, Double) / (CType(iTotalWidth, Double) * (CType(iTotalWidth, Double) * (CType(e.MarginBounds.Width, Double) / CType(iTotalWidth, Double))))), Double)), Integer)
iHeaderHeight = (CType(e.Graphics.MeasureString(GridCol.HeaderText, GridCol.InheritedStyle.Font, iTmpWidth).Height, Integer) + 11)
arrColumnLefts.Add(iLeftMargin)
arrColumnWidths.Add(iTmpWidth)
iLeftMargin = (iLeftMargin + iTmpWidth)
Next
End If
Dim iRow As Double
While (iRow <= (DataGridView_impot.Rows.Count - 1))
Dim GridRow As DataGridViewRow = DataGridView_impot.Rows(iRow)
iCellHeight = (GridRow.Height + 5)
Dim iCount As Integer = 0
If (iTopMargin + (iCellHeight >= (e.MarginBounds.Height + e.MarginBounds.Top))) Then
bNewPage = True
bFirstPage = False
bMorePagesToPrint = True
Exit While
Else
If bNewPage Then
e.Graphics.DrawString(Me.lbl_rs.Text, New Font(DataGridView_impot.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left, (e.MarginBounds.Top - (e.Graphics.MeasureString(Me.lbl_rs.Text, New Font(DataGridView_impot.Font, FontStyle.Bold), e.MarginBounds.Width).Height - 13)))
e.Graphics.DrawString(Me.lbl_date_now.Text, New Font(DataGridView_impot.Font, FontStyle.Bold), Brushes.Black, (e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(Me.lbl_date_now.Text, New Font(DataGridView_impot.Font, FontStyle.Bold), e.MarginBounds.Width).Width)), (e.MarginBounds.Top - (e.Graphics.MeasureString(Me.lbl_rs.Text, New Font(New Font(DataGridView_impot.Font, FontStyle.Bold), FontStyle.Bold), e.MarginBounds.Width).Height - 13)))
iTopMargin = e.MarginBounds.Top
For Each GridCol As DataGridViewColumn In DataGridView_impot.Columns
e.Graphics.FillRectangle(New SolidBrush(Color.LightGray), New Rectangle(CType(arrColumnLefts(iCount), Integer), iTopMargin, CType(arrColumnWidths(iCount), Integer), iHeaderHeight))
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(CType(arrColumnLefts(iCount), Integer), iTopMargin, CType(arrColumnWidths(iCount), Integer), iHeaderHeight))
e.Graphics.DrawString(GridCol.HeaderText, GridCol.InheritedStyle.Font, New SolidBrush(GridCol.InheritedStyle.ForeColor), New RectangleF(CType(arrColumnLefts(iCount), Integer), iTopMargin, CType(arrColumnWidths(iCount), Integer), iHeaderHeight), strFormat)
iCount = (iCount + 1)
Next
bNewPage = False
iTopMargin = (iTopMargin + iHeaderHeight)
End If
iCount = 0
For Each Cel As DataGridViewCell In GridRow.Cells
If (Not (Cel.Value) Is Nothing) Then
e.Graphics.DrawString(Cel.Value.ToString, Cel.InheritedStyle.Font, New SolidBrush(Cel.InheritedStyle.ForeColor), New RectangleF(CType(arrColumnLefts(iCount), Integer), CType(iTopMargin, Single), CType(arrColumnWidths(iCount), Integer), CType(iCellHeight, Single)), strFormat)
End If
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(CType(arrColumnLefts(iCount), Integer), iTopMargin, CType(arrColumnWidths(iCount), Integer), iCellHeight))
iCount = (iCount + 1)
Next
End If
iRow = (iRow + 1)
iTopMargin = (iTopMargin + iCellHeight)
End While
If bMorePagesToPrint Then
e.HasMorePages = True
Else
e.HasMorePages = False
End If
Catch exc As Exception
MessageBox.Show(exc.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
|
|
|
|
|
Have you considered using a reporting tool. Spending hours/days outputting lousy formatted grid content can be replaced by using SSRS fairly rapidly. With a reporting tool you get a MUCH better output and, once you get to know the tool, a much faster production.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
thanks for your fast and polite answer.
I'm a beginner in programming and I need just some help if you don't mind not some break down
|
|
|
|
|
Can you explain me more and how can I use a reporting tool?
Thanks
|
|
|
|
|
I'm creating an application in vb.net 2013 , where i need to implement a way to read from USB barcode readers.
I've read somewhere that most of barcode readers should be treated like simple keyboards.
But this is the problem.
On my Main form , i have several textboxes.
One of them is Barcode Textbox , where a user can write the barcode manually from keyboard, or can read from barcode reader.
On other textboxes , only manual write from keyboard should be permitted and not from barcode reader.
So is there a way to make this possible ?
And if someone know this Microsoft Library : Microsoft Point of Service for .NET v1.14 (POS for .NET) from Official Microsoft Download Center[^]
Can this be a help for managing a USB Barcode Reader and to solve my problem ?
Thank you !
|
|
|
|
|
The .NET CLR abstracts keyboard devices away so that by .NET standard means you have no way of telling from which keyboard device a keypress event originated from. There's a Windows "Raw Input API" but it's not part of .NET so you would have to use P/Invoke ("DllImport") or a C++/CLI bridge if there wasn't this nice CP article (which uses P/Invoke):
Using Raw Input from C# to handle multiple keyboards[^]
A small drawback for you might be that it's written in C# but as you probably know, this doesn't stop you at all from using it in VB. I used it for exactly that purpose you're asking for.
I didn't know about that Microsoft Library you've linked. I took a look at it and at first glance, I don't think it's suitable.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Actually , there's an article published on codeproject about that library Controlling a Barcode Reader with POS for .NET[^]
But it seems that has problems with barcode readers that are configured as a keyboard. I don't know what configuration should I make to use it.
|
|
|
|
|
To me that library is a MS documentation fail. It's not clear to me if it would simplify reading from a barcode scanner at all, with keyboard emulation or without. Instead it's something about "managing" POS devices, something I don't need if I just want to read from a barcode scanner. The Demo-App in the SDK doesn't clear things up either; I guess you already have to be an experienced POS developer to know what's what there. So for simply reading from a barcode scanner I would use the approach from the CodeProject article I linked before. It does all I need, does it well and the article explains what's going on there.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Ok , the article that you are suggesting to use , is going to work with all kind of barcode readers , or only with those that are configured as Keyboards ?
|
|
|
|
|
The intended purpose of that article is to being able to know from which keyboard device a keypress came from. So it only works for barcode scanners with keyboard emulation. To my knowledge, that's probably all scanners in the price range up to $500 give or take. I haven't dealt with more expensive scanners yet.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
|
He writes that the CodeProject article was his starting point. So it's mostly the same thing (Raw Input API); but I haven't read it all yet so I can't say if it's in some way better than the solution of the CP article or not.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
bonjour à Tous,
sa fait longtemps que je cherche du code pour imprimer mon DataGridView complet avec visual basic 2010 express et j'ai testé tous les codes qu'ils s'affichent avec des recherches sur google mais en vain.
Mais finalement, j'ai trouver un code c# que j'ai converti en VB qui me convient parfaitement mais le seul problème c qu'il boucle infiniment ( boucle infini au niveau du nombre de pages )
NB: Le code ne génère aucune erreur
Voici mon code :
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Try
Dim iHeaderHeight As Double
Dim iLeftMargin As Integer = e.MarginBounds.Left
Dim iTopMargin As Integer = e.MarginBounds.Top
Dim bMorePagesToPrint As Boolean = False
Dim iTmpWidth As Integer = 0
If bFirstPage Then
For Each GridCol As DataGridViewColumn In DataGridView_impot.Columns
iTmpWidth = CType(Math.Floor(CType((CType(GridCol.Width, Double) / (CType(iTotalWidth, Double) * (CType(iTotalWidth, Double) * (CType(e.MarginBounds.Width, Double) / CType(iTotalWidth, Double))))), Double)), Integer)
iHeaderHeight = (CType(e.Graphics.MeasureString(GridCol.HeaderText, GridCol.InheritedStyle.Font, iTmpWidth).Height, Integer) + 11)
arrColumnLefts.Add(iLeftMargin)
arrColumnWidths.Add(iTmpWidth)
iLeftMargin = (iLeftMargin + iTmpWidth)
Next
End If
Dim iRow As Double
While (iRow <= (DataGridView_impot.Rows.Count - 1))
Dim GridRow As DataGridViewRow = DataGridView_impot.Rows(iRow)
iCellHeight = (GridRow.Height + 5)
Dim iCount As Integer = 0
If (iTopMargin + (iCellHeight >= (e.MarginBounds.Height + e.MarginBounds.Top))) Then
bNewPage = True
bFirstPage = False
bMorePagesToPrint = True
Exit While
Else
If bNewPage Then
e.Graphics.DrawString(Me.lbl_rs.Text, New Font(DataGridView_impot.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left, (e.MarginBounds.Top - (e.Graphics.MeasureString(Me.lbl_rs.Text, New Font(DataGridView_impot.Font, FontStyle.Bold), e.MarginBounds.Width).Height - 13)))
e.Graphics.DrawString(Me.lbl_date_now.Text, New Font(DataGridView_impot.Font, FontStyle.Bold), Brushes.Black, (e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(Me.lbl_date_now.Text, New Font(DataGridView_impot.Font, FontStyle.Bold), e.MarginBounds.Width).Width)), (e.MarginBounds.Top - (e.Graphics.MeasureString(Me.lbl_rs.Text, New Font(New Font(DataGridView_impot.Font, FontStyle.Bold), FontStyle.Bold), e.MarginBounds.Width).Height - 13)))
iTopMargin = e.MarginBounds.Top
For Each GridCol As DataGridViewColumn In DataGridView_impot.Columns
e.Graphics.FillRectangle(New SolidBrush(Color.LightGray), New Rectangle(CType(arrColumnLefts(iCount), Integer), iTopMargin, CType(arrColumnWidths(iCount), Integer), iHeaderHeight))
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(CType(arrColumnLefts(iCount), Integer), iTopMargin, CType(arrColumnWidths(iCount), Integer), iHeaderHeight))
e.Graphics.DrawString(GridCol.HeaderText, GridCol.InheritedStyle.Font, New SolidBrush(GridCol.InheritedStyle.ForeColor), New RectangleF(CType(arrColumnLefts(iCount), Integer), iTopMargin, CType(arrColumnWidths(iCount), Integer), iHeaderHeight), strFormat)
iCount = (iCount + 1)
Next
bNewPage = False
iTopMargin = (iTopMargin + iHeaderHeight)
End If
iCount = 0
For Each Cel As DataGridViewCell In GridRow.Cells
If (Not (Cel.Value) Is Nothing) Then
e.Graphics.DrawString(Cel.Value.ToString, Cel.InheritedStyle.Font, New SolidBrush(Cel.InheritedStyle.ForeColor), New RectangleF(CType(arrColumnLefts(iCount), Integer), CType(iTopMargin, Single), CType(arrColumnWidths(iCount), Integer), CType(iCellHeight, Single)), strFormat)
End If
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(CType(arrColumnLefts(iCount), Integer), iTopMargin, CType(arrColumnWidths(iCount), Integer), iCellHeight))
iCount = (iCount + 1)
Next
End If
iRow = (iRow + 1)
iTopMargin = (iTopMargin + iCellHeight)
End While
If bMorePagesToPrint Then
e.HasMorePages = True
Else
e.HasMorePages = False
End If
Catch exc As Exception
MessageBox.Show(exc.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
|
|
|
|
|
This is an English-language site. Please edit your question and post it in English.
Ceci est un site de langue anglaise. S'il vous plaît modifier votre question et l'afficher en anglais.
(Google Translate[^])
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|