Henry note-starting from the center of the Datagrid title

Source: Internet
Author: User

From: http://www.microsoft.com/china/community/Column/18.mspx

Henry note-starting from the center of the Datagrid title
Han Rui

(02/20/2003) What is the fun of programming? I believe that everyone has their own opinions. Henry's greatest joy is to enjoy the joy of creation, and this joy is based on visible practices. Today, the source of this article is very simple. I saw a post posted by a web friend on the CSDN forum at noon. At that time, I also replied to the post, but the words are unknown. More importantly, I tried it a little in the afternoon, there are two ways to achieve it. Despite the troubles of the transaction, please note it down. Proposition: if the title text of the Datagrid is centered in. NET. Analysis 1: This proposition does not seem to be difficult. It is mainly because the Datagrid author does not provide CaptionAlign attributes for everyone, so ...... So how can we solve it? The most direct method I think of is to add a proper number of spaces to the front of the title text to push the title text to the middle. Okay, just do it. (1) You must know the width of an English character in the Datagrid title bar in the current environment; (2) the Datagrid width/character width = the number of characters that can be accommodated in the title bar (3) add n spaces before the text of an existing title, n = (number of characters that can be accommodated in the title bar-number of characters of an existing title)/2 code is as follows:
Private Sub Form1_Load (ByVal sender As Object, ByVal e As System. EventArgs) Handles MyBase. Load

 

Dim perWidth As Integer: The width occupied by an English character

Dim count As Integer number of characters that can be accommodated in the title bar

Dim len As Integer 'number of characters in an existing title

Dim I As Integer

With Graphics. FromHwnd (DataGrid1.Handle). MeasureString
(Text: = "ABCDEFGHIJKLMNOPQRSTUVWXYZ", Font: = DataGrid1.Font)

PerWidth = CInt (. Width/26.0 !)

End

Count = maid/perWidth

Len = System. Text. Encoding. Default. GetByteCount (DataGrid1.CaptionText)

For I = 1 To (count-len)/2

DataGrid1.CaptionText = "" + DataGrid1.CaptionText

Next

End Sub

Key 1: how to calculate the width of an English character. For details, refer to code key 2: How to obtain the number of characters occupied by the title text. You must know that English occupies one byte, and Chinese occupies two bytes. The solution is to use System. Text. Encoding. Default. GetByteCount. These skills are useful! In addition, you can use the String. PadLeft attribute to try filling in an empty String, as shown in result 1:Figure 1 solution 1: add space

Analysis 2: however, if you think about this method seriously, it is still uncomfortable and intelligent enough, so you can use this method. That is, the title text is written into a Label, and then the Label is embedded into the datagrid, dynamically adjust the Label location, it will not end!

Private Sub Form1_Load (ByVal sender As Object, ByVal e As System. EventArgs) Handles MyBase. Load

 

LabCaption. BackColor = Color. Transparent set to Transparent

LabCaption. AutoSize = true' is set to automatically adjust the text box size based on the text content

LabCaption. BorderStyle = BorderStyle. none' is set to borderless

LabCaption. ForeColor = Color. White' the foreground Color is White, that is, the text is White.

DataGrid1.Controls. Add (LabCaption) 'embed the text box into the Datagrid

LabCaption. Top = 5' sets the height. 0 indicates the position of the datagrid header.

LabCaption. Left = (maid-LabCaption. Width)/2' set Center

End Sub

Figure 2 solution 2: embed a text box

From figure 2, we can see the entire implementation process. Of course, there are also tips worth mentioning: Key Point 1: Set the background color of the text box to transparent. Write only one LabCaption. backColor = Color. transparent does not work. In this case, the system will set the background color to Form, because transparency is relative. Think about the principle of chameleon and you will understand it. We need to set one step: LabCaption. Parent = DataGrid1, or use: DataGrid1.Controls. Add (LabCaption) as written in the above Code. Here, the Parent attribute is not in the pop-up menu. Leave it alone and just write it. Key 2: Unfortunately, this simple method needs to set the borderstyle of the Datagrid to None or Single. When it is set to 3D, the Label will also have a small border. A simple proposition brings us the joy of thinking. I wish you more happiness in your work and study!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.