I have a feature that selects the line on which user clicks.
private void richTextBox1_MouseClick(object sender, MouseEventArgs e)
{
if(richTextBox1.Lines.Length!=0)
{
int firstcharindex = richTextBox1.GetFirstCharIndexOfCurrentLine();
int currentline = richTextBox1.GetLineFromCharIndex(firstcharindex);
string currentlinetext = richTextBox1.Lines[currentline];
richTextBox1.Select(firstcharindex, currentlinetext.Length);
}
}
I want to achieve that when user clicks elsewhere (not on any line of text but still inside the textbox or anywhere else) the text will become unselected.
I tried using the GetFirstCharIndexOfCurrentLine() method of RichTextBox but if i click outside of any text the previous value is remembered.
Related
Hello im making my first project with about 10 different textboxes where the user puts data in. when he/she clicks the the textbox the textbox text clears and a virtual numpad form pops up and when he leaves the textbox the numpad "hides".
right now (or i would) i have 2 events for every textbox, a click event and a leave event,
private void sheetWidthBox_Enter(object sender, EventArgs e)
{
vnumPadForm.Location = PointToScreen(new Point(sheetWidthBox.Right, sheetWidthBox.Top));
vnumPadForm.Show();
}
Im sure there is a way of dynamically coding that in one event and just grabbing the label name. i have played around with it a bit on my numpad like this and it works good;
private void button_Click(object sender, EventArgs e)
{
Button b = (Button)sender;
string num = b.Text;
SendKeys.SendWait(num);
}
Like that but instead i want to get the label name
right now (or i would) i have 2 events for every textbox, a click event and a leave event,
it works but very inefficient.
Change the name of the handler to something generic like "anyBox_Enter()", and update to the code below:
TextBox curTextBox = null;
private void anyBox_Enter(object sender, EventArgs e)
{
curTextBox = sender as TextBox;
vnumPadForm.Location = PointToScreen(new Point(curTextBox.Right, curTextBox.Top));
vnumPadForm.Show();
}
Note that I added a class level variable called "curTextBox" that gets set from the generic handler! This will track whatever TextBox was entered last.
Now, one by one, select each TextBox on your Form that you want to wire up to this common handler. After selecting each one, in the Properties Pane, click on the "Lightning Bolt" Icon to switch to the events for that control if they are not already showing. Find the "Enter" entry and change the dropdown to the right so that it says "anyBox_Enter".
Then in your button click handlers you can use the "curTextBox" variable to know where to send the key:
private void button_Click(object sender, EventArgs e)
{
Button b = (Button)sender;
string num = b.Text;
if (curTextBox != null) {
curTextBox.Text = num; // or possibly APPEND this to the TB?
}
}
I have a ContextMenuStrip with a ToolStripButtonMenu "Print".
A MDI child form is open containing a DataGridView. I am doing a validation to an editable column "Copies" in that grid. I don't want the user to input letters for example. The validation is working fine when leaving the cell but if I am clicking on a control such as the "Print" button, the validation is not caused.
The following screen shot shows how I can click on the "Print" button while the Copies cell contains letters:
// The code for the cell validation
private void QuantitiesDataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (e.ColumnIndex == QuantitiesDataGridView.Columns[COL_COPIES].Index)
{
QuantitiesDataGridView.Rows[e.RowIndex].ErrorText = "";
int enteredValue;
if (!int.TryParse(e.FormattedValue.ToString(), out enteredValue) || enteredValue < 1)
{
e.Cancel = true;
QuantitiesDataGridView.Rows[e.RowIndex].ErrorText = "Invalid number of copies";
}
}
}
I was looking for a property of the ToolStripButtonMenu such as CauseValidation but there is not such one.
Is there a way to trigger the validation when clicking on one of the ToolStripButtonMenu so the Print button will not be triggered until the Copies value is valid?
In your ToolStripButton's Click method, try calling the active form's ValidateChildren function:
private void toolStripButton1_Click(object sender, EventArgs e) {
if (this.ActiveMdiChild.ValidateChildren()) {
// do your processing ...
}
}
I'm trying to program a matching game. My idea is:
(1). When a certain pictureBox1 is clicked, it becomes invisible
(2). A MessageBox shows up, prompting "Pick another box."
(3). Finally, I need to program an if/else statement where if pictureBox13 is clicked it becomes invisible; else, (if another pictureBox is clicked) a MessageBox prompts "Try again." And both picture boxes become invisible, but I don't know what I am doing wrong:
// Program From Below
private void pictureBox1_Click(object sender, EventArgs e)
{
MessageBox.Show("Now Pick Another Hidden Picture!");
pictureBox1.Visible = false;
if (pictureBox13_Click)
{
MessageBox.Show("Great!");
pictureBox13.Visible = false;
}
else
{
MessageBox.Show("Try Again!");
}
}
There is a red squiggly line under if (pictureBox13_Click)
It would be better if every PictureBox had it's a state, that you would then manipulate using a Click_Event. Microsoft has a comprehensive tutorial for a matching game here: https://msdn.microsoft.com/en-us/library/dd553235.aspx
As other suggested, you can use same event handler for all your pictureBoxes and cast sender to PictureBox to see what PB was clicked :
List<string> selectedPictureBoxes;
public MyForm() // ctor
{
selectedPictureBoxes = new List<string>();
foreach(Control c in this.Controls)
if(c is PictureBox) c.Click += pictureBox_Click;
}
private void pictureBox_Click(object sender, EventArgs e)
{
PictureBox _clicked = sender as PictureBox;
if(!selectedPictureBoxes.Contains(_clicked.Name))
selectedPictureBoxes.Add(_clicked.Name);
else ....
}
You could create an int for the selected boxes (in this example, box1 and box2) which are both set to 0 and then create an on click event which sets the int to the clicked box.
if(box1 != 0)
{
box2 = 'insert selected box number'
}
else
{
box1 = 'insert selected box number'
}
After two boxes have been selected, both integers can be set to false, this allows for you to use a switch instead of if, which could shorten the code substantially if a separate if statement is required for each pair of pictures.
I asked a question similar to this a few days ago here. The answer works fine, with the exception of performing the same operation at the start up of the window. This means that I need to have the text in a textBox highlighted every time the window is opened.
Currently I am setting the focus to the textBox at startup with no problem in the constructor. With that being said, I am guessing that the correct area to perform this operation is in the constructor. This is what I am trying currently with no luck:
public AddDestination()
{
InitializeComponent();
//Give cursor focus to the textbox
destination_textBox.Focus();
//Highlights text **DOES NOT WORK
destination_textBox.SelectionStart = 0;
destination_textBox.SelectionLength = destination_textBox.Text.Length;
}
How can I make it so that the text inside my textBox is highlighted whenever the window opens?
Instead of constructor, move it to AddDestination_Load event.
public AddDestination()
{
InitializeComponent();
}
private void AddDestination_Load(object sender, EventArgs e)
{
//Give cursor focus to the textbox
textBox1.Focus();
//Highlights text **DOES NOT WORK
textBox1.SelectionStart = 0;
textBox1.SelectionLength = textBox1.Text.Length;
}
You can write this segment of code in Load EventHandler
//code
InitializeComponent();
//code
private void Form_Load(object sender, EventArgs e)
{
//Give cursor focus to the textbox
destination_textBox.Focus();
//Highlights text **DOES NOT WORK
destination_textBox.SelectionStart = 0;
destination_textBox.SelectionLength = destination_textBox.Text.Length;
}
To do so go to Designer, click on the window, go to Properties, click Events button, search for Load event then double-click on it.
I need to have the text in a TextBox become selected when a user clicks into the box. If the text is already selected, it needs to be a regular cursor. So on the click event of all the textboxes I have this code:
TextBox t = (TextBox)sender;
bool alreadyselected = t.SelectedText == t.Text;
if (!alreadyselected) t.SelectAll();
the problem is, by the time the click event is reached, t.SelectedText is empty
so the full text always becomes selected even when clicking multiple times
I would appreciate a solution that can be for all the textboxes at once if possible
You're correct, the default Click for the TextBox is changing the position of the caret and thus clearing any selected text. But you can restore it.
First add 2 int vars to store the selection Start and Length and initialize Start as -1 to signal not set:
private int SelectedStart = -1;
private int SelectedLength = 0;
then make a handler for the TextBox's Leave event and save the Start and Length for the currently selected text when we lose focus.
private void textBox1_Leave (object sender, EventArgs e)
{
SelectedStart = textBox1.SelectionStart;
SelectedLength = textBox1.SelectionLength;
}
Finally, make a handler for the TextBox's Click event and, if we previously saved the Start and Length, restore them to the TextBox and then set Start to -1 to signal not set again (this allows for normal click behavior within textbox when it is focused).
private void textBox1_Click (object sender, EventArgs e)
{
if (SelectedStart != -1) {
textBox1.SelectionStart = SelectedStart;
textBox1.SelectionLength = SelectedLength;
SelectedStart = -1;
}
}
Use the Control.Tag property to set a bool flag to select or deselect the TextBox text:
private void TextBox_Click(object sender, EventArgs e)
{
TextBox txtBox = (TextBox)sender;
txtBox.SelectionStart = 0;
// First click will select the text
if (txtBox.Tag == null)
{
txtBox.Tag = true;
txtBox.SelectionLength = txtBox.Text.Length;
}
// Second click will deselect the text
else
{
txtBox.Tag = null;
txtBox.SelectionLength = 0;
}
}