partially editable string in a textbox control - c#

is there a way to allow editing a string partially in c# and wpf textbox?
somthing , if the contents of the TextBox were for example
"http://xxxx.xxx/xx/path?param1=xxx&param2=xxx"
the x can be replaced with whatever length but any thing else is constant and cannot be edited in the textbox, any way to achive such thing?

There are two relevant events that you can handle on the TextBox; the PreviewKeyDown and the PreviewTextInput events. By handling these two events, you will have complete control over what the user can and can't edit in the TextBox. Of course you will need to work out the logic inside, but the event handlers are the tool to enable you to do what you want:
private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
// Do your text filtering here using e.Key and e.Handled
}
private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
// Do your text filtering here using e.Text and e.Handled
}

Related

DataGridView MaskedTextBoxColumn

I have a DataGridView, and I would like to have something similar to a MaskedTextBox inside my DataGridView. It doesn't have to be an exact MaskedTextBox, but at least somewhat acting like one.
Here is what my DataGridView looks like.
All I want is that the people who modify the DataGridView's right column (Durée - 'Duration'), follow the Mask pattern 00:00:00 for time.
Another solution would be to place a DateTimePicker. But similar to the solution using a MaskedTextBox, it does supposedly not exist as DataGridView columns.
I have tried using the Column's Builder to add a Behavior→Format, but I don't think this is quite the same. I need something to prevent the user from adding random stuff.
You have (at least) two options:
You can use a regular MaskedTextBox overlaid over the TextBox the DGV creates for editing
You can code the regular edit control, i.e. the TextBox the DGV shows when entering edit mode.
Here are examples for both:
First we create class level variables for the controls:
TextBox editBox = new TextBox();
MaskedTextBox editMBox = new MaskedTextBox();
To get a reference to the editing textbox we code the EditingControlShowing event:
private void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
editBox = e.Control as TextBox;
}
To control user input we hook up the KeyPress event:
public Form1()
{
InitializeComponent();
..
editBox.KeyPress += editBox_KeyPress;
..
}
Here we can do all sorts of check and prevent bad characters from entering. All the regular properties are there..:
void editBox_KeyPress(object sender, KeyPressEventArgs e)
{
string sNew = editBox.Text.Substring(0, editBox.SelectionStart)
+ e.KeyChar + editBox.Text.Substring(editBox.SelectionStart);
Console.WriteLine(sNew);
e.Handled = !validateMethod(sNew);
}
This would call a function you can write..
But if you are happy with what a MaskedTextBox you can simply use one:
private void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
DataGridViewCell cell = dataGridView1.CurrentCell;
editMBox.Parent = dataGridView1;
editMBox.Location = dataGridView1.GetCellDisplayRectangle(cell.ColumnIndex,
cell.RowIndex, false).Location;
editMBox.Size = editBox.Size;
editMBox.Show();
editMBox.Mask = yourMask;
editMBox.BringToFront();
}
We need to hook up the KeyPress event for the MaskedTextBox to end to editing. Here is just a simple way to accept the value when Enter is pressed.
You will want to handle Esc here and also at least the CurrentCellChanged event for more complete control..
void editMBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
editBox.Text = editMBox.Text;
editMBox.Hide();
}
}
I found this neat little program MaskedTextBoxColumn in DataGridViews. I had found that a little earlier, but was reluctant to download it since we had to register and stuff. Actually worked out pretty well. The mask properties is a little too simple, as it is lacking some nice features, but the general MaskedTextBox idea is there.

How to call event key_down only in specific situations in c#?

I have 2 combo boxes and one text box(combo1, combo2, textBox). Here is the code for event key_down:
private void MyForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
buttonSearch_Click(sender, e);
}
When I click button ENTER on keyboard I want that program call search button on form. The problem is when I select some item from combo box and click on ENTER to give me that item, he also call searh button, ofcource, but I dont want to call search until I fill both combo boxes and text box. So, I want to call search button ONLY when my focus is on text box. Any idea how to do that?
as said, you could put the event on the textbox. Also, going with your original problem, you could check if the textbox has focus:
private void MyForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (textBox1.Focused) // or whatever your textbox is called
{
buttonSearch_Click(sender, e);
}
}
}
You have specific events for each control. You are using the Form events but if you only want to have the keydown when the Textbox is focussed I suggest the following:
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter)
buttonSearch_Click(sender,e);
}

Setting Textbox values which has focus in C# Winforms

I have numeric buttons which when pressed display the number in different text boxes. Now my problem is that i want check which textbox has focus so that the number pressed will be entered in that textbox.
My Code:
private void btn_one_Click(object sender, EventArgs e)
{
if (txt_one.Focused==true)
{
txt_one.Text += btn_one.Text;
}
else if (txt_two.Focused==true)
{
txt_two.Text += btn_one.Text;
}
}
Now my problem is that the above code is not working what is wrong and what will be the solution? I even used something like this
private void btn_one_Click(object sender, EventArgs e)
{
if (txt_one.Focus()==true)
{
txt_one.Text += btn_one.Text;
}
else if (txt_two.Focus()=true)
{
txt_two.Text += btn_one.Text;
}
}
In both the above cases the text is entered in both the text boxes. Any solutions.
This problem is a little tricky (with my experience dealing with Enter, Focus, LostFocus, Leave events, all these things sometimes make your head ache a lot and you should avoid dealing with them if possible), at the time you click your Button, the current Focused control you can know is exactly the Button (ActiveControl is one short way to access it). So the solution is we have to record the track of focused TextBox, hold it in a reference and use it when needed. In fact if the control other than one of your TextBoxes is focused, we have to reset the variable lastFocused to null:
TextBox lastFocused;
//Enter event handler for all your TextBoxes
private void TextBoxes_Enter(object sender, EventArgs e){
lastFocused = sender as TextBox;
}
//Click event handler for your button
private void btn_one_Click(object sender, EventArgs e){
if(lastFocused != null) lastFocused.Text += btn_one.Text;
}

Problem controlling input on treeViewNode LabelEdit

I am trying to control user input when he/she wants to edit a treeNode. I don't want him to be able to write numbers (or even better not write a number at given index of the text but thats a bonus)
What I did was make a boolean on the mainWindow that determines if the user is editing the treeNode or not like this:
void Tree_AfterLabelEdit(object sender, System.Windows.Forms.NodeLabelEditEventArgs e) {
isEditing = false;
}
void Tree_BeforeLabelEdit(object sender, System.Windows.Forms.NodeLabelEditEventArgs e) {
isEditing = true;
}
private void Tree_KeyDown(object sender, KeyEventArgs e) {
control input. (e.Handled = true when keyDown is a number.)
}
This is what I tried but the event keydown is only called when I am NOT editing a treenode. So it falls out of purpose. (I use keydown already to handle arrow key events but I want to do both.)
Tried with keypress aswell to no avail.
The TreeView does not have any mechanism to control the LabelEdit part. Even if you can handle the KeyDown part of the Label to prevent numbers, you would still have to inspect the contents on the AfterLabelEdit because the user can "paste" numbers into the field as well. It's best to just control it during the AfterLabelEdit event:
private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
{
foreach (char c in e.Label)
{
if (char.IsNumber(c))
e.CancelEdit = true;
}
}

c# How to enforce uppercase in a specified colum of a DataGridView?

I would like to be able to set the CharacterCasing of a specified column to uppercase.
I can't find a solution anywhere that will convert characters to uppercase as they are typed.
Many thanks for any help
You need to use EditingControlShowing event of the Datagridview to edit the contents of any cell in a column. Using this event you can fire the keypress event in a particular cell. In the keypress event you can enforce a rule which will automatically convert lowercase letters to uppercase.
Here are the steps to achieve this:
In the EditingControlShowing event see whether user is in the column in which you want to enforce this rule. Say your column is 2nd column in the grid
private void TestDataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if(TestDataGridView.CurrentCell.ColumnIndex.Equals(1))
{
e.Control.KeyPress += Control_KeyPress; // Depending on your requirement you can register any key event for this.
}
}
private static void Control_KeyPress(object sender, KeyPressEventArgs e)
{
// Write your logic to convert the letter to uppercase
}
If you want to set the CharacterCasing property of the textbox control in the column, then you can do it where KeyPress event registering is done in the above code, which is in the 'if' block of checking column index. In this case you can avoid KeyPress event.
That can be done in the following way:
if(e.Control is TextBox)
{
((TextBox) (e.Control)).CharacterCasing = CharacterCasing.Upper;
}
Currently i really know, but if you could get access to the editing control of the column (which is a TextBox) you could probably set the CharacterCasing property.
Use EditingControlShowing event of the Datagridview to Edit the contents
After that Apply the Condition For Specific Column
private void dgvGrid_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (dgvGrid.CurrentCell.ColumnIndex == 0 || dgvGrid.CurrentCell.ColumnIndex == 2)
{
if (e.Control is TextBox)
{
((TextBox)(e.Control)).CharacterCasing = CharacterCasing.Upper;
}
}
}
Happy Coding

Categories