Label position should fixed right and grow to left - vb.net

How can I set my labels to align on the right even when they have diffrent lenghts.
I have a set of labels which are occuring next to each other and also underneath each other.
The problem now is that they always align from the left within the label row,but I need them to align on the right as they are showing sums from other rows. Just to verify I am not talking about the text align I am looking for a solution to align my labels.
Thanks in advance

Simply set the AutoSize property to False in the designer. Adjust the size to fit the column. Then set the TextAlign to one of the right-alignment ones.

You should be able to do it at runtime using the following code:
'find the current right alignment position
Dim rightAlign As Integer = Label1.Left + Label1.Width
'set the text (assumes AutoSize is set to True)
Label1.Text = value
'adjust position so the right hand point is in the same position as before
Label1.Left = rightAlign - Label1.Width

My method is even more strange. I create the labels and then when laying out the fields for the report adjust the labels for number (etc) that are to be right aligned
Note: all labels end with 'lbl'
- txtNew is the report column text box.
- get the column's left edge plus the width of the column minus the width of the label. Works! Just not my favorite way to do it.
' *** NEED TO CALC POSITION FOR RIGHT JUSTIFY OF LABEL !!!!!
If ShouldRightJustify(rs.Fields(i).Type) Then
rpt.Section(acPageHeader).Controls(rs.Fields(i).Name & "lbl").Left = _
(lblCol + txtNew.Width) _
- rpt.Section(acPageHeader).Controls(rs.Fields(i).Name & "lbl").Width
End If

If you are asking how to do this from the designer, use the Format Menu.
Select all the controls you want to align, then click the control you want the other aligned to. Do Format > Align > Rights.
If you are trying to do this at run-time you can loop through the controls you want to align and set their .X property according to their width. For example. To align a label so that it's right side is at X=200... SomeLabel.X = 200 - SomeLabel.Width.

Related

Word VBA to return the height and width of a preselected image

I have a Word macro that changes the orientation and page size of an individual page to accommodate the image placed on the selected page. My existing macro code does the page and footer resizing correctly (footers are based on our existing footer styles), but I'd like to enhance the code to display the height and width of the selected image before the userform appears to help guide the user's page size choice.
The user could get that information by right-clicking the image, choosing "Picture...", and selecting the "Size" tab in the "Format Object" window, but I am trying to have the height/width displayed as part of the macro instead of asking the user to follow those steps.
Editors in my department sometimes encounter documents in which an image has been placed on a page that is not wide enough to accommodate the entire image, resulting in part of the image being "cut off." My plan has been to have the user select the image on the Word page, then run the macro; the image's height and width (in inches, ideally) should be displayed prior to the user to selecting the optimal page size.
I've tried working with Selection.ShapeRange or Selection.InlineShapes, but I haven't yet been able to get the current height and width of the selected image. Any help will be greatly appreciated.
Thank you,
KRS
Here's how I opted to handle this:
My macro was designed to start with the user selecting an inline shape on the Word page. With that selection, I start by setting variables for the selection's width and height in points, convert to inches, and display the results in a MsgBox:
myHeightPoints = .Height
myWidthPoints = .Width
`Convert the previously established variable to inches`
`Dim myHeightInches As Double
Dim myWidthInches As Double
myHeightInches = myHeightPoints / Application.InchesToPoints(1)
myWidthInches = myWidthPoints / Application.InchesToPoints(1)
`Trim the result to 2 decimal places`
Dim myHeightDisplay
Dim myWidthDisplay
myHeightDisplay = Format(myHeightInches, "#.00")
myWidthDisplay = Format(myWidthInches, "#.00")
`Display the result`
MsgBox ("The selected image is " & myHeightDisplay & " H X " & myWidthDisplay
& " W" & Chr(13) & "Choose a page size that accomodates the figure and its caption.")

Word VSTO - Finding the location of a shape in the document?

I am building a Word VSTO (VB.NET) program where I need to find the exact position in Points of a shape from Top, Left, Right and Bottom. I use the following code,
objShape = Globals.ThisAddIn.Application.ActiveDocument.Shapes(intShapesLoop)
objShape.RelativeHorizontalPosition = Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionMargin
objShape.Select()
sngPageWidth = Globals.ThisAddIn.Application.Selection.Range.PageSetup.PageWidth
sngPageHeight = Globals.ThisAddIn.Application.Selection.Range.PageSetup.PageHeight
sngMarginsLeft = objShape.Left + Globals.ThisAddIn.Application.Selection.Range.PageSetup.LeftMargin
sngMarginsRight = sngPageWidth - (objShape.Width + sngMarginsLeft + sngGutterPosistionRight)
This works fine and shows the correct location values from Left and Right. However, I use the below code for the Top and Bottom locations,
sngMarginsTop = objShape.Top + Globals.ThisAddIn.Application.Selection.Range.PageSetup.TopMargin
sngMarginsBottom = sngPageHeight - (objShape.Height + sngMarginsTop)
This shows the wrong position values. What is the issue here? From the Top value it shows about 12 Points less than the correct value
I found that this happens only on few documents. It shows the correct Top value on most documents but on few it shows the wrong Top value.
This is the reason why the top value is wrong,
In the Advanced Layout dialog (Text Wrapping > More Layout Options...),
The combo boxes marked by the red rectangles have to be set as Margin. The reason the top value was wrong is because the Absolute position ... below is set to Paragraph instead of Margin. When this is set to Margin the top value became correct.

Completely resize DataGridView in VB.NET

I am having issues resizing a DataGridView in a VB.NET application. The DataGridView is not bound to a data source, as all data is entered into it manually.
It is currently docked in a TableLayoutPanel, set to Fill, and I would expect it to automatically resize to fit its assigned cell, but it seems to have a minimum size, at which I can not shrink it any further. This is a problem because the tablet PC I am deploying to has a much smaller resolution, so the Windows must scale properly. The TableLayoutPanels are keeping everything in the correct position, but it is crucial that my grids scale as well so that the end user can see the bottom scroll bar and all the records in the table.
This problem is pretty much the same as the one mentioned here.
If you go to AlternatingRowsDefaultCellStyle in the properties window of the datagridviewer there is Padding feature that defaults to 0,0,0,0 which represents each direction. Perhaps if you reduce the padding it will autosize up instead of being limited on the upper end moving down.
Use AutoSizeMode and FillWeight properties of columns in your datagridview
'Column 1
Dim columnindex As Int32 = Me.DataGridView1.Columns.Add("One", "One")
With Me.DataGridView1.Columns(columnindex)
.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
.FillWeight = 80 'This can be used as percent value for column width
End With
'Column 2
columnindex = Me.DataGridView1.Columns.Add("Two", "Two")
With Me.DataGridView1.Columns(columnindex)
.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
.FillWeight = 20
End With
Me.DataGridView1.Rows.Add({"sample text", "1"})
Me.DataGridView1.Rows.Add({"another sample text", "2"})

vb.net Datagridview scrollbar won't show last column

I'm working with Visual studio 2012 - Visual Basic.net
In my DGV I set the .AutoSizeMode for each column as follows:
with dgv_Clients
.Columns("Name").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
.Columns("Phone").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
.Columns("Email").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
.Columns("Address").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
end with
This configuration makes all cells to show the full text inside and the last column will be as big as needed to fit the blank space.
The problem comes when I resize the form shorter than the 3 columns; the horizontal scroll bar is shown but it doesn't show the last column.
If I change the "AutoSizeMode" from "Fill" to "AllCells" the scroll bar works fine but if the form is resized too long there is an ugly blank space.
Question:
How can I make the scroll bar to show the last column when the "AutoSizeMode" is set to "Fill" ??
You can set a minimum width of the fill column like this:
.Columns("Address").MinimumWidth = 100
The default value of this property is 5.

How to move a rotated shape to a specific location in excel 2010 using vba

I've written some VBA code that automatically creates a chart. One of the axes on this chart doesn't use normal labels but a graphic. I've stored the graphic as an image and I use the .Copy and .Paste methods to get a copy of this image onto the chart.
Here is where it gets confusing. I need to rotate the image to get it aligned with the axis (using the .rotation property). But when I set the .top and .left properties the shape doesn't end up where I would expect. In fact setting the properties to 0 and 0 doesn't do what I would expect either. I've tried changing the order of the way I set the properties on the image object but it only appears in a different (wrong) location.
I'm sure I'm missing some vital aspect of how VBA/Excel is placing the object relative to what I'm setting the top and left properties to. Basically my goal is to make the image on the left side of the chart with the same width as the plot area's height (since the image is rotated I theorize this will make it the same size).
This code does not work:
Sheets(ImageSheet).Shapes("agreement").Copy
c.Chart.Paste
c.Chart.Shapes(1).Rotation=270
c.Chart.Shapes(1).width = c.Chart.PlotArea.height
c.Chart.shapes(1).left = 5
c.Chart.Shapes(1).top = c.Chart.PlotArea.top
I've also tried this code
c.chart.Shapes(1).top = c.chart.PlotArea.top + c.Chart.PlotArea.height
because I thought maybe it was calculating the "top" as the upper-left corner of the image object when it is not rotated (rotating 270 degrees makes this point in a place where it should align with the bottom of the plot area). But that doesn't do what I expected either.
The image is a skinny rectangle that acts as a label for the axis. The chart will end up being laid out like this: http://imgur.com/NrSXR and the axis label image would be something like this http://imgur.com/08EWU
What am I missing here?
Is it possible for you to align your chart into a position where the shape could rest align/on a cell?
IF YES then here is a suggestion:-
You could position shape into a cell. Then adjust the size to what you need. And rotate.
Then change its bring forward property be shown on the Chart.
Next Group Chart and the Shape
PS: I recorded a macro. However it's best if you could show us what your the exact picture (=how your sheeet/chart/image should look like) of your question.
I ended up rotating and resizing the image before copying and pasting to the chart and then positioning it. I had to use the IncrementLeft and IncrementTop methods rather than setting the left and top properties directly because that did not have the desired effect.
When doing the paste into the chart the object always ended up in the upper left hand-corner so I could increment to the left by the small amount I wanted as a margin I wanted there and increment the top by the value of PlotArea.top to align it with the plot area.
I was also surprised that when creating the copy of my image it retained the "name" i referred to it as when I copied it to the new sheet and chart. This was especially useful for positioning the image once it was on the chart.
I also needed execute this code at the very end of my procedure, after everything else had been positioned and aligned, or when I positioned the data labels for one of my series they wouldn't appear correctly.
Here is the code that I ended up using:
'make a copy of the label image and refer to it with a variable for convenience
Sheets(ImageSheet).Shapes("maturity").Copy
i = Sheets(ImageSheet).Shapes.Count
Sheets(ImageSheet).Paste
Dim axisImage As Shape
Set axisImage = Sheets(ImageSheet).Shapes(i + 1)
'rotate and resize the image
With axisImage
.Rotation = 270
.width = c.Chart.PlotArea.height
End With
'cut and paste the new image to the chart
axisImage.Cut
c.Chart.Paste
'position it in the chart using IncrementTop and IncrementLeft because setting the properties directly does not have the desired effect
c.Chart.Shapes("maturity").IncrementTop c.Chart.PlotArea.top
c.Chart.Shapes("maturity").IncrementLeft c.Chart.PlotArea.left - c.Chart.Shapes("maturity").height