GetWidthPoint not returning expected value - vb.net

I'm using the iTextSharp to export reports to PDF and I have a table with all content that I need.
I want to give columns width based on the most wide text of each column, but the GetWidtPoint() method returns text width that I'm not expecting and the column content jumps to the next line.
Below, there is an example about the calculus of the header text width. I put all my table header inside an array first.
Dim ColSize(10) as single
Dim Ck as Chunk
For I = 0 to Colsize.getUpperBound(0)
Ck = new Chunk(Data(I),myFont)
Ck.SetCharacterSpacing(1)
Colsize(I) = Ck.GetWidthPoint
Next I
Finally, the table declaration...
Dim T as new PdfPtable(11)
T.HorizontalAligment = 0
T.WidthPercentage = 100
T.SpacingBefore = 3
T.setWidths(Colsize)
An example about issue:
Through the calculus method that I used, the GetWidthPoint() returns 33 points to "Produto" word, but if i set the column width to 33f so I cant show this text only in the first line. To Show the entire word in the same line, I need to set the column width to 55f.
When i print the text in the cell...
Dim Cell as new PdfpCell(new Phrase(new Chunk(Data(I,X),Font)))
T.addCell(cell)
Any ideas?

Don't forget the cell margins, they are 2 by default, 4 in total. You are not posting the way you are creating the cell but I'm guessing that it's introducing even more margins.

Related

Highlight row cell if value in column a doesn't match in column b in devexpress

can somebody help me, how to highlight row cell if value in column a doesn't match in column b in Devexpress VB.NET.
Here is the screenshot
Take a look at the Conditional Formatting feature which allows you to change the appearance of individual cells or rows based on specific conditions.
I believe, the FormatConditionRuleExpression will be helpful:
Imports DevExpress.XtraEditors
Imports DevExpress.XtraGrid
Dim gridFormatRule As New GridFormatRule()
Dim formatConditionRuleExpression As New FormatConditionRuleExpression()
gridFormatRule.Column = colA
gridFormatRule.ApplyToRow = True
formatConditionRuleExpression.PredefinedName = "Red Fill, Red Text"
formatConditionRuleExpression.Expression = "[A] != [B]"
gridFormatRule.Rule = formatConditionRuleExpression
gridView1.FormatRules.Add(gridFormatRule)

"Range" object must move along when adding a new row

I'm trying to color different rows depending on an if-statement. Example:
' # 1
If (test = True) Then
Worksheets("sheet1").Range("A1:J1").Interior.Color = varColor1
Else
Worksheets("sheet1").Range("A1:J1").Interior.Color = varColor2
End If
' # 2
If (test2 = True) Then
Worksheets("sheet1").Range("A2:J2").Interior.Color = varColor1
Else
Worksheets("sheet1").Range("A2:J2").Interior.Color = varColor2
End If
' # 3
'...etc
My question is, if I add a new row in the excel sheet, the value of the "Range" becomes incorrect. For instance, if I add a row between A and B the program will color the new inserted row (as this new inserted row becomes B) and the actual B row that I want to color is now C. How can I ensure that when a row is added, the correct row is still colored. Ofcourse I can change the "Range" values manually but I have a lot of them so it will take very long to change all, there must be another way to do it...
If you define a named range (Ctrl-F3) XL will expand the definition if you insert a column within that range.
Worksheets("sheet1").Range("test1_range").Interior.Color = varColor1
Be careful if you insert a column at left edge of the named range - it will not be included into the named range.

Runtime error 91: object variable or with block variable not set

I'm running a macro in Word which, among other things, adds a line to the bottom of a table already existing in the document and fills certain cells. The odd thing is that for the majority of the documents it Works, however there are a couple of documents for which I receive the Run Time error 91.
'Update the document properties, updates the header, updates the table of contents,
' and adds a file to the Version History table.
Sub zzAddVersionHistory(strUsuario As String, strDescripcion As String)
Dim newDate As String
Dim rowNumber As Integer
Dim rowNew As Row
Dim strIssue As String
Dim ascIssue As Integer
'Updates the Date property
newDate = Format(Date, "dd/MM/yyyy")
ActiveDocument.CustomDocumentProperties("Date").Value = newDate
'Finds the version from the Issue property and updates the version
If DocPropertyExists("Issue") = True Then
strIssue = ActiveDocument.CustomDocumentProperties("Issue").Value
ascIssue = (Asc(strIssue)) + 1 'Convierte el Issue en ascii y le suma uno
strIssue = Chr(ascIssue) 'Convierte el ascii en caracter
ActiveDocument.CustomDocumentProperties("Issue").Value = strIssue
End If
'Updates Header and footer
zzActualizarHeaderFooter
'Updates Fields
zzActualizarCampos
'Accepts changes in header y footer
zzAcceptChangesInHeaderFooter
'Adds a row to the table
rowNumber = Application.ActiveDocument.Tables(1).Rows.Count
Set rowNew = Application.ActiveDocument.Tables(1).Rows.Add
'Inserts KTC Issue In first cell of the new row
rowNew.Cells(1).Range.InsertAfter (strIssue) ''' Runtime-error here
'Inserts Issued By in the third cell of the new row
rowNew.Cells(3).Range.InsertAfter (strUsuario)
'Inserts the Date in the fourth cell of the new row
rowNew.Cells(4).Range.InsertAfter (newDate)
'Inserts Description of Changes in the fifth cell of the new row
rowNew.Cells(5).Range.InsertAfter (strDescripcion)
'Updates the Table of Contents
zzActualizarIndices
End Sub
If needed I can provide the subs and functions called by the macro, but I don't think they have anything to do with the issue.
I believe the problem is somewhere in those documents, in the table format, but I could not find an explanation anywhere nor I can find any difference with the tables in other documents.
Nested tables mess up the cells collection. Once you manually merge/split cells on the last row and then add a new row, things become... different. Save as rtf, look at the code, and scratch your head.
Use one (the first? second?) "standard" row to count the columns and adjust the code in case the column count / cells count of the last row differs from that "norm". Use "Selection" and a breakpoint to investigate the troublesome table to learn how to handle these special cases.

How can i get a large number of text boxs to subtract from each other

I'm trying to make a program where a user inputs numbers into a subtraction equation and the program tells them if they are right or wrong and what the correct answer is in a label. There are 20 different equations with 3 text boxes each. The first two text boxes are for the two numbers that are being subtracted and the third text box is the answer. I declared them into a array but I can't figure out how make them subtract. The code i have so far is:
Dim i As Integer
Dim txtNumber1() As TextBox = {txt1Number1, txt2Number1, txt3Number1, txt4Number1, txt5Number1, txt6Number1, txt7Number1, txt8Number1, txt9Number1, txt10Number1, txt11Number1, txt12Number1, txt13Number1, txt14Number1, txt15Number1, txt16Number1, txt17Number1, txt18Number1, txt19Number1, txt20Number1}
Dim txtNumber2() As TextBox = {txt1Number2, txt2Number2, txt3Number2, txt4Number2, txt5Number2, txt6Number2, txt7Number2, txt8Number2, txt9Number2, txt10Number2, txt11Number2, txt12Number2, txt13Number2, txt14Number2, txt15Number2, txt16Number2, txt17Number2, txt18Number2, txt19Number2, txt20Number2}
Dim txtAnswer() As TextBox = {txt1Answer, txt2Answer, txt3Answer, txt4Answer, txt5Answer, txt6Answer, txt7Answer, txt8Answer, txt9Answer, txt10Answer, txt11Answer, txt12Answer, txt13Answer, txt14Answer, txt15Answer, txt16Answer, txt17Answer, txt18Answer, txt19Answer, txt20Answer}
Dim intAnswer() As Integer
For i = 0 To txtNumber1.Length - 1
intAnswer(i) = txtNumber1(i) - txtNumber2(i)
Next
I also can't figure out how i would make each answer display into a label. I think it would be some like
If intAnswer(0) = txtAnswer(0) Then
Me.lblAnswer1.Text = "Correct:" & intAnswer(0)
Else
Me.lblAnswer1.Text = "Incorrect:" & intAnswer(0)
End If
But I'm not sure how i would loop that to make it do all 20 labels, or would i just need to have it 20 different times, one for each label.
Thanks for the help.
Best to create a user control with 3 labels and 3 textboxes on each. Then you only need to code this much, and wrap this logic in a loop to repeat as many times as you want. Basically, narrow down your problem to "I only have 1 equation", solve it using this approach, the rest is as easy as using adding a loop to your code.

How to autosize/resize the columns of a table in Lotus using VBA?

I am coding up a VBA script to create & email a table of data. And while it does work, it's also ugly. One thing I'd like to do is resize these columns so they don't take up the entire width of the page.
The "autosize column" option would be nice, except I don't see anywhere in the API. Alternatively, I'm open to manually setting the widths of each column. Can someone tell me what code I need to add?
Dim rtNav As NotesRichTextNavigator
Dim rtTbl As NotesRichTextTable
Dim TblHeader_FontStyle As NotesRichTextStyle
Dim TblBody_BackgroundStyle As NotesRichTextStyle
Dim TblHeader_BackgroundStyle As NotesColorObject
Dim TblBody_FontStyle As NotesColorObject
Sub AppendTable()
'Define styles
Set TblHeader_BackgroundStyle = NtSession.CreateColorObject
TblHeader_BackgroundStyle.NotesColor = COLOR_DARK_BLUE
Set TblHeader_FontStyle = NtSession.CreateRichTextStyle
TblHeader_FontStyle.NotesColor = COLOR_WHITE
TblHeader_FontStyle.FontSize = 8
Set TblBody_FontStyle = NtSession.CreateColorObject
TblBody_FontStyle.NotesColor = COLOR_WHITE
Set TblBody_BackgroundStyle = NtSession.CreateRichTextStyle
TblBody_BackgroundStyle.NotesColor = COLOR_BLACK
TblBody_BackgroundStyle.FontSize = 10
'-----------------------------------------------------
'Make table structure
NtBod.AppendTable lRowCount:=1, lColumnCount:=5
Set rtNav = NtBod.CreateNavigator
Set rtTbl = rtNav.GetFirstElement(RTELEM_TYPE_TABLE)
rtTbl.Style = TABLESTYLE_TOP
Call NtBod.AppendStyle(TblHeader_FontStyle)
Call rtTbl.SetColor(TblHeader_BackgroundStyle)
rtNav.FindFirstElement (RTELEM_TYPE_TABLECELL)
'The rest of the procedure to navigate the table and insert the data goes here
There is a parameter in the AppendTable method that lets you specify the style for each column, including the width.
From the AppendTable method documentation:
Call
notesRichTextItem.AppendTable(rows%,
columns% [, labels] [,leftMargin&]
[, rtpsStyleArray] )
Parameters:
rows% Integer. Number of rows in the table.
columns% Integer. Number of columns in the table.
labels Array of type String. Optional. Text of labels for a tabbed
table. The number of array elements
must equal the number of rows.
Omitting this parameter appends a
basic table. Including this parameter
appends a tabbed table.
leftMargin& Long. Optional. Left margin of the table in twips.
Defaults to 1440. The following
constants are available:
RULER_ONE_CENTIMETER (567)
RULER_ONE_INCH (1440)
rtpsStyleArray Array of type NotesRichTextParagraphStyle. Optional.
Creates a table with fixed-width
columns and style attributes as
specified. Omitting this parameter
creates an auto-width table. The
array must contain one element for
each column in the table in sequence.
Explicitly set the first line left
margin and left margin, which control
the start of text relative to the
start of the column, and the right
margin, which controls column width.