I have a winform application that has a set of RichTextBoxes.
I want to change the text color into Red when the text box contents change
and I have a button, when this button is clicked, the text color is reset to its default color.
The problem is, when I use the event handler (TextChanged) to detect if change in Contents has occured, it is also triggered when the color is reset.
to be more clear I will give an example:
1- the contents of the text box change
2- the event handler is triggered and text color is changed to Red.
3- the button is clicked, then the text is black again
4- when the text color changes, the event handler is triggered again and color is changed to Red.
so, The color seems to be always Red even if the button is clicked.
how Can I handle this problem? I need to detect only the change in contents, not in color
here is a piece from the code:
private void AHReg_TextChanged(object sender, EventArgs e)
{
AHReg.ForeColor = Color.Red;
}
private void RunButton_Click(object sender, EventArgs e)
{
resetControlColor(); //this function sets the text color to Black
}
There's a few ways to skin this cat. You can track the actual text and look for mismatches, or handle the ForeColorChanged event, but the simplest way I think in your case is to just "turn off" the event subscription when you do your reset.
For example, in your RunButton_Click method:
private void RunButton_Click(object sender, EventArgs e)
{
AHReg.TextChanged -= AHReg_TextChanged;
resetControlColor(); //this function sets the text color to Black
AHReg.TextChanged += AHReg_TextChanged;
}
If you need that event to be active in your resetControlColor() function then you'll need to come at this at a different angle, but that's the simplest away to approach it.
you can add a boolean variable called NeedToBeChangedin your class.
private bool NeedToBeChanged = true;
private void RunButton_Click(object sender, EventArgs e)
{
NeedToBeChanged =false;
resetControlColor(); //this function sets the text color to Black
NeedToBeChanged =true;
}
private void AHReg_TextChanged(object sender, EventArgs e)
{
if(NeedToBeChanged)
AHReg.ForeColor = Color.Red;
}
Related
I need to change the button background color by itself, that means when I will click on a button then it should change the background color, but when I will click again on the same button then I need to back default button color
I have tried on this event method
private void slot_1(object sender, RoutedEventArgs e)
{
//SlotLogic();
slot1.Background = Brushes.Green;
}
private void slot_2(object sender, RoutedEventArgs e)
{
//SlotLogic();
slot2.Background = Brushes.Green;
}
1 - You specified Windows Application. That's kind of broad. I believe you mean Windows Forms?
2 - You're also not checking whatsoever the current state of the button, so I'm not sure how do you think that clicking on the same button twice would have a different result, since you are executing the exact same come twice slot1.Background = Brushes.Green;.
Windows Forms
private bool isSlot_1Clicked = false;
private void slot_Click(object sender, EventArgs e)
{
if (isSlot_1Clicked)
slot.BackColor = Color.Green;
else
slot.BackColor = Color.Red;
isSlot_1Clicked = !isSlot_1Clicked;
}
If you want to have a toggle logic, you need to check the current state to inverse it. There is a lot of ways to do it, I choose to store it on a separate variable isSlot_1Clicked for sake of simplicity.
You could also check the slot's button background color, or check a complex object's property, or an array, and so on.
The first lines of code just checks the current state of the variable, and changes the button's background accordingly. The last line just set the variable isSlot_1Clicked to the inverse of it, executing the essence of the toggle logic.
WPF Version
private Brush slot_1DefaultBackground = null;
private void Slot1_Click(object sender, RoutedEventArgs e)
{
// store the default background value
if (slot_1DefaultBackground == null)
slot_1DefaultBackground = slot1.Background;
// check the current background, and toggle accordingly
if (slot1.Background != slot_1DefaultBackground)
slot1.Background = slot_1DefaultBackground;
else
slot1.Background = Brushes.Green;
}
This version stores the default button's background, and checks the current background against it to execute the toggle logic.
To show the relevant information(in monopoly game, the property belongs which player, current market price etc.), I put a Label on the top of a panel, and used a ToolTip object to display the information. This is the image of my current setup.
Here are the steps I have done:
1.Added MouseHover event handler (The Label name is MEDITERANEAN)
this.MEDITERANEAN.MouseHover += new System.EventHandler(this.MEDITERANEAN_MouseHover);
2.Initialized Tooltip
private void InitializeToolTip()
{
toolTipLabel.ToolTipIcon = ToolTipIcon.Info;
toolTipLabel.IsBalloon = true;
toolTipLabel.ShowAlways = true;
}
3.Call setToolTip() in MouseHover call back function
private void MEDITERANEAN_MouseHover(object sender, EventArgs e)
{
toolTipLabel.SetToolTip(MEDITERANEAN, "You put mouse over me");
rolledDice.AppendText("Mouse Over");
}
But when I start application and move my cursor over the label, there is no text from toolTipLabel. What part did I make mistakes?
Interestingly, i made other function and it works.
private void panelBoard_MouseOver(object sender, EventArgs e)
{
toolTipLabel.SetToolTip(panelBoard, "You put mouse over me");
rolledDice.AppendText("Mouse Over");
}
I think you just need to bring your lable control in front of image. Try something like this .
MEDITERANEAN.BringToFront();
I found the solution, first I should set Panel's property "Enable" to true, then set label's property "visible" to true as well.
my Question is how to "draw" on multiple labes. I have a form containing a matrix of labels. Now I want to click on one Label drag over some others and all these Labels should change the background color. I have a method which changes the color with the Click-Event, but I can't find an Event for this Problem. I also tried the Mous_Enter Event and checked if the left button was down, but it looks like, that the Event Trigger was stuck in the first label.
So at first I have this, where each number is in a different label:
And then I want to "draw" on the labels, so that the Background Color changes and so I have something like the following:
Connect the MouseClick and MouseMove event of all your labels to the following event handler:
private void MouseClickedOrMoved(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
ChangeLabelBackColor(this.PointToClient(MousePosition));
}
}
and add this function to your code:
private void ChangeLabelBackColor(Point Location)
{
foreach (Label l in this.Controls.OfType<Label>()) {
if (l.Bounds.Contains(Location))
{
l.BackColor = Color.Black;
}
}
}
If a textbox has focus and I want to be able to select it again is there a way to do this.
So first click the background turns blue and while it is still selected I press again the background turns green. How do I catch the second press even though its already selected?
You can subscribe to the PointerEntered and the SelectionChanged events. The first one is always fired when the pointer hits the TextBox. However if it contains text and you tap on it you will eventually select the text. The SelectionChanged handler will take care for that.
Your XAML markup looks as follows:
<TextBox x:Name="tb"
Text="Test"
PointerEntered="TextBox_PointerEntered"
SelectionChanged="TextBox_SelectionChanged"
GotFocus="TextBox_GotFocus"/>
The code behind file contains the following code:
private void TextBox_PointerEntered(object sender, PointerRoutedEventArgs e)
{
tb.Background = new SolidColorBrush(Colors.Green);
}
private void TextBox_SelectionChanged(object sender, RoutedEventArgs e)
{
tb.Background = new SolidColorBrush(Colors.Green);
}
private void TextBox_GotFocus(object sender, RoutedEventArgs e)
{
tb.Background = new SolidColorBrush(Colors.Blue);
}
You will have to adjust the code to your needs and take care of special cases where both SelectionChanged AND PointerEntered are fired (at this point both handlers do the same, so there's no problem).
I have an application that allows you to draw an activity diagram and I need to generate an algorithm from this diagram.
My question is how to get the value of a Textbox after drag and drop?
Here is the code that I wrote in my button:
private void generat_algo(object sender, ExecutedRoutedEventArgs e)
{
{
IEnumerable<DesignerItem> designerItems = this.Children.OfType<DesignerItem>();
IEnumerable<Connection> connections = this.Children.OfType<Connection>();
If you only have one textbox then just access the textbox by name:
textbox1.Text
Or, if you need to support multiple textboxes then you can simply get the text when the drag/drop is complete. Just handle the Drop event and check if it is a textbox that was moved:
private void Canvas_Drop(object sender, DragEventArgs e)
{
bool itemIsTextbox = (e.Data.GetDataPresent(typeof(TextBox)) == true);
//get the textbox then get hte text out of it
if (itemIsTextbox)
string text = (TextBox)e.Data.GetData(typeof(TextBox)).Text;
}