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.
Related
This is really quite ridiculous, but I have a picture box called picWeather, and it covers the whole form and looks like rain, and I want it to look like it's falling. This code does it perfectly, but goes up instead of down?!?!?!?!!?!
picWeather.Top = picWeather.Top - 50 'Moves rain up by 50
picWeather.Height = picWeather.Height + 50 'Adds 50 to the height of the rain
If picWeather.Top < -10000 Then 'If the rain is 10000 above the form then
picWeather.Top = Me.Top 'Set the position of the top of the rain to the top of the form
picWeather.Height = Me.Height 'Set the height of the rain to the height of the form
End If 'End the if statement
It's so simple, but because moving something by the bottom is read only and when you add height to something it adds it to the bottom, it's confusing... Anyone able to get there head around this?
Y coordinates are measured down from the top of the form. If you want to move something downward then you need to INCREASE the Top value, not DESCREASE it.
You should only be setting Left, Top, Width and Height if you only want to set one of them. If you want to set multiple then do one of the following:
Set Location to set Left and Top.
Set Size to set Width and Height.
Set Bounds to set Location and Size.
Call SetBounds to set any combination of Left, Top, Width and Height.
In your case, you want to set Top and Height so you should be calling SetBounds. That said, is there actually any point changing the Height? If you want the rain to initially cover the whole form and appear to be moving down then you need the PictureBox to have an initial Height that is greater then the form and a Top that is less than zero. As you move the PictureBox down, the Top approaches zero and you can reset when it gets there, e.g.
If picWeather.Top = 0 Then
'Move the rain back up to its starting position.
picWeather.Top = -10000
Else
'Move the rain down 50 pixels.
picWeather.Top += 50
End If
As a physical analogy, let's say that you are looking through a window at a picture that I'm holding. The picture is much taller than the window. I start by positioning the picture such that its bottom edge lines up with the bottom edge of the window. I slowly move it down and you see "rain falling". When the top of the picture gets level with the top of the window, I quickly move picture back up to its starting position. That's exactly what that code does: it moves the control down by increasing the Top value and then, when the top edge lines up with the top edge of the form, it immediately the control back to its starting position with its top edge well above that of the form.
I am working on a personal home inventory database. On one of the forms called Update Location, there is an image of the floor plan of the condo and then on top an image of location flag (pin icon) indicating where the location is in the condo.
For example:
Room: Den
Location: Top of desk
Flag is at point
LeftCoordinate.Value (X) and
TopCoordinate.Value (Y).
I have a toggle button then allows me to click on the map and sets a new X and Y position for the flag based on where I click on the map.
However, when I do this the flag image disappears behind the floor plan image until I click on another part of the form and then it is in the right spot. I am fairly new to VBA (especially in Access). Is there a way I can get the flag image on top?
I have tried searching Google but all the examples tend to be for one image and never an image over another image.
Private Sub Draw_Location_Icon()
Me.Painting = False
LeftStartPoint = 5292 ' 3.675" Left corner of floor plan attachment
TopStartPoint = 837 ' 0.5826" Top corner of floor plan attachment
IconHalfWidth = 180 ' 0.125" Half the icon width
IconFullHeight = 360 ' 0.250" Full height of icon
Me.Location_Icon.Visible = False
If Me.LeftCoordinate.Value > 0 Or Me.TopCoordinate.Value > 0 Then
Location_Icon.Left = LeftStartPoint - IconHalfWidth + Me.LeftCoordinate.Value
Location_Icon.Top = TopStartPoint - IconFullHeight + Me.TopCoordinate.Value
Me.Location_Icon.Visible = True
End If
Me.Painting = True
End Sub
As I stated currently the floor plan image is on top of the flag when I move the location flag but it is fine once I click on another part of the form. I would like the form to refresh with the floor plan on bottom and flag on top.
I am sure I am missing a small item to either set the orders of the image or perhaps I can get the VBA code to "click" somewhere else on the form and make the flag appear on top?
Any help is greatly appreciated.
I have a datagridview whose styling is kept as follows
dgvCreatinine.AutoGenerateColumns = False
dgvCreatinine.AutoSize = False
dgvCreatinine.AutoResizeRows()
dgvCreatinine.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill
dgvCreatinine.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders)
dgvCreatinine.DataSource = dtTemp
It is appearing as shown in the attached image
As you can see the gridview is not resizing to the content along row wise. Could someone tell me how this can be accomplished? Thanks in advance.
You can just set the height of the entire control using this formula
DataGridViewHeight = HeaderHeight + NumberOfRows * RowHeight
Code:
dgvCreatinine.Height =
dgvCreatinine.ColumnHeadersHeight _
+ dgvCreatinine.Rows.OfType(Of DataGridViewRow).First().Height * dgvCreatinine.Rows.Count()
This works, but could use some tweaking. For example, there may be a better way to get the row height. And you may also need to add a pixel or two depending on borders. Also, if you have an underlying DataSource, you could get the rows count from it.
That code should be called whenever a row is added or removed, or DataBindings are updated etc.
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
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.