im working on a code-editor(winforms) and Im just wondering if its possible to call a specific box from a form to another?
sample for this set of codes:
int line = 1 + richTextBox1.GetLineFromCharIndex(richTextBox1.GetFirstCharIndexOfCurrentLine());
int column = 1 + richTextBox1.SelectionStart - richTextBox1.GetFirstCharIndexOfCurrentLine();
label1.Text = "line: " + line.ToString() + ", column: " + column.ToString();
***code above was inside a timer which calls the count of line and column in a richtextbox like in lower rightpart of actual code editor .
now im just wondering if its possible to call the label that displays to the main form and will display to another but will still run .
like in mainform theres the code for richtextbox and on other form it should have the code of label that connects to the mainform .
my question is it possible to call a tool function from another form
to another?
hope you could help me, really in need and thanks a lot!
As long as you have a reference to that form toolbox, just expose that Label/TextBox or whatever you want to change via a public property and set it from your context.
public class ToolBox : ToolBoxBase
{
public CustomLabel
{
get
{
return label1.Text;
}
set
{
label1.Text = value;
}
}
}
private ToolBox toolbox;
void ShowToolBox()
{
InitToolBox();
toolbox.CustomLabel = "New label";
}
As I'm not even sure what technology the question refers to I added a poor pseudo example to get you the idea. The InitToolBox method initializes the toolbox and displays it, and sets the field toolbox with a reference to it.
If the other form runs on another thread, then you'll have to invoke the label setter asynchronously. See this question for more instructions.
Related
So i am creating an x and o's game and i have a label which shows who's turn it is ( WhosTurnLabel) but when i try to format their chosen name into a string it only assigns the string to the label and not the formatted version.
WhosTurnLabel.Text = $"{playerXName}'s Turn";
playerXName is taken from a text box input.
game.playerXName = PlayerXName.Text;
When i am debugging and hover over the $ it shows the string i want the label to be, but when i check the form and hover my mouse over WhosTurnLabel.Text it only shows "'s Turn".
I assign the names to variables in another class then open that form up here:
Game game;
game = new Game();
game.playerXName = PlayerXName.Text;
game.playerOName = PlayerOName.Text;
game.Show();
Thanks in advance for any help.
p.s: im still learning c# and really i need someone to check my programs code, i think there may be a few hiccups
Have you tried passing in the values for playerXName and playerOName as parameters to the Game form constructor? This worked fine for me:
public partial class Game : Form
{
public string playerXName,
playerOName;
public Game(string player1, string player2)
{
InitializeComponent();
playerXName = player1;
playerOName = player2;
WhosTurnLabel.Text = $"{playerXName}'s Turn";
}
}
I just assigned the label text attribute in the constructor, however, playerXName and playerOName are fields in the object and should be accessible to any methods and event handlers within the Game class. Hope this helps!
Looks like the value, at least at the point the string is created is null, empty, or undefined.
Something else to look into, though I am unsure without seeing more of the code, but should
WhosTurnLabel.Text = $"{playerXName}'s Turn";
be
WhosTurnLabel.Text = $"{game.playerXName}'s Turn";
I don't know it would have compiled or not thrown an error in that case, but just something that stood out to me.
But to sum it up, the string is definitely empty or null, at least at the point the string is created.
Add a Game_Shown event and put WhosTurnLabel.Text = $"{playerXName}'s Turn"; in there
In C# windows forms how can I use TextBox.Text property as another property named TextBox.WritePublicText to display text on a multiline textbox? I am editing C# game code that cannot be modified. I am trying to simulate the game code exactly in Visual Studio to edit and then copy directly into the game code. Also I am new to C# so try to dumb it down.
This code displays the text on the windows form textbox named textBox1:
textBox1.Text = "Text to display\r\n"
+ "More Text\r\n"
+ "More Text2\r\n";
This is the code displays the text in game. But I cannot figure out how make it display the text in a windows form textbox.
textBox1.WritePublicText = "Text to display\r\n"
+ "More Text\r\n"
+ "More Text2\r\n";
Other Ideas:
It might be possible to display multiline text another way on windows forms that will allow me to do this.
Create a class and inherit from Windows.Forms.TextBox.
Like this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication
{
public class CustomTextBox : TextBox
{
public String WritePublicText
{
get { return Text; }
set { Text = value; }
}
}
}
This tool now is exactly like a normal TextBox, but now it contais a new method called "WritePublicText" that sets and returns a String.
Rebuild your application, then you will see a new tool in your toolBox called CustomTextBox, just drag and drop like any other component.
If you already have to many TextBoxes in you application you could go in your designer file and change the 'TextBox' to 'CustomTextBox' and then rebuild again.
Write a subclass of TextBox which has a public property like so:
public String WritePublicText {
get { return Text; }
set { Text = value; }
}
And use that control instead of TextBox in your UI.
You'll probably have to add a constructor as well to get it to work with the form designer in Visual Studio. Look at the Form1.Designer.cs file in your project (assuming the Form is named Form1; use common sense) to see how the current TextBox is being created; that's the constructor you'll need to duplicate.
As an alternative, if you let use method instead of property (moreover Write... being an verb sounds as a method and doesn't look like a property's name) you can implement extension method:
public static class TextBoxExtensions {
public static void WritePublicText(this TextBox textBox, string value) {
if (null == textBox)
throw new ArgumentNullException("textBox");
textBox.Text = value;
}
}
...
// Extension method instead of property
textBox1.WritePublicText(
"Text to display\r\n"
+ "More Text\r\n"
+ "More Text2\r\n");
I'm just reading some LINQ for dummies stuff and I got a question. Here is the code:
private void btnTest_Click(object sender, EventArgs e)
{
// Create an array as a data source.
String[] QueryString = { “One”, “Two”, “Three”, “Four”, “Five” };
// Define the query.
var ThisQuery = from StringValue in QueryString
where StringValue.Length > 3
select StringValue + “\r\n”;
// Display the result.
foreach (var ThisValue in ThisQuery)
txtResult.Text = txtResult.Text + ThisValue;
}
What is txtResult, will it work without a declaration?
Open Visual Studio > New Project > Windows Application
Drag a textbox control onto the form design surface.
Press F4, in the properties window set the Name property of the textbox to txtResult
Drag a button control onto the form design surface.
Peess F4, in the properties window set the Name property of the button to btnTest
Double click the button and it will take you to the buttons click event in code.
Paste in the above code and Press F11 to start debugging.
txtResult is a textbox control. There must be another reference to it elsewhere in the example.
txtresult is the value of Name Property of used TextBox control in this code. You need to add a TextBox in your application and assign it Name property value 'txtresult' for this code to work.
It will give you error if txtresult is not on the form or in your class. Add it on the form/class or define a string variable to hold the results.
foreach (var ThisValue in ThisQuery)
txtResult.Text = txtResult.Text + ThisValue;
I'm calling a public method from another class. It takes in a List as a parameter, and goes through the list printing out each item into a text field. The problem is the text field is remaining empty!. I've checked that the list is populated by outputing the item to the console before I put it into the text box, and the text is coming up fine there.
The list contains strings, and should output each string to the textfield followed by a semi colon.
This is the method which is being called:
public void fillAttachment(List<string> attachList)
{
for (int i = 0; i < attachList.Count; i++)
{
Console.WriteLine("List: " + attachList[i]);
txtAttach.Text += attachList[i] + ";";
}
}
I would solve it in this way:
foreach(var attach in attachList)
{
Console.WriteLine(attach);
txtAttach.AppendText(string.Format("{0};", attach));
}
Setting the text property on a text box and it not displaying could be one of the following:
You are not looking at the same control as you are setting the text in
Could you have instantiated a second copy of the form object and it is this form that you are setting the txtAttach text property in?
Could the control that you are expecting to be populated be a different one? Right click the text box that you want the text to appear in click properties and check the name.
Something else is clearing the textbox after you set it
Right click the txtAttach.Text and click Find All References, this will show you all the places that the Text property is referenced - written and read - in your project. This is a very useful way to locate other interaction with this control.
Fomatting is making the text box appear empty
Is the Font too small, or in the same colour as the background. Can you select the text in the text box?
The easiest way to test all of the above is to create a new text control on your form with a different name, change your code to populate it, check that it is indeed populated, then replace the old one.
As an aside, you could also reduce the code with a single line as follows:
public void fillAttachment(List<string> attachList)
{
txtAttach.Text = String.Join(";", attachList.ToArray());
}
Although this obviously skips out the console write line function.
Not sure why yours doesn't work but I would have done it like this...
public void fillAttachment(List<string> attachList)
{
string result = "";
//OR (if you want to append to existing textbox data)
//string result = txtAttach.Text;
for (int i = 0; i < attachList.Count; i++)
{
Console.WriteLine("List: " + attachList[i]);
result += attachList[i] + ";";
}
txtAttach.Text = result;
}
Does that work for you? If not then there is something else very wrong that is not obvious from your code
I have a winforms application.
I have a textbox on one form (call F1) and when a button is clicked on this form (call F2), it launches another form.
On F2, I want to set a string via a textbox (and save it to a variable in the class), and then when I close this form, the string will appear in a label in F1.
So I am basically sharing variables between both forms. However, I can't get this to work correctly. How would this code look?
I would add a new property to form2. Say it's for a phone number. Then I'd add a friend property m_phone() as string to form 2. After showing an instance of form2 but before closing it, you can refer to the property m_phone in form1's code.
It's an additional level of indirection from Matthew Abbott's solution. It doesn't expose form2 UI controls to form1.
EDIT
e.g.:
public string StoredText
{
get;
private set;
}
inside the set you can refer to your UI control, like return textBox1.text. Use the get to set the textbox value from an earlier load.
And:
public string GetSomeValue()
{
var form = new F2();
form.ShowDialog();
return form.StoredText;
}
Just ensure that StoredText is populated (or not, if appropriate) before the form is closed.
Are you showing the second form as a dialog, this is probably the best way to do it. If you can avoid doing shared variables, you could do the following:
public string GetSomeValue()
{
var form = new F2();
form.ShowDialog();
return form.TextBox1.Text;
}
And called in code:
Label1.Text = GetSomeValue();
This might not be the most efficient way of approaching, but you could create a class called DB (database). Inside this class, create variables like
public static bool test or public static bool[] test = new bool[5];
In your other forms, you can just create an instance. DB db = new DB(); then grab the information using db.test = true/false. This is what I've been doing and it works great.
Sorry, I'm only like a year late.