C# - Writing to a multiline textbox - c#

I'm trying to write the status of the program to the user. But my method to do so doesn't work. statusBox is a textbox windows form.
public static void writetoStatus(string text)
{
TextBox statusBox = new TextBox();
statusBox.Text = text;
}
Help please!

You can't access instance variables with a static method. I can't think of a way that statusBox would not be an instance member. Try making your method non-static and it should be fine.

As mentioned in a previous answer, your method needs to be static in order to access the TextBox on your form.
Also pressing, however is the fact that you're putting the status in a new TextBox instead of the one on your form.
If you created a form and put a TextBox on it, then the TextBox already has a name, and you can access it from the code-behind file. By default, I think it would be Textbox1 or some other number. You can, of course, change this name in the designer file or in the form editor GUI in Visual Studio.
So, lets say you change the name of the existing TextBox to statusBox. Now your method need only be this:
void WriteToStatus(string status)
{
statusBox.Text = status;
}

Ok, i see that you are creating a new instance of text box inside static method. That instance of textbox ends up nowhere and it's destroyed once you your static method is executed.
You can either have:
public static void writetoStatus(TextBox tb, string text)
{
tb.Text = text;
}
// and then later use it like:
writetoStatus(statusBox, text);
Or:
public static void writetoStatus(Form frm, string text)
{
TextBox tb = new TextBox();
tb.Text = text;
frm.Controls.Add(tb);
}
// and then later use it like:
writetoStatus(myForm, text);

Related

C# Get Value of a text box in a Static Void

I am trying to get the value of a text box called txtNum on the form FrmPhone as well as write to the text box txtLog however there seems to be no interacation between the Static Void and the Text Boxes on FrmPhone.
Below is the code for the CreateCall Static Void.
private void btnCall_Click(object sender, EventArgs e)
{
CreateCall();
}
static void CreateCall()
{
FrmPhone FrmPhone = new FrmPhone();
var numberToDial = FrmPhone.txtNum.Text;
FrmPhone.txtLog.Text += "\r\nCall " + numberToDial;
FrmPhone.txtLog.ScrollToCaret();
SetupDevices();
call = softphone.CreateCallObject(phoneLine, numberToDial);
call.CallStateChanged += call_CallStateChanged;
call.Start();
}
What am I missing to allow me to interact with the text boxes on the form while executing code in the static void?
Also worth noting is that If replace
var numberToDial = FrmPhone.txtNum.Text;
with
var numberToDial = "2788";
The call is made but the txtLog text does not change.
There are two options.
You make the method non-static. This is the easiest solution and the one that makes most sense. You are after all using instance variables.
void CreateCall()
{
var numberToDial = this.txtNum.Text;
}
Pass in the form to the method, as said, it doesn't make sense, but it does the job. txtNum needs to be internal or public, or CreateCall must reside in the same class:
static void CreateCall(FrmPhone frmPhone)
{
var numberToDial = frmPhone.txtNum.Text;
}
In calling the code you pass in this:
CreateCall(this);

How to write to a textbox that is in another Form? [duplicate]

This question already has answers here:
Passing a value from one form to another form
(9 answers)
Writing to a textbox on a separate form (C#)
(4 answers)
Closed 5 years ago.
I have a .NET application which has 2 forms: inside Form1 there is all the application stuff, while inside LogForm there is only a readonly textbox. I want to print some text to this textbox inside LogForm from Form1, while Form1 is performing all the work.
I open my LogForm via the
LogForm logForm = new LogForm();
logForm.Show();
But then? How can I do that?
You must have the reference to this TextBox.
Put your access modifier to public in your visual studio form designer and access your TextBox by logForm1.YourTextBox.Text += "new line \r\n";
You can make your LogForm to accept Arguments on initialization:
string ValueFromForm1 = null;
public LogForm(string input)
{
ValueFromForm1 = input;
}
The on Form_Load set the value of textbox:
TextBox1.Text = ValueFromForm1 ;
Create a public variable in Form1 and call in LogForm
Form 1
public static string logformtext;
logformtext="Required text"; //Value which you want to pass to LogForm
LogForm
TextBox1.Text=Form1.logformtext;
Either you can set the text in the constructor of LogForm:
public LogForm(string text)
{
InitializeComponent();
textBox1.Text = text;
}
or you can set the modifier of the TextBox to Internal (or even Public) on the Designer and then access it from Form1 like this:
logForm.textBox1.Text = "Your text";
But keep in mind that while your program is working, the text will not show up on your LogForm, unless you repaint it, or use a BackgroundWorker to have the work be done in a different thread.

Change Textbox object to Static

I have little experience in C#, mostly playing with it as a hobby, and I was wondering what I needed to do to change a textbox from non-static to static.
In Visual Studio 2012, I'm trying to add a line of text using the method textbox1.AppendText("Text");, and it won't work because the textbox isn't static, while the method trying to write the code is static. I can't find the line of code where the textbox is initialized in my code, nor have I found an option in the properties that allows me to change that.
Is there a work-around, or do I need to make it static? And if I need to make it static, how would I do that? I'm at a loss.
Thank you for your help!
EDIT: adding code sample. The method below is called from a second form, same from which the value of x is determined.
public static void getMethod(int x)
{
if (x > 4)
{
textbox1.AppendText("Text");
}
else
{
textbox1.AppendText("Other text");
otherVariable = x;
}
}
It's not clear from your post which GUI framework you're using. Both Winforms and WPF have a TextBox class.
But, to the point of your question: you could in the object where the TextBox is declared and created, also have a static field to which you assign that reference. But that would be a poor design choice, IMHO.
It's not clear what your static code is doing, where it's declared, or who called it (another failing of your question is that you did not provide any code, never mind a concise, complete code example), but assuming the static method is not in the UI object that owns the TextBox instance itself (if it is, then you just need to make the method non-static), the the correct way to address this would be for the UI object that does know about the TextBox instance to have some public method or property used to set the text, and then for the code that invokes your static method to pass the reference of that UI object to the static method, so that it can use the member you added.
For example:
class Form1 : Form
{
public string FieldText
{
get { return textBox1.Text; }
set { textBox1.Text = value; }
}
}
and elsewhere:
static void SomeMethod(Form1 form)
{
// ...do some stuff...
form.FieldText = "some text";
// ...do some other stuff...
}
In your specific scenario, you seem to have two forms: one containing the textbox1 member, and another that passes an int value to a method, where you want to be able to add some text to the TextBox1 based on the value.
In that case, it would look more like this:
class Form1 : Form
{
public void AppendFieldText(string text)
{
textbox1.AppendText(text);
}
}
and in the static method:
public static void getMethod(int x, Form1 form)
{
if (x > 4)
{
form.AppendFieldText("Text");
}
else
{
form.AppendFieldText("Other text");
otherVariable = x;
}
}
Naturally, the caller of the getMethod() method will need the reference for the form parameter; you will have to pass that somehow to that second form which is calling this method, so that it can pass it to the method.
Note that in both of my examples, I have not exposed the TextBox object itself. You should follow this example, exposing only the minimum amount of functionality needed in order to get the job done. This helps ensure that the TextBox object doesn't wind up getting used in appropriately by some other code, and especially helps ensure that your classes remain reasonably decoupled.
On that latter point, I will mention that your code example is still pretty bare. There are other techniques which can solve this problem with even less coupling between the types. But again, lacking a good code example, it's not possible to know for sure what would work, never mind what would be best.
The above example is appropriate, given the information you've shared.
If you would like to edit your question to provide better, more specific detail, a better, more specific answer could be provided.
you can do something like below
private void button3_Click(object sender, EventArgs e)
{
getMethod(textBox1,5);
}
public static void getMethod(TextBox textbox1,int x)
{
if (x > 4)
{
textbox1.AppendText("Text");
}
else
{
textbox1.AppendText("Other text");
otherVariable = x;
}
}
Textboxes aren't static. And you can't make them static, they are all instanciated. The name of your textbox is the instance name.
So just use the text property on the instance of the text box.
textbox1.Text = "Text";
If you want to Append one just do:
textbox1.Text = String.Concat(Textbox1.Text, "more text");
same thing about could also be seen as:
textbox1.Text = textbox1.Text + "more text";

How to continuosly update the textbox in C#

I have the following piece of code:
class NotepadCloneNoMenu : Form
{
protected TextBox txtbox;
public NotepadCloneNoMenu(string a)
{
Text = "Notepad Clone No Menu";
txtbox = new TextBox();
txtbox.Parent = this;
txtbox.Dock = DockStyle.Fill;
txtbox.BorderStyle = BorderStyle.None;
txtbox.Multiline = true;
txtbox.ScrollBars = ScrollBars.Both;
txtbox.AcceptsTab = true;
txtbox.AppendText(a);
txtbox.AppendText("\n");
}
}
class program1
{
public static void Main()
{
string result = "abc";
while(true)
{
Application.Run(new NotepadCloneNoMenu(result));
}
}
}
I want to continuously appending the string result to the textbox so it looks like this:
abc
abc
abc
so on and so forth. However, every time I called this:
Application.Run(new NotepadCloneNoMenu(result));
It will reset the textbox. Is there anyway I can update the textbox continuously? I am fairly new to C# so this is quite confusing to me.
thanks,
Phuc Pham
First of all, you're continuously closing and opening an application. That's why it resets. If you want to run an infinite loop, you probably want to run it inside your application proper.
In your application code, use some event (maybe a timer would suit you) to append text to the textBox. Like this:
public someEventOnTheForm (object sender, EventArgs e)
{
txtBox.Text += "Notepad Clone to Menu";
}
There's two more things to take into account: first, if you don't have a stoping condition, this will just keep filling memory until you run out of it.
Second, windows forms run on only one thread by default. You'll be using that thread to update the textbox, so while it's appending text, the form itself will be unusable. It'll probably blank out during the event if it starts taking long. You'll need a second thread to handle the event if you want your form to be usable.

C# Call function in a class from another class

I'll start of by saying I'm not a developer. Yes this is a c# nightmare. But this is a one time tool and thats it. Quick and Dirty it just needs to work and thats it.
I have the following code:
public string[] get_status(string local_fname)
{
var dts_doc = new HtmlAgilityPack.HtmlDocument();
dts_doc.Load(local_fname);
//Pull the values
var ViewState = dts_doc.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[1]/input[4]/#value[1]");
var EventValidation = dts_doc.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[2]/input[1]/#value[1]");
string ViewState2 = ViewState.Attributes[3].Value;
string EventValidation2 = EventValidation.Attributes[3].Value;
//Display the values
//System.Console.WriteLine(ViewState.Attributes[3].Value);
//System.Console.WriteLine(EventValidation.Attributes[3].Value);
//System.Console.ReadKey();
return new string[] { ViewState2, EventValidation2 };
}
I want to call get_status from a button on my Main.cs which will show 2 Message Boxes with ViewState2 and EventValidation2.
Again, I'm not a developer, this is probably the wrong way of doing things. But I just need a quick and dirty solution to get this job done once.
Make the function static by adding the static keyword to the function definition:
static public string[] get_status(string local_fname)
Use the class name to reference the function from your Main class.
try this:
foreach(string s in get_status(localFname))
{
MessageBox.Show(s);
}
As you said, it is quick and dirty and I stayed faithful to that paradigm.
And yes, if you need to acces another class, make the method static or just simply create an instance and call the method on it. I hope I have understood the problem correctly.
if you are using visual studio, go to the Button you want to click, double-click the button. This will create an eventhandler. In the eventhandler you should call the above method.
protected void Button1_Click(object sender, eventArgs e)
{
string local_fname = someValue;
string results[] = get_status(local_fname);
MessageBox.Show(results[0]);
MessageBox.Show(results[1]);
}

Categories

Resources