|
OK.
Some more clues.
Since you are interested in the longest name, look at the first element (your code already does that) and set the LengthOfLongestName variable to its length (your code already does that), and set the LongName variable to the name of the first element (your code already does that). So you've made a good start.
Then look at the length of the next element and if, but only if it is greater than LengthOfLongestName , set LengthOfLongestName to this length and set LongName to the Name of this element.
Then do the same for the next element, and so on, until you have examined all of the elements.
When you have done this, LengthOfLongestName will hold the length of the longest name and LongName will hold the longest name itself.
That's it.
Iterating over an array is such a basic operation that there are thousands and thousands of examples in MSDN and elsewhere.
I'm not going to write it for you. Do some research find an example and try to code it yourself. To start with it doesn't have to do the longest name thing. Try just getting it to list the elements, then when you are sure you understand what's happening, do the longest name bit.
Once you have done some coding and got at least part of it working, come back if you get stuck.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Henry Minute wrote: Once you ... got at least part of it working, come back if you get stuck.
This begs for an article entitled "How to get unstuck at the very start?"
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
Picky, picky, picky.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Private Sub subLongest()
Dim arr(100) As String
arr(0) = "aa"
arr(1) = "bbb"
arr(2) = "cc"
arr(3) = "ddddd"
arr(4) = "aa"
arr(5) = "bbb"
arr(6) = "cc"
Dim maxLenIndex As Int16 = 0
Dim maxLen As Int16 = arr(0).Length
For ii As Int16 = 1 To 100
If arr(ii) IsNot Nothing Then
If arr(ii).Length > maxLen Then
maxLen = arr(ii).Length
maxLenIndex = ii
End If
End If
Next
MsgBox("Longest element is " & arr(maxLenIndex))
End Sub
|
|
|
|
|
OK.
That's your code.
You have made a lot of progress, since the last code-sample you posted. Well done!
Now what happens when you run it?
Do you get errors? If so, what are they?
Does it give an incorrect answer?
What is the problem that you need help with?
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
I know how to determine how many lines per printed page in .net
Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As System.Drawing.Printing.PrintPageEventArgs)
Dim linesPerPage As Single = 0
linesPerPage = ev.MarginBounds.Height / printingFont.GetHeight(ev.Graphics)
end sub
Is there a way I can determine how many characters will fit on a line before hitting the left margin, based on font?
I have a report that can get very long. I want to break long lines to print on separate pages so the report could be viewed on two pieces of paper side by side.
Thanks
Ryan
|
|
|
|
|
I think Graphics.MeasureString is what you are looking for. You can the size of print area, then you can find the size of text you are printing.
जय हिंद
|
|
|
|
|
I had been looking at the MeasureString method, and was not sure if there was a better way. I just want to clarify that there is not a better way to use MeasureString than the way I am doing it right now. By the way because this is a text report, I am using the Courier New font, so all characters are the same width. Code Snippet Below:
***************************************************************************************
Private Sub buildReport(ByVal ev As System.Drawing.Printing.PrintPageEventArgs)
Dim line As String = ""
Dim LinesIfPrinted As Int16 = 0
'Get the longest line of text in the report
For LineIndex As Int16 = 0 To strLines.Length - 1
'strLines is a text array with each line of the report
If strLines(LineIndex).Length > line.Length Then line = strLines(LineIndex)
Next
'Find Out How Many Pages
ev.Graphics.MeasureString(line, printingFont, ev.MarginBounds.Size, System.Drawing.StringFormat.GenericTypographic, CharactersPerPage, PagesToPrint)
'PagesToPrint is set to how many lines the longest text of the report is
'Thus I know how many pages this line will require.
ReDim LinesToPrint(PagesToPrint - 1, strLines.Length - 1)
'This is what I think could be more efficient. Currently I shorten the
'longest line of text by one ASCII character, until the LinesIfPrinted
'Variable is equal to 1. Than I know how many characters fit on a single
'line.
For CharIndex As Int16 = 1 To line.Length - 1
ev.Graphics.MeasureString(line.Substring(0, line.Length - CharIndex), printingFont, ev.MarginBounds.Size, System.Drawing.StringFormat.GenericTypographic, CharactersPerPage, LinesIfPrinted)
If LinesIfPrinted = 1 Then Exit For
Next
'Next I build the multidimenstional array with each line of text
'and if it is longer than CharactersPerPage I break it into separate
'pages.
<this code="" left="" out=""> End Sub
*************************************************************************************
So my question is if there is a more efficient way to determine how many characters fit on a line without using the For CharIndex as Int16 = 1....... block of code?
Thanks
Ryan
|
|
|
|
|
Yes and no. If the font you're using is a fixed-width font, then yes, you can figure it out. If you're using a proportional spaced font, then each line can take a different number of characters because each character takes up a variable width.
|
|
|
|
|
Doesn't the measurestring solve this? (provided he does it for every line)
|
|
|
|
|
To a point. There's some extra math you have to do to get just the one line in characters. If you send a string that's too big, it'll wrap down the the next line, or just cut off the rendered image, but not tell you how many characters are on the line that you wanted.
|
|
|
|
|
True, didn't say it was easy just possible
|
|
|
|
|
Nothing is ever easy, is it??
|
|
|
|
|
hi
modified 28-Nov-12 8:52am.
|
|
|
|
|
vijay2482 wrote:
If m <> -1 Then
If m = -1 Then
there you have your problem. You got m <> -1 and m = -1 . Well only one of them is true. Don't you think so?
Yusuf
Oh didn't you notice, analogous to square roots, they recently introduced rectangular, circular, and diamond roots to determine the size of the corresponding shapes when given the area. Luc Pattyn[^]
|
|
|
|
|
m = name.IndexOf("Groupe")
If m <> -1 Then
sw.WriteLine(LTrim(name))
End If
this is right...
but even then i dont get the output
|
|
|
|
|
So what are you expecting LTrim to return?? It removes all whitespace characters from the left end of a String, like Tab and Space. If there are character values higher than these than don't have a glyph image, or has just a box image, those characters will not get removed.
You'll have to explain what you mean by "doesn't work".
|
|
|
|
|
i want to shift the line left by 2 characters
|
|
|
|
|
No kidding, you already said that.
What I asked you is, do you know what LTrim does?? How is it suppoed to know you want to move the characters to the left, by 2 places, since you didn't specify anything like that in any of your code?
|
|
|
|
|
vijay2482 wrote: f the index "Groupe" is found in name,i want to write the line altering the name by shifting it left by 2 places...
To me it sounds like you need to do a mid (getting a section of a string) vs instead of a LTRIM (trimming spaces).
vijay2482 wrote: If m = -1 Then
Agreed w/ above poster - that's whack.
Any suggestions, ideas, or 'constructive criticism' are always welcome.
"There's no such thing as a stupid question, only stupid people." - Mr. Garrison
|
|
|
|
|
Hi all
Quick question, how do I format a number, say 34 to 034, just for better presentation? (I'm currently using String.Format("{0}", newPartNumber) )
Thanks
|
|
|
|
|
|
Use tostring Function to Convert string Format. .Tostring("Formatofdata")
Val1= 1
Str1 = Val1.ToString("000")
//Value of Str1is 001
Best Regard
Anubhava Dimri
mailto: anubhava.prodata@gmail.com
|
|
|
|
|
here is my code
<dim openfiledialog="" as="" new="" mode="hold"> Dim filetoread As String
OpenFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
OpenFileDialog.ShowDialog()
filetoread = OpenFileDialog.FileName
Dim filestream As StreamReader
filestream = File.OpenText(filetoread)
Dim readcontents As String
readcontents = filestream.ReadLine
Do While Not filestream.EndOfStream
readcontents = filestream.ReadLine
Loop
Dim textdelimiter As String
textdelimiter = ","
Dim splitout = Split(readcontents, textdelimiter)
txtt24import.Text &= readcontents
filestream.Close()
Dim sql As String = ""
If txtt24import.Text.ToString = "" Then
End
End If >
|
|
|
|
|
So where are you stuck?
What is happening and what should be happening?
Any error?
From what I can see you read all the lines off a text file line per line (you could simply use the method 'readtoend' for that) and each time overwrite your variable 'readcontents' so you actually only have the last line in the file.
|
|
|
|