C# Basics - TextBox event handler - c#

I have a simple TextBox called MsgBox1 and I have changed the trigger from LostFocus to ProperyChanged.
When I modify the text (i.e. MsgBox1.Text = "Some Text") execution branches to the event handler.
So far, so good.
Now, what do I put in the empty event handler to tell it to actually update MsgBox1.Text?
Hours of searching yields less than helpful results like:
{
// Do Something
}
Edit: Thanks, It was stupidity on my part. The methods don't run in parallel while tracing. If I run rather than trace everything works as it should. Thanks again.

As stated in the comments there is no need to update anything. The event fires when the textChange event occurs.
you can test this by using the following code :
private void textBox1_TextChanged(object sender, EventArgs e)
{
var currentTextValue = this.textBox1.Text;
var currentTextValueFromObject = (sender as TextBox).Text;
}
both vars yield the same result. One grabs the object from the event handler while the other grabs it directly from the form.

Related

WinForms field not updating [duplicate]

Is it possible to know if any of the textbox values have changed in the application.
I have around 30 textboxes and I want to run a part of code only if, any of the textboxes value has changed out of the 30. Is there a way I can know that.
Each text box will raise an event TextChanged when it's contents have changed. However, that requires you to subscribe to each and every event.
The good news is that you can subscribe to the event with the same method multiple times. The handler has a parameter sender which you can use to determine which of your 30 text boxes has actually raised the event.
You can also use the GotFocus and LostFocus events to keep track of actual changes. You would need to store the original value on GotFocus and then compare to the current value on LostFocus. This gets round the problem of two TextChanged events cancelling each other out.
You can assign an event handler to each of the TextBox's TextChanged events. All of them can be assigned to the same event handler in code. Then you'll know when the text changes. You can set a boolean flag field in your class to record that a change occurred.
This is perhaps on the rough and ready side, but I did it this way.
In the constructor, I created
bool bChanged = false;
In the TextChanged event handler of each control (actually same for each), I put
bChanged = true;
When appropriate, I could do some processing, and set bChanged back to false.
You can also just do this:
In your Constructor:
MyTextBox.TextChanged += new TextChangedEventHandler( TextChanged );
And Then this Method:
private void TextChanged(object Sender, TextChangedEventArgs e){
//Do something
}
try this. Add this code to the load/constructor. no need to specify the event in the XAML explicitly
this.AddHandler(TextBox.TextChangedEvent, new TextChangedEventHandler(TextChanged));
private void TextChanged(object Sender, TextChangedEventArgs e)
{
//ToDO (use sender to identify the actuale text from where it fired }
}

Repeat same steps on every textChanged event

I have in my project 5 text boxes.
Every TextBox should accept only digits.
For that I created a function which takes not prepared text and returns the proper one.
Now I'm wondering if there is any simpler way to perform this action on every TextBox, on every TextChanged event without repeating almost same code?
private void TextGoldPack_TextChanged(object sender, EventArgs e)
{
(sender as TextBox).Text = Only_digits((sender as TextBox).Text);
}
private void TextGoldTake_TextChanged(object sender, EventArgs e)
{
//repeat here and on every _TextChanged event
}
If I'm understanding you correctly, just because it's named TextGoldTake_TextChanged, doesn't mean that's the only textbox that can use that code. On the events tab, you can set the TextChanged function for all you textboxes to lead to that function. If it helps, rename it something that doesn't sound textbox-specific such as TextChanged.
Change all the TextBoxes to refer this method upon TextChanged.
Use the sender property to get the actual caller TextBox.

When combobox changes do something ( C# )

I've been trying to make a program with 3 comboboxes where depending on what you pick different things happen.
Here is a screenshot of what I'm stuck with.
The only thing missing in the screenshot is the following which is in the private void Form1_Load event
cBxColor1.Items.Add("Black");
cBxColor2.Items.Add("Black");
cBxTest.Items.Add("Something");
In the screenshot above I try two methods to write something in the textbox. One whenever the text changes and then checks for the choosen item. In this case Something, Black and Black. I'm planning on adding more later but so far I'm trying to get this to work with one.
The original plan was to have while(the selected texts in the comboboxes are Something, Black and Black) then add some text to the textbox if that is true.
Screenshot of the error I get when trying the other method, I'm not sure what this means.
I've googled and searched around for a solution but I truly couldn't find anything that would help to solve my problem. I would appreciate if the 1337 hax0rz on here would help me out.
TextChanged is a Event. Use it in a methode like this:
private void ComboBox_TextUpdate(Object sender, EventArgs e)
{
//Your code here
MessageBox.Show("You are in the ComboBox.TextUpdate event.");
}
Add the event with += to your combobox in your initialisation:
ComboBox.TextUpdate += ComboBox_TextUpdate;
So at every TextUpdate your Methode ComboBox_TextUpdate gets called and you can code there.
Instead of using the if condition to see if the text has changed, you should use the ComboBox event SelectedValueChanged.
To create that event right-click on your ComboBox and select properties. Select "Events" and double-click the textbox next to the SelectedValueChanged event.
Then you want to check the values of each ComboBox like you did.
private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
{
if (cBxColor1.SelectedText.Equals("Black") || cBxColor2.SelectedText.Equals("Black") || cBxTest.SelectedText.Equals("Something"))
{
tbxTest.Text = "TEST";
}
}
Also, that while statement is almost a death threat because once it enters that condition, it will not leave.
You won't be able to change the ComboBox value due to the while being executed.

Generate postback to specific method/event handler

I have a custom gridview, and i'm generating a postback event for onclick manually on RowDataBound. However, this postback ALWAYS goes to/expects a specific method on the row click.
Here's my current code to make that postback happen:
if (!String.IsNullOrEmpty(this.OnRowClick))
e.Row.Attributes.Add("onclick", this.Page.ClientScript.GetPostBackEventReference(this.grid1, "Select$" + e.Row.RowIndex.ToString()));
As you can see here, i've setup a property to my grid to ask which method to call when executing that postback... however, i cannot seem to find anywhere on the web how to specify in the _dopostback call WHICH method to call... it always, without exception, expects this method:
grid1_SelectedIndexChanged(object sender, eventargs e)
But I don't want that. I want it to go to whatever method name is contained in the property OnRowClick.
Here's how things are currently setup for the visual people:
that's just it... i'm not sure. I am a visual person so maybe this will help a bit for everyone as well:
Custom Control Setup (minified version):
[ Some other controls
-= A gridview =-
Some other controls]
[(Control Event Property)
OnRowClick Event (GridViewRow eventarg)]
[(Control Event Wireup)
if(OnRowClick!=null)
this.GridView1.SelectedIndexChanged += InternalMethod(...)
[(InternalMethod(...))
GridViewRow row = this.GridView1.SelectedRow;
if(OnRowClick!=null)
OnRowClick(row);]]
So far so good... no problems whatsoever... BUT... when i set it up like this:
HtmlPage
-> Custom Control(mygrid)
---> mygrid.OnRowClick += SomePageMethod(GridViewRow row)
------> SomePageMethod never, ever, gets called... like... never...
Since i have built my control as a composite control, i can't step through its internal code to debug, so i can't tell why it's not happening... :S please please help me bubble up this event properly!
Custom Control Code:
this.grid1.SelectedIndexChanged += new EventHandler(grid1_SelectedIndexChanged);
void grid1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = this.grid1.SelectedRow;
if(this.OnRowClick!=null)
OnRowClick(this.grid1, row);
}
#region Events
public delegate void RowClickEventHandler(object sender, GridViewRow SelectedRow);
[Category("Events")]
[Description("This event fires when a row is clicked, if it is defined")]
public event RowClickEventHandler OnRowClick;
#endregion
Web page code:
this.pgrSummary.OnRowClick += new SimplePager.SimplePager.RowClickEventHandler(pgrSummary_OnRowClick);
And that methode never gets called...

C# TreeView SetFocus firing leave event twice

I have this simple code, where when the user leaves the TextBox control, TreeView gets focused:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.treeView1.Nodes.Add("A");
this.treeView1.Nodes[0].Nodes.Add("A.A");
this.treeView1.Nodes.Add("B");
this.treeView1.Nodes[0].Nodes.Add("B.A");
}
private void textBox1_Leave(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("Leave..");
this.treeView1.Focus();
}
}
If we execute this code the Leave event is fired twice:
Leave..
Leave..
But if we set focus to other control, only one Leave event is fired.
Is that a problem of the TreeView? Do you know any workaround? Should we report this to Microsoft?
Thanks,
RG
this.treeView1.Focus();
Do not use the Focus() method in an event handler that's called because of a focusing event, like Leave. If you need to prevent a focus change then use the Validating event instead. Setting e.Cancel = true stops it.
But do note that this isn't very logical to do so for a TreeView, there isn't anything the user can do to alter the state of the control. You'll trap the user. Maybe that was the intention, do make sure the user can still close the window. If not then you might need the FormClosing event to force e.Cancel back to false.
Given that there is no code there to wire up the event I'm guessing you did it from the designer which means a line of code such as
textBox1.Leave += new EventHandler(textBox1_Leave);
will have been added to the Form1.designer.cs, check this file to ensure the line doesn't exist more than once as for each time this line is run you will get an event trigger, so if you run the line 3 times the Leave event will fire 3 times when you leave the textbox!
HTH
OneShot

Categories