So, I'm adding a label programatically and I'm in need of altering the top margin a little bit to the value 8. I can't do that the obvious way, so what's wrong with my thinking?
Dim LabelAdapter As New Label
LabelAdapter.text = "Adapter"
LabelAdapter.Margin.Top = 8
This gives me the error "Expression is a value and therefore cannot be the target of an assignment".
Label.Margin returns a Padding object.
Since Padding is a structure, it will actually return a copy. You are changing the Top value of that copy, not of the actual control’s margin. Since that would have no noticeable effect, VB correctly prevents it.
You need to assign a whole new margin. In fact, the Margin property (or rather, the Padding class) is arguably broken since it doesn’t allow an easy way to change the individual values.
Unfortunately, we just have to live with it. So to change just the Top value, we need to write:
Dim old As Padding = LabelAdapter.Margin
LabelAdapter.Margin = New Padding(old.Left, 8, old.Right, old.Bottom)
Weird, huh?
Related
Im a little confused on the difference between a property and a method. When I think of this, I use the cells default values for my logic and how they change
Default value of a cell is:
Color = blank
Width = 8.43
--If we Set(change) the color to red, then this is referencing a property
--If we auto fit the cells width to say 10(Change the cells width), then this is a method
Either way, the values are being "Changed" from the default settings to a new one. How is it then, that a property and method are not the same??
I understand this logic is wrong. I guess im just trying to think of a better to way to think about it..
Thanks,
I'm creating a label dynamically, and then adding text to it dynamically (and the amount of text added to it will be different each time). So, the label needs to always be the same width as the text inside it.
This happens by default when creating a label in the Windows Designer. But when creating a label dynamically, it appears to be "set" at a specific width, regardless of how much text is in it (which means that it often "cuts off" some of the text).
So... any idea how I can get the dynamically created label to always stay the same width as the text inside it?
If you want to do it manually, you can do something like this, every time you change the text:
Dim g As Graphics = Label1.CreateGraphics()
Label1.Width = CInt(g.MeasureString(Label1.Text, Label1.Font).Width)
However, it's much easier to simply set the label's AutoSize property to True and let the label do the work for you.
Label1.AutoSize = True
Getting a strange error with this code. This excerpt is the beginning of the main code. First it opens a form that takes 3 inputs. The inputs aren't being used right now so it's not the issue.
Public triangle As Range
Public Height, Width, i As Integer
Sub asdf()
Set triangle = Selection
WeightsUserForm.Show
Height = triangle.Rows.Count
Width = triangle.Columns.Count
Debug.Print Height
Debug.Print Width
This gives me 12 as Height and 12 as Width, which is correct based on what I've selected.
Now here is the sub that runs when you press OK on the user form:
Private Sub OKButton_Click()
Set triangle = Selection
Height = triangle.Rows.Count
Width = triangle.Columns.Count
Debug.Print Height
Debug.Print Width
This gives me Height as 28.5 and Width as 99. I have no clue where this comes from. I even checked all the objects on my userform as if maybe it was interpreting that as my selection (which is clearly wrong since those objects wouldn't have a rows property).
Ideally I would like to save my initial selection as a "public variable" if that is a real thing.
Any help is appreciated.
EDIT: Further update. I have set the variable triangle manually now ie
triangle = Range("B3","M14")
This STILL gives me the same strange dimensions. Now I'm really stumped.
2xEDIT: When I don't use the variables Height and Width and just refer directly to triangle.Rows.Count and triangle.Columns.Count it gives me correct answers. So I can run my program properly now. I would still love to know why using a variable was wrong though.
So you're saying that the line
Public Height,Width,i As Integer
is only declaring i as an integer and not specifying a type for height and width? If so that would be my problem.
I redid it declaring both Height and Width properly as integers but it still bugged out the same way. However using private variables W and H defined the same way it works. shrug
Reading between the lines, I think your issue is that Debug.Print Height and Debug.Print Width is printing the size of your form.
This will happen if your declarations are in a Module rather than the Form class, because Height and Width are keywords applicable to the form, so the local versions are used in preference.
To solve, you should do all of these things (not all of this is absolutely necassary, but is all good practice):
Use Option Explicit in all your modules
Don't use Keywords for variable names
Scope your variables, in order of preference, to Procedure, then local Module, then public Module, and only when there is good reason
Prefix your remote module calls with the module name, eg Module1.Height
BTW, using Dim Height, Width, i as integer declares declares Height and Width as the default type which is Variant.
I am working with vb.net and excel 2007 to create some graphs for myself. I wanted to set the datalabel positions to a custom value since the default above position (xlLabelPositionAbove) causes the labels to clash with error bars and the default option for a side (such as xlLabelPositionRight) may leave the label over another point or other errorbar. Due to this, I wanted to set the label to a custom position where it is off to about a 45 degree angle to the top right (like right in the middle of where the default above and right positions would place it).
I tried doing this by adjusting xlMySeries.Points(index).DataLabel.Top and xlMySeries.Points(index).DataLabel.Left at first, however I ran into an undescriptive error leading me to believe I was not doing things correctly. I then thought to try setting xlMySeries.DataLabels.Position = xlLabelPositionCustom and then adjusting top and left. However, to my surprise, I could not even change xlMySeries.DataLabels.Position to xlLabelPositionCustom!
Whenever I try to adjust top, left, or the position to certain datalabel positions, I get HRESULT: 0x80004005 (E_FAIL), which I have generally found to mean "You are doing it wrong" in my experience so far with excel. I cannot set the position member to custom, or anything other than just above, left, right, center (so not bestfit, custom, or any inside___ one)
Any idea why I cannot set the position property to what I need it or otherwise change the position of my datalabels? I just need SOME way to adjust the positioning of my datalabels to a custom psoition (or position other than above, left, right, center, bottum). Thanks in advance!
You can set positions only to whose which you can see on the Data Label properties window.
I have a strange problem and it's probably a simple fix, but after much research, I cannot seem to find a solution.
I have a DataGridView on which I'm trying to center the column headings, but the result is a left bias in the centering—almost like an indenting problem. I've seen a few posts on this issue on a site or two, but never a solution. Any thoughts?
Here's the statement I'm currently trying to use:
DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
The code you've posted is on the right track: you need to set the ColumnHeadersDefaultCellStyle property of your DataGridView control.
However, you need to create a new DataGridViewCellStyle class and assign that to the ColumnHeadersDefaultCellStyle property. You can't modify the Alignment property as your code sample shows unless you have assigned a DataGridViewCellStyle class to this property.
So, for example, the following code achieves perfectly centered column headings in a blank project:
Dim dgvColumnHeaderStyle As New DataGridViewCellStyle()
dgvColumnHeaderStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
myDataGridView.ColumnHeadersDefaultCellStyle = dgvColumnHeaderStyle
In the future, you may find it easier to do these types of things from the Designer. If you still need to do it yourself through code, you can check the *.Designer.vb file that is created to see how it was done.
EDIT: I just now noticed the slight offset you're referring to in the columns—it does indeed create a little extra padding to the right of each header. It's not a bug, though. There's a much simpler explanation.
Like a ListView, the DataGridView supports sorting by columns. Therefore, each column header reserves enough space to display the sort glyph (usually an arrow) when calculating center justification.
If you want the column headers to be perfectly centered, you'll need to disable sorting. Set the SortMode property for the column to "NonSortable". This should prevent space from being reserved for the sort glyph whenever the column text is center or right justified.
If you want to center or use any other alignment style of the Column Header text you can use this
dgvResults.Columns("ColumnName").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter