afterlabeledit treeview handler c# - c#

i need to based in what the user wrote in the edition of a node label, rewrite that label with other text. Example if the user wrote "NewNodeName" I want that the node text after finish the edition be "S :NewNodeName".
I try this two codes and i don't know why neither work
private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
{
e.Node.Text = "S :"+ e.Label;
}
and also:
private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
{
treeView1.SelectedNode.Text = "S :"+ e.Label;
}

Yes, doesn't work, the Text property gets the label value after this event runs. Which is why e.Cancel works. So the Text value you assigned will be overwritten again by code that runs after raising this event. Code inside of the native Windows control.
There is no AfterAfterLabelEdit event and you cannot alter e.Label in the event handler, you need a trick. Change the Text property after the event stopped running. Elegantly done by using Control.BeginInvoke(). Like this:
private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e) {
this.BeginInvoke((MethodInvoker)delegate { e.Node.Text = "S: " + e.Node.Text; });
}

It's quite late to answer this question, but here's another solution:
1) Remove the part that you want user not to edit of the node label right before you call BeginEdit()
2) In AfterLabelEdit(), set the node text as you want and set NodeLabelEditEventArgs.CancelEdit = true so that the text user input won't replace the text you set
private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (e.Node == null) return;
e.Node.Text = e.Node.Text.Substring(3, e.Node.Text.Length - 3);
e.Node.BeginEdit();
}
private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
{
e.Node.Text = "S :" + e.Label;
e.CancelEdit = true;
}

Related

How to make two textboxes working from both sides? [duplicate]

This question already has answers here:
C# update two text boxes at the same time?
(4 answers)
Closed 3 years ago.
I am making a simple unit converter in winforms, which means I'd be needing two Textboxes, in textbox1 'from' value is entered and in textbox2 the converted value is printed out. I want to make it work the other way around too i.e. to take input from textbox2 and to print the converted value out in textbox1. How am I supposed to do that?
You have to implement TextChanged event handlers for both TextBoxes; the only difficulty is to understand which control (textBox1 or textBox2)
has been changed by user (in order to prevent infinite loop when textBox1s value converts to textBox2s which in turn converts to textBox1s etc.). You can either check Focused:
private void textBox1_TextChanged(object sender, EventArgs e) {
// Do nothing, if user is changing some other control (e.g. textBox2)
if (!(sender as Control).Focused)
return;
// Having textBox1.Text value convert it forward to textBox2.Text
textBox2.Text = Convert(textBox1.Text);
}
private void textBox2_TextChanged(object sender, EventArgs e) {
// Do nothing, if user is changing some other control (e.g. textBox1)
if (!(sender as Control).Focused)
return;
// Having textBox2.Text value convert it backward to textBox1.Text
textBox1.Text = ReverseConvert(textBox2.Text);
}
Or play with event handlers (which is more flexible esp. if you can start conversion, by, say, button click, so neither textBox1 nor textBox2 have focus):
private void textBox1_TextChanged(object sender, EventArgs e) {
textBox2.TextChange -= textBox2_TextChanged;
try {
// Since textBox2.TextChanged switched off,
// changing textBox2.Text will not cause textBox1.Text change
textBox2.Text = Convert(textBox1.Text);
}
finally {
textBox2.TextChange += textBox2_TextChanged;
}
}
private void textBox2_TextChanged(object sender, EventArgs e) {
textBox1.TextChange -= textBox1_TextChanged;
try {
// Since textBox1.TextChanged switched off,
// changing textBox1.Text will not cause textBox2.Text change
textBox1.Text = ReverseConvert(textBox2.Text);
}
finally {
textBox1.TextChange += textBox1_TextChanged;
}
}
You can handle both textbox's Text change event in one Event handler (it will trigger event two times)
private void textBox_TextChanged(object sender, EventArgs e)
{
if (((TextBox)sender).Equals(textBox1))
textBox2.Text = Convert((TextBox)sender).Text);
else
textBox1.Text = ReverseConvert((TextBox)sender).Text);
}

How to catch if cell Value got changed in DataGridView as sender

I haven't found something that matches my problem so I ask it here. I have some code which belongs to a Textbox:
if ((sender as TextBox).Text == form1.filterType())
{
//Do something
}
This comes from the TextBox TextChanged event. So when the TextChanged Event gets fired it calls a method which has the if-construct above and recognizes that the textchanged event came from the textbox.
Now I want exactly the same just when someone writes into a cell in a DataGridView (not when it's just clicked - when the content changes).
How to do this correctly and which event fires when the content changes in a cell without leaving the cell?
I have found a solution for this:
private void Form1_Load(object sender, EventArgs e)
{
this.dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
}
void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
if (dataGridView1.CurrentCell.ColumnIndex == 0)
{
TextBox tb = (TextBox)e.Control;
//"unwire" the event before hooking it up ensures the event handler gets called only once
tb.TextChanged -= new EventHandler(tb_TextChanged);
tb.TextChanged += new EventHandler(tb_TextChanged);
}
}
void tb_TextChanged(object sender, EventArgs e)
{
MessageBox.Show("changed");
}
Now it fires everytime the value in the cell gets changed - it behaves like a textbox.
Now - I still need a solution for the "if-construct" above. How to do this exactly? I've casted the cell into a textbox but now? Does this still comes from the dataGridview?
Once I had a similar problem an solved it this way (think there are better ones but it worked)
private void DataGridView1_onFocus ( Object sender, EventArgs e)
{
DataGridView1.onKeyPress+=DataGridView1_onKeyStroke;
}
private void DataGridView1_onFocusLost( Object sender, EventArgs e)
{
DataGridView1.onKeyPress-=DataGridView1_onKeyStroke;
}
private void DataGridView1_onKeyStroke( Object sender , EventArgs e)
{
//Do your thing
}

Preserve control's visibility on condition

This is probably an easy one for some of you.
I have a TextBox and a ListBox. ListBox provides options for the TextBox and copies selected item's text to TextBox on DoubleClick event. ListBox becomes visible only when TextBox fires Enter event. I do not want to discuss my reasons for selecting this control combination.
I want ListBox to disappear when any other control within the Form gets focus. So I capture Leave event of TextBox and call ListBox.Visible = fale The problem is that TextBox will also loose focus when I click on ListBox to select provided option thus preventing me from selecting that option.
What event combination should I use to preserve ListBox to select option but hide it whenever other controls get focus?
In the Leave method, you can check to see if the ListBox is the focused control or not before changing its Visibility:
private void myTextBox_Leave(object sender, EventArgs e)
{
if (!myListBox.Focused)
{
myListBox.Visible = false;
}
}
This example will provide you with the desired outcome:
public Form1()
{
InitializeComponent();
textBox1.LostFocus += new EventHandler(textBox1_LostFocus);
textBox1.GotFocus += new EventHandler(textBox1_GotFocus);
}
void textBox1_GotFocus(object sender, EventArgs e)
{
listBox1.Visible = true;
}
void textBox1_LostFocus(object sender, EventArgs e)
{
if(!listBox1.Focused)
listBox1.Visible = false;
}
private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
textBox1.Text = listBox1.SelectedItem.ToString();
}
private void Form1_Shown(object sender, EventArgs e)
{
//if your textbox as focus when the form shows
//this is the place to switch focus to another control
listBox1.Visible = false;
}

Mask edit text box focus

I am using mask edit TextBox.The textbox always shows 0 (zero). I cannot type any key from the keyboard. I need to delete the zero first then I can type digits. Therefore I am doing extra steps here. Is it possible to type as soon as I type from the keyboard? Any suggestion is welcome.
private void DateDay_GotFocus(object sender, RoutedEventArgs e)
{
if (((TextBox)sender).Text == "Day")
((TextBox)sender).Text = string.Empty;
}
private void DateDay_LostFocus(object sender, RoutedEventArgs e)
{
if (((TextBox)sender).Text == string.Empty)
((TextBox)sender).Text = "Day";
else
CheckForCorrectDateDay((TextBox)sender);
}
I have tried with Focus event but not successful:
You need to select all content in the textbox in GotFocus event. For MaskedTextBox control it handle the selection internally after the focus event fire. So we need to do BeginInvoke to call the SelectAll() afterward.
private void DateDay_GotFocus(object sender, RoutedEventArgs e)
{
this.BeginInvoke((MethodInvoker)delegate() {
((TextBox)sender).SelectAll();
});
}
This way you can start typing directly.
You can't make the text null if null is not allowed.
WPF version:
private void TextBox_GotFocus(object sender, RoutedEventArgs e) {
this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate() {
((TextBox)sender).SelectAll();
});
}
Alternate solution for MaskedTextBox using the Enter Event
private void maskedEdit_Enter(object sender, EventArgs e)
{
MaskedTextBox maskedTextBox = (MaskedTextBox)sender;
maskedTextBox.BeginInvoke
(new Action
(() =>
{
maskedTextBox.SelectAll();
}
)
);
}

C# program question

private void delete_Click(object sender, EventArgs e)
{
convertedText.Text = "";
}
private void copy_Click(object sender, EventArgs e)
{
if (convertedText.Text != "")
Clipboard.SetText(convertedText.Text);
convertedText.Text = Clipboard.GetText();
else... what to put here?
}
The program has two buttons (copy and delete) and one textbox. If I click the 'Copy' button, it copies the text from convertedText.Text without any problem. The 'delete' button also clears the textbox fine.
But if there's nothing in the textbox, the 'copy' button still attempts to copy it which is causing an unexpected behaviour.
So, what code do I add to the 'else' statement...? What I want is that if the textbox has nothing in it, then the clipboard operation won't be used. How to do that?
Thanks in advance!
Don't add an else clause, just have the if by itself, e.g.
private void copy_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(convertedText.Text))
{
Clipboard.SetText(convertedText.Text);
convertedText.Text = Clipboard.GetText();
}
}
Also, is there any reason why you're copying the text box text to the clipboard and then using the clipboard text to update the text box text? Unless I'm missing something, this should have no effect on the text box, so the code can be simpler:
private void copy_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(convertedText.Text))
Clipboard.SetText(convertedText.Text);
}
Your error stems from the fact that you are missing some parentheses there:
if (convertedText.Text != "")
{
Clipboard.SetText(convertedText.Text);
convertedText.Text = Clipboard.GetText();
}
Only the first line after an if statement is considered to be part of what gets executed dependent on the evaluation of the if when you are omitting parentheses.
You could also return if the textbox does not have a value...
private void copy_Click(object sender, EventArgs e)
{
if (convertedText.Text.Equals(""))
return;
Clipboard.SetText(convertedText.Text);
convertedText.Text = Clipboard.GetText();
}
maybe you are missing brackets { and }
if (convertedText.Text != ""){
Clipboard.SetText(convertedText.Text);
convertedText.Text = Clipboard.GetText();
}
else
Try putting
try
{
string foo = "bar" + 42;
}
catch
{
throw;
}

Categories