Click here to Skip to main content
15,563,054 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hey all, I'm new to C# - so please forgive my ignorance.

Basically what I'm trying to do is quite simple - I want a window to appear with a free text box in it, but I want the window to have no borders or title bar and want the text to sit on top of a semi-transparent dark-colored panel.

I set the form to have a transparency key of LimeGreen, then set the RichTextBox to have a Background of LimeGreen. [Oddly this stuff doesn't show up in the code - unless Im missing something]:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ControlBox = false;
            Text = "";
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            panel1.BackColor = Color.FromArgb(200,Color.Black);
        
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}


Anyway, so I have a Panel UNDERNEATH the text box, which I was hoping would mean the text box, being transparent should reveal underneath.

In fact, what happens is the text box is transparent COMPLETELY and shows no panel underneath but rather whatever window or desktop is behind it.

Is there a way to make it work the way I was hoping... or (as I suspect) am I barking up the wrong tree with the WinForms approach and should I be looking into something else?

What I have tried:

What I've tried is the above approach, which in itself was mostly the result of trawling through tutorials and other forum posts online (sadly I can't find anything for my exact use-case). Having about 4 hours experience of C# and Visual Studio, I'm fresh out of ideas... Hence why I'm asking here.
Posted
Updated 18-Dec-21 3:17am
Comments
Audio Babble 18-Dec-21 7:53am    
Thank you! I'm so new to all this - just needed pointing in the right direction.

Worked a treat with WPF.

In Windows Forms, Transparent is not transparent. All that does is make the background of the control you set the background to take on the background properties of the control containing your control.

In your example, your TextBox background takes on the background properties of the Form you dropped it on.

You would have to create your own version of a TextBox control to make it transparent. There's plenty of discussion and a few examples[^] on the web.

But, in WPF, this would be easy as setting a background color on a TextBlock.
 
Share this answer
 
<Window x:Class="WpfApp3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp3"
mc:Ignorable="d"
WindowStartupLocation="CenterScreen"
Title="WpfApp3" Height="429" Width="800" AllowsTransparency="True" WindowStyle="None">
<Window.Background>
<SolidColorBrush Opacity="0" Color="White"/>
</Window.Background>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="0*"/>
<RowDefinition/>
</Grid.RowDefinitions>
<!--TextBox HorizontalAlignment="Center" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Center" Width="800" Height="430" Background="Black" Opacity="0.25" FontSize="24" FontFamily="Arial" Foreground="#FF030303" Grid.Row="1"/-->
<Rectangle HorizontalAlignment="Center" Height="463" Stroke="Black" Fill="Black" Opacity="0.35" VerticalAlignment="Center" Width="834" Grid.Row="1"/>
<TextBox HorizontalAlignment="Center" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Center" Width="780" Height="408" Background="Transparent" AcceptsReturn="True" FontSize="34" Foreground="#FFF9E97F" FontWeight="Bold" SelectionBrush="#FF030303" Grid.Row="1" RenderTransformOrigin="0.625,0.689" TextChanged="TextBox_TextChanged" BorderBrush="Transparent" BorderThickness="0"/>


</Grid>
</Window>
 
Share this answer
 
Comments
Audio Babble 18-Dec-21 10:50am    
The correct approach was through XAML, as posted in "Solution 2". Quite simple really.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900