vb.net 将文本框的背景颜色设置为透明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22535852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:10:16  来源:igfitidea点击:

Set BackGround Color of Textbox to Transparent

vb.net

提问by

I am trying to set the background color of textbox to transparent, to blend with my backcolor of my form.

我正在尝试将文本框的背景颜色设置为透明,以与表单的背景颜色混合。

I have tried the following below.

我在下面尝试了以下方法。

 TextBox1.BackColor = Color.Transparent 'This doesn't work it stays white'

Is there something I am missing?

有什么我想念的吗?

采纳答案by Arthur Rey

When I set TextBox.BackColorto Color.Transparent, it throws System.ArgumentException. I got this message :

当我设置TextBox.BackColor为 时Color.Transparent,它会抛出System.ArgumentException。我收到这条消息:

Unvalid property value, The control does not support transparent colors background.

Unvalid property value, The control does not support transparent colors background.

回答by Dennys Henry

Hope am not late to the party, but this actually works for me. First create a class for the panel as below

希望聚会没有迟到,但这实际上对我有用。首先为面板创建一个类,如下所示

Partial Public Class iPanel
    Inherits Panel
    Public Sub New()
        SetStyle(ControlStyles.SupportsTransparentBackColor Or ControlStyles.OptimizedDoubleBuffer Or ControlStyles.AllPaintingInWmPaint Or ControlStyles.ResizeRedraw Or ControlStyles.UserPaint, True)
        BackColor = Color.Transparent
    End Sub
End Class

Then create a RichTextBox(Instead of a Textbox) as below

然后创建一个RichTextBox(而不是 a Textbox) 如下

Protected Overrides ReadOnly Property CreateParams() As CreateParams
    Get
         Dim CP As CreateParams = MyBase.CreateParams
         CP.ExStyle = CP.ExStyle Or &H20
         Return CP
    End Get
End Property

Now compile the code and add the iRichTextBox inside the panel. Works for me

现在编译代码并在面板内添加 iRichTextBox。对我有用

回答by user11117489

Private Sub TextBox1_Paint(sender As Object, e As PaintEventArgs) Handles TextBox1.Paint
    TextBox1.ForeColor = Color.White
    TextBox1.BackColor = Color.Transparent
End Sub

Instead of doing the first one, You can try this. Just put your code in Form Load

你可以试试这个,而不是做第一个。只需将您的代码放入 Form Load

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

    TextBox1.BackColor = color.(color of your Choice, same color of your background)
    TextBox1.ForeColor = color.White

End Sub

As simple as that, it works for me

就这么简单,它对我有用

回答by Shreekant

As far as I know textbox does not supporttransparent color property. Butif you set the back colorof the textboxto the samecolor as of its background component, still it can be considered as transparent.

据我所知文本框does not support透明颜色属性。但是,如果你设置back colortextboxsame颜色作为其的background component,它仍然可以被看作transparent

How to do that- You can get the color name of the background component(in your case it is the form) and pass that name to the component which you want to be transparent.

如何做到这一点- 您可以获得背景组件的颜色名称(在您的情况下它是表单)并将该名称传递给您想要透明的组件。

Dim lname As String = Me.BackColor.ToString
Dim name As String = lname.Substring(7, lname.Length - 8)
txtbox1.BackColor = System.Drawing.Color.FromName(name)

Explanation-

说明——

  1. The first line in the code gets you the name of the color, but there's a rub, it gets the name something like this - Color [Dark Orange]and we need only the name of the color i.e Dark Orange.
  2. Thus the second line is to get the exact color name by removing this - Color []part
  3. And the last line to set that color same as of the background component color. Hope it works, still have problem let me know...
  1. 代码中的第一行为您提供颜色的名称,但有一个问题,它的名称类似于这样 -Color [Dark Orange]我们只需要颜色的名称,即Dark Orange.
  2. 因此,第二行是通过删除这个 -Color []部分来获得确切的颜色名称
  3. 最后一行将该颜色设置为与背景组件颜色相同。希望它有效,仍然有问题让我知道......