Henry Instead - Stating from the title of DataGrid
Han Rui (02/20/2003)
What is the fun of programming? I believe that all people have their own opinions. Henry's biggest happiness is the joy of creating, and this joy is built in practice that can be seen. Today, this Xiaowen source is very simple. I saw a post on a netizen in the 9CBS forum at noon. At that time, I went back to post, but the words were unknown. More importantly, I tried it in the afternoon, I got two implementation methods. , It is awkward, although the business is entangled, I have a record.
Proposition: If you have a DataGrid's title text in .NET.
Analysis one:
This proposition doesn't seem to, mainly the DataGrid author does not provide attributes such as CAPTIONALIGN to everyone, so ...
So how do you solve it? I think the most direct way is to add the appropriate space number to the title text, so that the title text is pushed to the middle. Ok, let me do it.
(1) To know how much the DataGrid title bar accounts for an English character in the current environment;
(2) DataGrid's width / character width = number of characters that can be accommodated in the title bar
(3) Add n spacer, n = to the text of the title, n = (the number of characters that can be accommodated in the title bar - the number of characters of the title) / 2
The code is as follows:
Private Sub Form1_Load (Byval E AS ISTEM.EventArgs) Handles MyBase.LOAD
Dim Perwidth As INTEGER 'The width of an English character occupied
Dim Count As integer 'Number of characters that can be accommodated in the title bar
Dim Len As Integer 'The number of characters of the title
DIM I as integer
With graphics.fromhwnd (DataGrid1.handle) .measureString (text: = "AbcdefghijklmnopQrstuvwxyz", Font: = DataGrid1.font)
Perwidth = cint (.width / 26.0!)
End with
Count = DataGrid1.width / Perwidth
Len = system.text.Encoding.default.getbytecount (DataGrid1.captiontext)
For i = 1 to (count - len) / 2
DataGrid1.captiontext = "" DataGrid1.captiontext
NEXT
End Sub
Point 1 How to calculate the width of an English character, see the code
Total 2 How to get the number of characters occupied by the title text, you have to know one byte in English, including two bytes. The solution is to do with System.Text.Encoding.default.getbytecount. These tricks are still very useful, looking for it!
In addition, you can use the string.padleft property to try the empty string
The result is as shown in the figure:
Figure 1 First Program: Addition
Analysis 2:
But think about it, this method is still unhappy, intelligent is not enough, so there is this method. That is, write the title text into a label, then embed the Label to the DataGrid, dynamically adjust the location of the label, not joined! Private Sub Form1_Load (Byval E AS ISTEM.EventArgs) Handles MyBase.LOAD
Labcaption.backcolor = color.transparent 'is set to transparent color
Labcaption.autosize = true 'is set to automatically adjust the text box according to the text content
Labcaption.Borderstyle = BorderStyle.none Set to Borderless
Labcaption.ForeColor = color.White 'The foreground color is set to white, that is, the text is white word.
DataGrid1.controls.add (labcaption) 'embeds the text box into the DataGrid
Labcaption.top = 5 'Setting the height, 0 refers to the head position of DataGrid
Labcaption.Left = (DataGrid1.Width - Labcaption.width) / 2 'Settings
End Sub
Figure 2 Second Solution: Embedding Text Box
From Figure 2 we can see the entire implementation process, of course, there are also tips worth mentioning:
Point 1: Setting the text box in the transparent. Only written with Labcaption.backcolor = color.Transparent is not, in this way, the system will set the background color to the background color, because transparency is relatively, I think about the principle of changing dragons. We have to set a step: Labcaption.Parent = DataGrid1 is only available, or the use written in the above code: DataGrid1.controls.add (LabCAption). Here the Parent attribute is not in the pop-up menu, don't worry it, write it.
Key 2: Unfortunately, this simple method needs to set the DataGrid's BorderStyle to None or Single, and Label will have a small border when set to 3D.
A simple proposition, bring us the joy of thinking, I wish you all the best in work and study!
----
Disclaimer: The right to copyright and interpretation of this article belongs to Han Rui, if you need to reprint, please keep your full content and this statement.
QQ: 18349592
E-mail: Henry7685@hotmail.com
Please visit my column: http://www.9cbs.net/develop/author/netauthor/latitude/