I've recently started to learn c#, and I started off by making a simple tic-tac-toe game, using labels and forms.
When I click on a label I want it to change background color and Foreground color.
Here is my code;
public void LabelClick(Label lbl, int i)
{
if (strCurrPlayer == strPlayer1)
{
liP1Squares.Add(i);
lbl.BackColor = System.Drawing.Color.Black;
lbl.ForeColor = System.Drawing.Color.White;
lbl.Text = "X";
}
else
{
//Player2
liP2Squares.Add(i);
lbl.BackColor = System.Drawing.Color.White;
lbl.ForeColor = System.Drawing.Color.Black;
lbl.Text = "O";
}
lbl.Enabled = false;
SwapPlayer();
}
However, when it's called, it sets the background color correctly, but the foregorund, i.e. text, changes from the red, (default) to black for player 1 instead of White, and Light Grey for player 2, instead of Black.
I was wondering if there are any fields within Label or Forms that change text color by default when the background color is changed. If not, what else could be making this change?
Any help would be appreciated.
After you set your items on the label, you are disabling it by
lbl.Enabled = false;
This then causes the label to use the disabled-theme from Windows.
In order to change this behavior, you should turn to using events instead of direct methods.
Every label offers a Click-event that you can use to call a method once the label was clicked.
You can then remove the event-handler from Label.Click and you don't need to disable it.
See MSDN to learn about Control.Click-EventHandler
remove lbl.Enabled = false; to see the changes
after you dissable control it will set the label background color to SystemColors.Control and foreground color to SystemColors.GrayText
Related
I have simple Win-form GUI in C# which display the text in red or Green depended upon the value received. The RichText display the text correctly as long as the i do not minimize the GUI. When the GUI is minimized, the text shown in Text window is in black color (only data that was processed when GUI was minimized). when the GUI is maximized the text color for the data shown correctly again.
Please let me know what is wrong here.
Here is my code:
LogMessageWindow.Find(message);
LogMessageWindow.SelectionColor = Color.Red; /// if message&2==0 set color to Red otherwise set color to green
LogMessageWindow.SuspendLayout();
LogMessageWindow.Focus();
LogMessageWindow.AppendText(message + ".\n");
LogMessageWindow.ScrollToCaret();*
In your code you have:
LogMessageWindow.Find(message);
This line is useless: you are Appending a new chunk of Text. Searching for it before appending it won't do much (maybe locate an identical string. Then what?).
LogMessageWindow.SuspendLayout();
SuspendLayout() can be useful if you're adding/appending a large selection of lines of text in batch. When you're finished, you should ResumeLayout(). Doesn't seem to be needed here.
LogMessageWindow.Focus();
Moving the Focus on the RTB control doesn't accomplish anything special. And if the container Form is minimized... Since you're adding text in a procedure, the focus is not needed.
A couple of things you can do.
Using a method, pass a reference to the RichTextBox that is used for this task, the color to use and the text to be appended. Here the new text color is defined as Color? color, so if you pass null, the control ForeColor is used.
RTBAppendWithColor(LogMessageWindow,
((message & 2) == 0) ? Color.Red : Color.Green,
message.ToString() + "\n");
private void RTBAppendWithColor(RichTextBox rtb, Color? color, string AppendedText)
{
int sLenght = AppendedText.Length;
rtb.AppendText(AppendedText);
rtb.Select(rtb.Text.Length - sLenght, sLenght);
if (color != null)
rtb.SelectionColor = (Color)color;
rtb.ScrollToCaret();
}
Using an Extension.
Create a static Class with a static Method that references a RichTextBox object. This method will be a new method of any RichTextBox you create.
LogMessageWindow.AppendWithColor(((message & 2) == 0) ? Color.Red : Color.Green,
message.ToString() + "\n");
public static class RTBExtensions
{
public static void AppendWithColor(this RichTextBox rtb, Color? color, string AppendedText)
{
int sLenght = AppendedText.Length;
rtb.AppendText(AppendedText);
rtb.Select(rtb.Text.Length - sLenght, sLenght);
if (color != null)
rtb.SelectionColor = (Color)color;
rtb.ScrollToCaret();
}
}
If you using FrameWork 3.5, the selection text will probably remain selected even after ScrollToCaret() is called. If it looks ugly, add:
rtb.SelectionStart = rtb.Text.Length;
before rtb.ScrollToCaret().
Thanks al for yur valuable feedback. i was able to get this done by suing this code.
LogMessageWindow.SelectionStart = LogMessageWindow.TextLength; LogMessageWindow.SelectionLength = 0; LogMessageWindow.SelectionColor = Color.Red; LogMessageWindow.SuspendLayout();LogMessageWindow.AppendText(message + ".\n"); LogMessageWindow.ScrollToCaret(); ` LogMessageWindow.ResumeLayout()
Whenever I set a TextBox's border style to "None" the bottom of text in the box is invisible. That means I can't see underscores and descenders of letters like y and j.
Here is the textbox with border
Here is the textbox without border
Any suggestions as to how this is solved would be appreciated.
This seemed to work for me:
public Form1()
{
InitializeComponent();
textBox1.Multiline = true;
textBox1.MinimumSize = new Size(0, 30);
textBox1.Size = new Size(textBox1.Size.Width, 30);
textBox1.Multiline = false;
}
Setting box to multi line to adjust size of the box then when its changed back keeps the size.
I am creating a custom control, a custom looking TextBox designed to only allow numeric input. It consists of a plain control, with a TextBox as one of it's properties:
class NumericControl : Control
{
private TextBox text;
//rest of the code
}
This TextBox is drawn inside of the control. It all works very nicely, except that when you press enter it makes this horrible DING noise. To fix this, I thought I'd make the TextBox multiline. However, when I do this, because the font size of the TextBox is changed to resize it, the blinking cursor inside the TextBox appears to disappear, which is a problem because then you can't tell by looking at it whether or not it has focus. If I don't change the font size of the TextBox, the cursor appears and acts normally, however I need the font size to change based on the height of the control, otherwise it doesn't look any good.
The code setting the TextBox properties is as follows, and resides within the constructor of my control:
text = new TextBox();
text.AutoSize = false;
text.Left = 10;
text.Top = 2;
text.Text = "0";
text.Multiline = true;
text.BorderStyle = BorderStyle.None;
text.TextChanged += text_TextChanged;
text.LostFocus += text_LostFocus;
this.Controls.Add(text);
The font size is changed inside the OnPaint event, and looks like this:
text.BackColor = Enabled ? Background : SystemColors.Control; //Fixes an issue I had with disabled TextBox BackColor being able to be changed
text.Font = new Font(TextBox.DefaultFont.FontFamily, (float)(rc.Height * 0.7 - 2), FontStyle.Regular);
text.Width = this.Width - 21;
text.Height = this.Height - 4;
How do I both resize the textbox (and font size), and make it multiline while retaining the blinking cursor when it has focus?
I am using the windows form in which I have used DataGridView and now I want to change the color of these scrollbars. I got one solution also on CodeProject but its not working . The Code which I have Used is as below
foreach (Control child in DataGridview1.Controls)
{
child.Enabled = true;
child.IsAccessible = true;
child.ForeColor = Color.Red;
child.BackColor = Color.Green;
child.Width = 5;
}
but it doesn't changes the color of the scrollbars.
I am not getting what else needs to be done.
Also is there any other way to do this .
I have a simple Windows Forms application with a tabControl. I have 3 panels on the tabControl, each having 5 buttons. The text on first set of buttons is hard-coded, but the next set populates when you click one from the first group, and then the same thing happens again for the last group when you click one of the buttons from the second group. In the [Design] view I manually set the TextAlign property of each button to MiddleCenter. However, when I run the application the text on the middle set of buttons is never centered. It is always TopLeft aligned. I've tried changing the font size and even explicitly setting the TextAlign property every time I set button text programmatically, as follows:
private void setButtons(List<string> labels, Button[] buttons)
{
for (int i = 0; i < buttons.Count(); i++)
{
if (i < labels.Count)
{
buttons[i].Text = labels.ElementAt(i);
buttons[i].TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
buttons[i].Enabled = true;
}
else
{
buttons[i].Text = "";
buttons[i].Enabled = false;
}
}
}
This image shows the result:
Does anyone have any ideas for what I'm missing?
Trim text which you are assign to button. Also you can refer label by index, without calling ElementAt
private void setButtons(List<string> labels, Button[] buttons)
{
for (int i = 0; i < buttons.Count(); i++)
{
Button button = buttons[i];
if (i < labels.Count)
{
button.Text = labels[i].Trim(); // trim text here
// button.TextAlign = ContentAlignment.MiddleCenter;
button.Enabled = true;
}
else
{
button.Text = "";
button.Enabled = false;
}
}
}
You can set the UseCompatibleTextRendering property to true, then use the TextAlign property.
The strings in the SQL table that were assigned to the middle column were actually nchar(50), not nvarchar(50), which explains the problem. I added .Trim() to the Text assignment and it looks great now.
You can use the TextAlign from the Properties Menu and set it to MiddleCenter ...
If this does not work then the text you have for your button is larger than the actual button itself... to which you should either rescale your Font Size to a lower base size or a percent size of the actual button by using
btnFunction.Font = new Font(btnFunction.Font.Name, Convert.ToInt32(btnFunction.Height * 0.3333333333333333));
This would cause the button's font to be one third of the height of the button....