Make TextBox Editable by ContextMenuStrip - c# winform - c#

I have a textbox in my C# Winform. The program assigns a value to the textbox by default.
I want the user to have a right click function to edit this text at runtime. So when the user right click to edit, the backgroud should become white and user should be able to edit the text. And after editing, the background should return to default and non editable
I have created a ContextMenuStrip with right click event to edit text as follows and assigned readonly property to false when user right clicks and press edit menu item:
private void editTextToolStripMenuItem_Click(object sender, EventArgs e)
{
itxt_CommonTitle.ReadOnly = false;
}
I am not sure how to proceed further. Is this possible using textbox?

If you have not changed the BackColor of the TextBox in the designer then the background color should automatically change from white to gray when you set ReadOnly = true and change from gray back to white when you set ReadOnly = false. However, if you have changed it to something else in the designer, then the easiest way is just to set a private variable to remember the original BackColor before you enable the control for editing. Then you can restore the color after you set it back to read-only.
private void editToolStripMenuItem_Click(object sender, EventArgs e)
{
MakeTextBoxEditable(itxt_CommonTitle);
}
private void itxt_CommonTitle_Leave(object sender, EventArgs e)
{
MakeTextBoxReadOnly(itxt_CommonTitle);
}
private void Form1_Click(object sender, EventArgs e)
{
MakeTextBoxReadOnly(itxt_CommonTitle);
}
private Color origTextBoxBackColor = SystemColors.Control;
private void MakeTextBoxEditable(TextBox textBox)
{
origTextBoxBackColor = textBox.BackColor;
textBox.ReadOnly = false;
textBox.BackColor = Color.White;
textBox.Focus();
}
private void MakeTextBoxReadOnly(TextBox textBox)
{
textBox.ReadOnly = true;
textBox.BackColor = origTextBoxBackColor;
}

I think you are missing a process. After edit, there should be an update or save method.
textbox readonly = true;
edit textbox: textbox readonly = false;
button save: textbox readonyl = true;
Edit:
Something like this:
private void buttonSave_Click(object sender, EventArgs e)
{
textBox1.ReadOnly = true;
}
private void editToolStripMenuItem_Click(object sender, EventArgs e)
{
textBox1.ReadOnly = false;
}
You dont need to change backColor, just readonly prop is fine.

Related

How to disable and enable a button dynamically?

In my UWP, I have a TextBox and a Button. I want to disable the Button when the TextBox is empty.
I tried this on startup-
if (string.IsNullOrEmpty(hmmtxtBox.Text))
{
hmmbtn.IsEnabled = false;
}
private void HmmtxtBox_TextChanged(object sender, TextChangedEventArgs e)
{
hmmbtn.IsEnabled = true;
}
It works kind of okay, but when I clear the TextBox, the Button remains enabled. I want to disable if the TextBox is empty and vice versa.
Try this:
private void HmmtxtBox_TextChanged(object sender, TextChangedEventArgs e)
{
hmmbtn.IsEnabled = HmmtxtBox.Text.Length > 0;
}

Set the Caret/Cursor position in C# WPF when textbox clicked on

I am using a textbox for a login window. I want the textbox to display "Username" in light grey so the user knows to use that box to type in the username. Whenever the user clicks on the textbox even if it's in the middle of the word username I want the cursor to go to the first position and username will disappear when they start typing. I tried using the PreviewMouseDown event but it only works inside breakpoints but doesn't trigger at all outside it. Using the PreviewMouseUp event it works, but other caret positions can be selected before the cursor jumps to the beginning. I want it to appear like the user is unable to select any cursor position besides the first. This is the code I've tried.
private bool textboxuserfirstchange = true;
private void eventTextChanged(object sender, TextChangedEventArgs e)
{
if (textBoxUser.Text != "Username")
{
if (textboxuserfirstchange)
{
textBoxUser.Text = textBoxUser.Text[0].ToString();
textBoxUser.SelectionStart = 1;
textBoxUser.Opacity = 100;
}
textboxuserfirstchange = false;
}
}
private void eventPreviewMouseDown(object sender, MouseButtonEventArgs e)
{
if (textboxuserfirstchange)
{
textBoxUser.Focus();
textBoxUser.Select(0, 0); //None of these working
textBoxUser.SelectionStart = 0;
textBoxUser.CaretIndex = 0;
}
}
You could for example handle the GotKeyboardFocus and PreviewTextInput event. Something like this:
private const string Watermark = "Username";
private void TextBox_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
if (textBoxUser.Text == Watermark)
textBoxUser.Dispatcher.BeginInvoke(new Action(() => textBoxUser.CaretIndex = 0), DispatcherPriority.Background);
}
private void textBoxUser_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
if (textBoxUser.Text == Watermark)
textBoxUser.Text = string.Empty;
}

Removing the default hover effect of a button

I am trying to apply a color to a hovered button like this:
private void button_MouseEnter(object sender, EventArgs e)
{
button.UseVisualStyleBackColor = false;
button.BackColor = System.Drawing.SystemColors.GradientInactiveCaption;
}
private void button_MouseLeave(object sender, EventArgs e)
{
button.UseVisualStyleBackColor = true;
button.BackColor = System.Drawing.SystemColors.ControlLight;
}
My Problem is there seems to be some kind of a default hover effect. The color is much darker than it should be. At the moment I do not know where to start to get rid of it.

TextBox for Live Search in Windows Forms

How to create a Textbox which displays "search" in grey color when it is empty and standard behavior when user starts typing text into it?
Do it via TextBox Events Enter and Leave and Attributes:
private void textBox1_Leave(object sender, EventArgs e)
{
if(textBox1.Text.Trim().Length == 0)
{
textBox1.Text = "Search";
textBox1.ForeColor = Color.LightGray;
}
}
private void textBox1_Enter(object sender, EventArgs e)
{
textBox1.Text = string.Empty;
}
See this thread at MSDN for a possible solution: http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/93a67793-6426-4d4f-be9d-a9b79725efc8

CheckBox button and ContextMenuStrip for dropdown menu

I'm tring to implement a button which have a dropdown menu when checked and this menu is gone when unchecked. My problem is I cannot uncheck the checkbox when it or its menu lost focus.
The checkbox's appearance mode is button.
My code:
private void cbSettings_CheckedChanged(object sender, EventArgs e)
{
if (cbSettings.Checked) {cmsSettings.Show(cbSettings, 0, cbSettings.Height);}
else {cmsSettings.Hide();}
}
I've tried to uncheck the checkBox on contextMenuStrip's VisibleChanged / Closed event but this caused menu not to hide (or hide and show immediately).
The example below does not, of course, include the code you would need for swapping BackGroundImage of the CheckBox to indicate CheckState. The events to "wire-up" should be obvious. Hope this is helpful.
// tested in VS 2010 Pro, .NET 4.0 FrameWork Client Profile
// uses:
// CheckBox named 'checkBox1
// ContextMenuStrip named 'contextMenuStrip1
// TextBox named 'cMenuSelectionInfo for run-time checking of results
// used to position the ContextMenuStrip
private Point cPoint;
// context click ? dubious assumption that 'right' = context click
private bool cmOpenedRight;
// the clicked ToolStripMenuItem
private ToolStripMenuItem tsMIClicked;
private void checkBox1_MouseDown(object sender, MouseEventArgs e)
{
cmOpenedRight = e.Button == MouseButtons.Right;
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
// positioning the CheckBox like this
// is something in a 'real-world' example
// you'd want to do in the Form.Load event !
// unless, of course, you'd made the CheckBox movable
if(checkBox1.Checked)
{
contextMenuStrip1.Show();
cPoint = PointToScreen(new Point(checkBox1.Left, checkBox1.Top + checkBox1.Height));
contextMenuStrip1.Location = cPoint;
}
else
{
contextMenuStrip1.Hide();
}
}
private void contextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
// assume you do not have to check for null here ?
tsMIClicked = e.ClickedItem as ToolStripMenuItem;
tbCbMenuSelectionInfo.Text = tsMIClicked + " : " + ! (tsMIClicked.Checked);
}
private void contextMenuStrip1_Closing(object sender, ToolStripDropDownClosingEventArgs e)
{
e.Cancel = checkBox1.Checked;
}
private void contextMenuStrip1_Closed(object sender, ToolStripDropDownClosedEventArgs e)
{
if (cmOpenedRight)
{
tbCbMenuSelectionInfo.Text += " : closed because : " + e.CloseReason.ToString();
}
}
I think your approach of unchecking the check box on the context menu's closed event is a good one, what you need is a bit of "event cancelling logic"(c), like this:
private void OnContextClosing(object sender, EventArgs e)
{
_cancel = true;
cbSettings.Checked = false;
_cancel = false;
}
private void cbSettings_CheckedChanged(object sender, EventArgs e)
{
if(_cancel)
return;
if (cbSettings.Checked) {cmsSettings.Show(cbSettings, 0, cbSettings.Height);}
else {cmsSettings.Hide();}
}
This will keep your CheckChanged event from re-checking your checkbox.

Categories