Reading text from label - c#

I have just a little problem with reading text from label, but...
I have two forms. Form_1, and Form_2.
Form_1 is sending (on demand) text from "label_one" directly to "label_two" in Form_2.
But in Form_2 i have another label called "label_reader" that need to show any changes done in "label_two" text.
I must (dynamically?) read any changes from "label_two" and show it in label_reader.
Never had a similar problem, and have no idea how to do that. It can't be done with the use of a button.
Any help will be greatly appreciated!

You can use the TextChanged event of label_two. Subscribe to that event (most likely in the constructor of your Form_2) and set the text of label_reader when the event is raised:
public partial class Form_2 : Form
{
//...
public Form_2()
{
InitializeComponent();
// your other code
label_two.TextChanged += label_two_TextChanged;
}
// the event handler
private void label_two_TextChanged(object sender, EventArgs e)
{
label_reader.Text = label_two.Text; // or what ever you want to do
}
}

Related

Capturing TextChanged on a text field

Not sure what I'm doing wrong since this doesn't work at all. TextChanged, KeyDown or any function that is listening on a text box.
Can you advise how to create a listener to perform functions XYZ when text is entered on txtSearch textbox?
public Form1()
{
InitializeComponent();
}
void txtSearch_TextChanged(object sender, EventArgs e)
{
cmboxCountry.SelectedIndex = 1;
}
Make sure that this method is set to event you are expecting. This line should be added somewhere after InitialiseComponent() to be sure that TextBox has been already initialised.
txtSearch.TextChanged += txtSearch_TextChanged;
You can set it either in a code, or in Designer class. To add this line at designer class, you can use Event tab page at properties window of text box, by double click on Event you like to add.
Have you hooked up the event either in the designer file or somewhere else in code e.g.
public Form1()
{
InitializeComponent();
this.txtSearch.TextChanged += txtSearch_TextChanged;
}

Why my custom event fires twice?

I've searched relevant posts but I got nothing much
I have created a user control. In my user control there is a text box. I want to have an event in my user control that fires whenever text box TextChanged event raises. This is what I have done so far : (This is code of user control)
public event EventHandler txtchnged;
public void ontxtchnged()
{
txtchnged(this, EventArgs.Empty);
}
public MyTextBox()
{
InitializeComponent();
textBox1.TextChanged += textBox1_TextChanged;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
ontxtchnged();
}
Here is where I have used user control
public RegisterMainFrm()
{
InitializeComponent();
myUserControl1.txtchnged += myUserControl1_txtchnged;
}
private void myUserControl1_txtchnged(object sender, EventArgs e)
{
Console.WriteLine("hello");
}
This works and I know that the code might not be clean but that's not the problem. Problem is : "hello" will be printed in console twice and I really don't know why and how to fix it.
From MSDN on TextBox.TextChanged:
Note:This event fires when the TextBox control is created and
initially populated with text.
Could this be your problem that you get the initial event?
UPDATE:
From Adriano Repetti Hint in Comments: Did you get the textBox1_TextChanged event handler by double clicking in the designer?
Then you have added a second hook to the TextChanged Event.
Check the code inside InitializeComponent of your UserControl if it is already hooking the event.

How to force a certain Event sequence

I wrote a small control that creates a popup for my Win8 Phone application which does all the nasty things for me like rotation, proper placement etc.
The popup is opened in a Popup control but not on a new phone page.
To close the popup, my control hooks up to the "backKeyPressed" event of the underlying page.
This works like charm until the underlying page has its own implementation of BackKeyPressed event. In this case, the page event is triggered but not the popup control event.
If I would own the event, I could create my own stack to call the last added event first, but I do not own the event of the pages.
As far as I know, I am unable to unregister any previously attached event handler and reassign it once my control unsubscribes from the event.
I could have only one implementation for the BackKeyPressed event which then informs the popup control to close itself (if open), if nothing was open, do the Page specific implementation. But this would require code changes on all pages where I might want to use the popup. Even worse, if I have 5 possible popups, I would have to check all of them :-(
So I am looking for an option to handle this centrally.
What other options do I have to overcome this situation?
Normally you cannot change the order of fired events - they are executed in registered order, but it's not required by specifications - source.
But as Jon Skeet says here:
Summary: For all sane events, you can rely on the ordering. In theory, events can do what they like, but I've never seen an event which doesn't maintain the appropriate ordering.
it is fired in registered order and should be.
BUT for your purpose (I think) you can set an event to invoke your method where you would control the order. I think simple example can show this behaviour:
public partial class MainPage : PhoneApplicationPage
{
private List<EventHandler<CancelEventArgs>> listOfHandlers = new List<EventHandler<CancelEventArgs>>();
private void InvokingMethod(object sender, CancelEventArgs e)
{
for (int i = 0; i < listOfHandlers.Count; i++)
listOfHandlers[i](sender, e);
}
public event EventHandler<CancelEventArgs> myBackKeyEvent
{
add { listOfHandlers.Add(value); }
remove { listOfHandlers.Remove(value); }
}
public void AddToTop(EventHandler<CancelEventArgs> eventToAdd)
{
listOfHandlers.Insert(0, eventToAdd);
}
public MainPage()
{
InitializeComponent();
this.BackKeyPress += InvokingMethod;
myBackKeyEvent += (s, e) => { MessageBox.Show("Added first"); e.Cancel = true; };
AddToTop((s, e) => { MessageBox.Show("Added later"); });
}
}

Adding events to Form designer file

I created a new Event in my user control (SearchControl) like this:
//Event which is triggered on double click or Enter
public event EditRecordEventHandler EditRecord;
public delegate void EditRecordEventHandler(object sender, EventArgs e);
//Supressing the events
private bool _raiseEvents = true;
private void OnEditRecord(System.EventArgs e)
{
if (_raiseEvents)
{
if (this.SearchResultGridView.FocusedRowHandle > -1)
{
if (EditRecord != null)
{
EditRecord(this, e);
}
}
}
}
Now this Event is called when user double click a row in a grid. So from the properties window I selected the MouseDoubleClick Event of the grid view and called the above created EditRecord event.
private void SearchResultListGridControl_MouseDoubleClick(object sender, MouseEventArgs e)
{
// Check whether the user clicked on a real and not a header row or group row
DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo info = SearchResultGridView.CalcHitInfo(e.Location);
if (info.InRow && !SearchResultGridView.IsGroupRow(info.RowHandle))
{
OnEditRecord(e);
}
}
Now the issue I am facing is every time I double click a row in grid view it calls the SearchResultListGridControl_MouseDoubleClick() which then calls OnEditRecord(), however the value of EditRecord is everytime null.
To solve this I checked the designer file of the Main Control which has SearchControl and could not find the EditRecord Event entry in this. So I manually created it like this:
this.MySearchControl.EditRecord += new performis.BA.Merkmalsleisten.Search.SearchControl.EditRecordEventHandle(this.MySearchControl_EditRecord);
Now the things are working fine, but my question is why it did not create it automatically at the first place? And as far I know it is not recommendable to add anything manually to the designer file..is there any other way I can do it?
Thanks
When you create event it has to be used in the form designer similar to how you are using MouseDoubleClick for the Grid (so you need to find event in the Misc category, because you didn't define CategoryAttribute, double clicked there, etc).
If I understand it right you want to subscribe to event automatically, when form is created. You can do this in the control constructor (find parent form control.Parent or control.FindForm()) or perhaps in the special method, which you have to call from the form constructor, which in turn is basically similar to wiring event manually (which you did in the designer created file, but, instead, you can do in the form file, which is totally ok to edit) Up to you.
Sure.
A better practice would be to add your binding line:
this.MySearchControl.EditRecord += new performis.BA.Merkmalsleisten.Search.SearchControl.EditRecordEventHandle(this.MySearchControl_EditRecord);
To the form's constructor. something like:
public MyForm()
{
this.MySearchControl.EditRecord += new performis.BA.Merkmalsleisten.Search.SearchControl.EditRecordEventHandle(this.MySearchControl_EditRecord);
//The rest of your constructor.
}

Why doesn't my button click work?

I'm trying to write a Fahrenheit to Celsius converter but nothing happens when I click the button
and I have no idea why. Please help me.
It's written in C# and I have a form with a label, a button, a textbox for input, and a multiline textbox for output.
namespace _14._6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click( object sender, EventArgs e)
{
int farenhait;
int celsius;
farenhait = Convert.ToInt32(textbox2.Text);
celsius = Convert.ToInt32(5.0 / 9.0 * (farenhait - 32));
textBox1.Text = farenhait + "grader på farenhait skalan motsvarar \n " + celsius + "grader celsius";
}
}
}
Assuming the statement:
but nothing happends when i klick the button...
is literally true (i.e. when placing a breakpoint on the first line in the handler button1_Click it doesn't hit the breakpoint) then something happened to the handler. So, go to the Form Designer and double-click the button again to hookup the handler.
Is the event wired up?
Click on the button in the designer and look in the properties window (bottom-right underneath your file list), then click the events button (looks like a little lightning bolt) and scroll down until you find Click, then type button1_Click in there if it isn't already in there, that will wire the event up.
Alternatively, you can add it manually in the form's constructor:
public Form1()
{
InitializeComponent();
this.button1.Click += new EventHandler(button1_Click);
}
What exactly does the debugger tell you is wrong?
Not to deviate to far from your original question, I would suggest to use a technique called abstraction. An example of this would be to create a separate class, which will take parameters that will accomplish a task.
public class TemperatureConverter
{
public void Convertion(int fahrenheit, int celsius)
{
// Input your conversion syntax here.
}
}
Now with that particular class can handle the conversion, on your form within your button you can provide the necessary Error Handling then you can actually call it with those values.
private void button1_click(object sender, EventArgs e)
{
// Error Handling
TemperatureConverter tc = new TemperatureConverter();
tc.Conversion(textbox1.Text, textbox2.Text);
}
In this example which is quite poor, you'll have to cast the string to a integer otherwise it will error. But this is an example to help better adhere to Object-Orientation.
Your other issue: "nothing happens when I click the button". This leads me to believe the EventHandler has been broken. Follow these steps:
Go into Designer Mode
Select your Button
Open Properties
You'll see a button with almost a lightning bolt, click it
Locate your on click, and ensure the EventHandler is wired correctly.
This is a designer approach to the coded approach that Sean provided.
Hopefully this helps.

Categories