display text in textbox
-
i have developed a very simple application which contains a single win form and two classes. i would like provide or show some text from those classess into my win textbox form. how can i achieve it? what I want excatly sames as "View Syn status" in MY SQL Replication. i am trying to achieve from c#.
Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan
-
i have developed a very simple application which contains a single win form and two classes. i would like provide or show some text from those classess into my win textbox form. how can i achieve it? what I want excatly sames as "View Syn status" in MY SQL Replication. i am trying to achieve from c#.
Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan
Use
string s = "Hello";
textBox.Text = s;for a single line TextBox, or
string[] as = new string[n];
...
TextBox.Lines = as;for a multiline.
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones
-
Use
string s = "Hello";
textBox.Text = s;for a single line TextBox, or
string[] as = new string[n];
...
TextBox.Lines = as;for a multiline.
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones
my friend. i am asking about how can i access from other classess not from my win form. from win form that is ok, but if i want to access it from other classes how can i access it and set the text to "hello" or something else?
Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan
-
my friend. i am asking about how can i access from other classess not from my win form. from win form that is ok, but if i want to access it from other classes how can i access it and set the text to "hello" or something else?
Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan
Set a public property of the form to set the text like
public string SetText
{
get
{
return textbox1.Text;
}
set
{
textbox1.Text = value;
}
}And call it from your class by accessing it through the instance of the form on which the text box resides.
Ahsan Ullah Senior Software Engineer MCTS 2.0
-
my friend. i am asking about how can i access from other classess not from my win form. from win form that is ok, but if i want to access it from other classes how can i access it and set the text to "hello" or something else?
Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan
If I understand you correctly, you have a form with a textbox, and a separate class which does some work and is to display text in the textbox? If yes, then either: 1) the class needs to be given the instance of the form. or 2) the class needs to signal to the form that data is ready for display. The first way is pretty trivial, the second is better practice. Assuming:
myForm formMine = new myForm();
myClass classMine = new myClass();In the first case, create a field in myClass:
public myForm formDisplayResult;
and assign it after the form and class creation:
myForm formMine = new myForm();
myClass classMine = new myClass();
classMine.formDisplayResult = formMine;then access the textbox from your class:
formDisplayResult.tbResultsGoHere.Text = "Hello";
In the second case, it is a little more complex: In myClass:
public partial class myClass { // Signal file change happened public delegate void ChangeHandler(object sender, EventArgs e); public event ChangeHandler Changed; protected virtual void OnChanged(EventArgs e) { ChangeHandler ch = Changed; if (ch != null) { ch(this, e); } } private void DoSomethingToChangeData() { OnChanged(null); } }
----- The asign to ch is in case the handler changes between null check ----- and exec. ----- (unlikely, but possible) ----- The null check is to ensure there is a handler. If not, better to ----- ignore it gracefully, than to rely on the thrown exception ----- (NullReferenceException) In myForm:
myClass classMine = new myClass(); public myForm() { classMine.Change += new myClass.ChangeHandler(Changed); } // // Fired when the results are available // private void Changed(object sender, EventArgs e) { tbResultsGoHere.Text = classMine.strResults; }
It is easy really - you just have to get your head around it!
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones
-
If I understand you correctly, you have a form with a textbox, and a separate class which does some work and is to display text in the textbox? If yes, then either: 1) the class needs to be given the instance of the form. or 2) the class needs to signal to the form that data is ready for display. The first way is pretty trivial, the second is better practice. Assuming:
myForm formMine = new myForm();
myClass classMine = new myClass();In the first case, create a field in myClass:
public myForm formDisplayResult;
and assign it after the form and class creation:
myForm formMine = new myForm();
myClass classMine = new myClass();
classMine.formDisplayResult = formMine;then access the textbox from your class:
formDisplayResult.tbResultsGoHere.Text = "Hello";
In the second case, it is a little more complex: In myClass:
public partial class myClass { // Signal file change happened public delegate void ChangeHandler(object sender, EventArgs e); public event ChangeHandler Changed; protected virtual void OnChanged(EventArgs e) { ChangeHandler ch = Changed; if (ch != null) { ch(this, e); } } private void DoSomethingToChangeData() { OnChanged(null); } }
----- The asign to ch is in case the handler changes between null check ----- and exec. ----- (unlikely, but possible) ----- The null check is to ensure there is a handler. If not, better to ----- ignore it gracefully, than to rely on the thrown exception ----- (NullReferenceException) In myForm:
myClass classMine = new myClass(); public myForm() { classMine.Change += new myClass.ChangeHandler(Changed); } // // Fired when the results are available // private void Changed(object sender, EventArgs e) { tbResultsGoHere.Text = classMine.strResults; }
It is easy really - you just have to get your head around it!
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones
thx alot, I really appreciate the second one, the first one might be bad habbit :) but one thing, does the class inherits from some thing, or its simply public class className { //cnt and others {
Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan
-
thx alot, I really appreciate the second one, the first one might be bad habbit :) but one thing, does the class inherits from some thing, or its simply public class className { //cnt and others {
Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan
Either. It makes no real difference for your example.
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
thx alot, I really appreciate the second one, the first one might be bad habbit :) but one thing, does the class inherits from some thing, or its simply public class className { //cnt and others {
Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan
As Henry said, it doesn't matter - any class (derived or not) can generate events. Sorry for the late reply, I was busy making a bulk supply of meatballs...
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones
-
If I understand you correctly, you have a form with a textbox, and a separate class which does some work and is to display text in the textbox? If yes, then either: 1) the class needs to be given the instance of the form. or 2) the class needs to signal to the form that data is ready for display. The first way is pretty trivial, the second is better practice. Assuming:
myForm formMine = new myForm();
myClass classMine = new myClass();In the first case, create a field in myClass:
public myForm formDisplayResult;
and assign it after the form and class creation:
myForm formMine = new myForm();
myClass classMine = new myClass();
classMine.formDisplayResult = formMine;then access the textbox from your class:
formDisplayResult.tbResultsGoHere.Text = "Hello";
In the second case, it is a little more complex: In myClass:
public partial class myClass { // Signal file change happened public delegate void ChangeHandler(object sender, EventArgs e); public event ChangeHandler Changed; protected virtual void OnChanged(EventArgs e) { ChangeHandler ch = Changed; if (ch != null) { ch(this, e); } } private void DoSomethingToChangeData() { OnChanged(null); } }
----- The asign to ch is in case the handler changes between null check ----- and exec. ----- (unlikely, but possible) ----- The null check is to ensure there is a handler. If not, better to ----- ignore it gracefully, than to rely on the thrown exception ----- (NullReferenceException) In myForm:
myClass classMine = new myClass(); public myForm() { classMine.Change += new myClass.ChangeHandler(Changed); } // // Fired when the results are available // private void Changed(object sender, EventArgs e) { tbResultsGoHere.Text = classMine.strResults; }
It is easy really - you just have to get your head around it!
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones
Good answer! For future reference, there is no need to create a delegate if using 2.0 or above.
public event ChangeHandler Changed;
can be written aspublic event EventHandler Changed;
If custom event args are needed, there is a generic event handlerpublic event EventHandler<ChangedEventArgs> Changed;
Yay, no more delegate writing when using events :-DDave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
If I understand you correctly, you have a form with a textbox, and a separate class which does some work and is to display text in the textbox? If yes, then either: 1) the class needs to be given the instance of the form. or 2) the class needs to signal to the form that data is ready for display. The first way is pretty trivial, the second is better practice. Assuming:
myForm formMine = new myForm();
myClass classMine = new myClass();In the first case, create a field in myClass:
public myForm formDisplayResult;
and assign it after the form and class creation:
myForm formMine = new myForm();
myClass classMine = new myClass();
classMine.formDisplayResult = formMine;then access the textbox from your class:
formDisplayResult.tbResultsGoHere.Text = "Hello";
In the second case, it is a little more complex: In myClass:
public partial class myClass { // Signal file change happened public delegate void ChangeHandler(object sender, EventArgs e); public event ChangeHandler Changed; protected virtual void OnChanged(EventArgs e) { ChangeHandler ch = Changed; if (ch != null) { ch(this, e); } } private void DoSomethingToChangeData() { OnChanged(null); } }
----- The asign to ch is in case the handler changes between null check ----- and exec. ----- (unlikely, but possible) ----- The null check is to ensure there is a handler. If not, better to ----- ignore it gracefully, than to rely on the thrown exception ----- (NullReferenceException) In myForm:
myClass classMine = new myClass(); public myForm() { classMine.Change += new myClass.ChangeHandler(Changed); } // // Fired when the results are available // private void Changed(object sender, EventArgs e) { tbResultsGoHere.Text = classMine.strResults; }
It is easy really - you just have to get your head around it!
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones
Well, i think i need more help for (int i=0; iAbdul Rahaman Hamidy Database Developer Kabul, Afghanistan
-
Well, i think i need more help for (int i=0; iAbdul Rahaman Hamidy Database Developer Kabul, Afghanistan
I think you mean that your class generates a sequence of results, rather than a single result? If so, then I would output them to a list and signal to the higher level that there were available. Specifically, I would set up a queue in your class:
public Queue<string> qsOutputResults = new Queue<string>(100);
(Always create a queue with a minimum size or it gets extended the first time you use it) Then when your results are available:
qsOutputResults.Enqueue(i.ToString());
OnChanged(null);This adds the results to the queue and signals the results are ready. In your form:
private void Changed(object sender, EventArgs e)
{
while (classMine.qsOutputResults.Count > 0)
{
tbResultsGoHere.AppendText(classMine.qsOutputResults.Dequeue());
}
}Using the queue means they come off in the same order they went on. If your form is too busy, they will just wait on the queue until you are ready. You can replace the string with whatever is sensible for your results (of course). You probably don't want to AppendText (it depends on your app) it was just for example.
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones
-
Good answer! For future reference, there is no need to create a delegate if using 2.0 or above.
public event ChangeHandler Changed;
can be written aspublic event EventHandler Changed;
If custom event args are needed, there is a generic event handlerpublic event EventHandler<ChangedEventArgs> Changed;
Yay, no more delegate writing when using events :-DDave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus)Thanks! I didn't know that, I'll give it a try sometime soon. Would be nice to simplify delegate / handler construction a bit...
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones
-
I think you mean that your class generates a sequence of results, rather than a single result? If so, then I would output them to a list and signal to the higher level that there were available. Specifically, I would set up a queue in your class:
public Queue<string> qsOutputResults = new Queue<string>(100);
(Always create a queue with a minimum size or it gets extended the first time you use it) Then when your results are available:
qsOutputResults.Enqueue(i.ToString());
OnChanged(null);This adds the results to the queue and signals the results are ready. In your form:
private void Changed(object sender, EventArgs e)
{
while (classMine.qsOutputResults.Count > 0)
{
tbResultsGoHere.AppendText(classMine.qsOutputResults.Dequeue());
}
}Using the queue means they come off in the same order they went on. If your form is too busy, they will just wait on the queue until you are ready. You can replace the string with whatever is sensible for your results (of course). You probably don't want to AppendText (it depends on your app) it was just for example.
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones
thx dude, that is really appreciated. the thing which i got concern with, My messsage will not increase than 1000 lines, in each 1000 lines i will clear the textbox contents, well do i need to use queue for my case, or i should use some other collection for good performance? and if the size of queue increase will it affect the speed and performance of computer?
Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan
-
thx dude, that is really appreciated. the thing which i got concern with, My messsage will not increase than 1000 lines, in each 1000 lines i will clear the textbox contents, well do i need to use queue for my case, or i should use some other collection for good performance? and if the size of queue increase will it affect the speed and performance of computer?
Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan
The performance of the queue should be pretty good anyway (except when the size is increased, at which point a new structure has to be created and populated with a copy of the original). Just a case of setting the initial size to a reasonable limit. If you use another collection (a List<T> for example) then you would have to take care of the FIFO ordering which a Queue guarantees. If you are displaying the latest results (as you seem to) then why re-invent the wheel? [edit]Removed word "not" from "should not be pretty good" above[/edit]
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones
-
The performance of the queue should be pretty good anyway (except when the size is increased, at which point a new structure has to be created and populated with a copy of the original). Just a case of setting the initial size to a reasonable limit. If you use another collection (a List<T> for example) then you would have to take care of the FIFO ordering which a Queue guarantees. If you are displaying the latest results (as you seem to) then why re-invent the wheel? [edit]Removed word "not" from "should not be pretty good" above[/edit]
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones
thx alot, you helped me alot, thanks again :)
Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan