Build a new class - c#

I have simple Form1.cs which includes buttons, text boxes, etc.
I would like to bulid a new class and to have the ability to call buttons and test boxes from Form1.cs
In the new class, when i type for example textbox. nothing appears.
What is the easiest way to do this?
Thank you

You can pass the Form1 instance to the new class. The easiest way is to assign it at initialization. If you create the new class instance while in Form1, then use this:
In Form1:
NewClass nc= new NewClass(this);
With the new class looks like this:
public class NewClass
{
Form1 fm;
public NewClass(Form1 frm)
{
fm=frm;
}
void ChangeTextBox()
{
fm.YourTextBox.Text="Foo";
}
}

You should try
Form1.textbox
or
Form frm = new Form1();
frm.TextboxName
Where as to get the values of one form to another form you can also send it via objects. If you create the class of the values you are taking. and assign the values to the class members.

You need to take several steps for this.
First of all, you need to expose the controls you want to access outside the form. To do so, in designer, select each control -> right-click -> Properties -> Modifiers -> change selection to Public;
Afterwards, in your class, create an instance of Form1 and access the controls you need:
var form = new Form1();
form.TextboxName.Text = "some text";

create a new instance of the class \ form
var myForm = new Form2()
then use myForm then call the control from this such as
myForm.TextBox1.Text = "your text here"

Related

how to know which form called another form in C#

I am having multiple forms with Buy button provided. The forms i am having are LawnA.cs and Lawnb.cs, i want to use single Buy.cs form for both of these forms. I mean I want to know what form called the Buy.cs.
In Form LawnA.cs
buy.lotAtobuy = this;
buy.ShowDialog();
In Form LawnB.cs
buy.lotBtobuy = this;
buy.ShowDialog();
In Form Buy.cs
public LawnA lotAtobuy;
public LawnB lotBtobuy;
((LawnA)lotAtobuy).textBox1.Text;
((LawnB)lotBtobuy).textBox1.Text;
In class Buy.cs, I want to execute:
((LawnA)lotAtobuy).textBox1.Text;
if LawnA.cs called Buy.cs while if LawnB.cs called Buy.cs I want to execute this code:
((LawnB)lotBtobuy).textBox1.Text;
You need to to define separate object for each class instead for that define the variable as object, and check the type of object before assigning the text. Which means the declaration of that variable in Buy.cs will be:
public object lotToBuyInstance;
So that you can get the type of object and compare before use, that would be like thi:
if (lotToBuyInstance.GetType() == typeof(LawnA))
{
((LawnA)lotAtobuy).textBox1.Text;
}
else if (lotToBuyInstance.GetType() == typeof(LawnB))
{
((LawnB)lotAtobuy).textBox1.Text;
}
// and so on
Consider that you wanted to create another class(let it be some LawnC) then you need not to create an object of that type and make changes as per that, Just add another condition in your if else if ladder to make them work
Try this in the constructor for the receiving form:
using System.Diagnostics;
public FormThatWasCalled
{
string caller = new StackTrace().GetFrame(1).GetMethod().DeclaringType.Name;
InitializeComponent();
}

C# Cannot implicitly convert one form to another form

So i am trying to store my main form and open a new one however i get this error, here is the code:
I have this at form level
public static frmAddBook frmkeepBooks = null;
public frmMain()
{
InitializeComponent();
frmkeepBooks = this;
}
The error underlines "this" saying it "Cannot inplicitly convert type Books.frmMain to Books.frmAddBook"
Change the first line into:
public static frmMain frmkeepBooks = null;
The types should be equal (or in herited) and probably it is not.
Are you trying to just show the new form on top of the old as a dialog keeping the old form up? I don't quite understand why you are trying to set your instance of frmMain to equal a null instance of frmAddBook.
if you are trying to open new form as a dialog you would do something like this:
public static frmAddBook frmkeepBooks;
public frmMain()
{
InitializeComponent();
frmKeepBooks = new frmAddBook();
/* if you want to display the 2nd form ontop of the first disallowing
user interaction on the first until the 2nd form closes */
frmKeepBooks.ShowDialog();
// If you want to allow interaction on either form
frmKeepBooks.Show();
/* maybe you don't want to display the first form
anymore after the 2nd form is displayed */
this.Visible = false;
}
I think this question needs some clarification on what you are trying to do exactly.

How to set text in form from another form?

I'm working on a term paper that performs operations with complex numbers. Application looks like this:
If you click on "Calculate", will calculate the operands and the result is displayed in a new dialog box. So I have two dialogs - a main window (up on pic) to display the read result (down on pic). Unfortunately I was not able to save result in the main dialog box, which is then written to a text file. There is important piece of code:
Thank you for ideas.
In your MainForm class declare a property as
public class MainForm:Form
{
public string CalculationResult { get; set; }
...
...
//Your code here
...
...
}
then on method of calculate change it to
if(resultBool!=null)
{
CalculationResult = resultBool.ToString());
formResult dlg=new formResult(CalculationResult);
dlg.Owner=this
dlg.StartPosition=FormStartPosition.CenterParent;
dlg.ShowDialog();
}
else
{
...
...
//your code
...
...
}
and change the following line
sw.WriteLine("Result: ");
to
sw.WriteLine("Result: " + CalculationResult);
in saveData method which is in MainForm class. Hope this will work for you. Happy coding
You have to use a reference on the target form to write on it.
You have to assure that you use the correct targetForm reference. You could either create a new targetForm or use an existing one. You could for example use the "parent" form of your dialog box and set the text of the main form textbox, from a dialog box event (using this as MSDN reference):
Dim targetForm As Form = Me.parentForm
targetForm.targetTextBox.Text = "text"
Hope I helped!
In formResult create a variable which keep a reference of Main form
private MyMainForm _mainForm;
Then create one more constructor which take reference of your Main form as parameter:
public formResult(MyMainForm mainform)
{
this.InitializeComponent();
this._mainForm = mainform;
//Now you have access to public property ResultOut
this.textBoxResult.Text = this._mainForm.ResultOut;
}
After this you can use all public properties and method of your main form in second form(formResult)

Modifying a form object created by application.run(form1)

I'm new to C# and trying to figure out how to edit a form from another class. The form is created by the default VS approach, as so:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
I've created a method in the Form1.cs file, just as a test for how to update a label1 field that is in Form1. Here is the method:
public void UpdateLabel(string state = "Changed Text")
{
label1.Text = state;
}
The problem I'm having is that the Application.Run command doesn't provide for a named object of type Form1. So when I want to trigger the UpdateLabel method from Program.cs, like so:
XXX.UpdateLabel();
I don't have an object to target to access the form. If I were creating the form manually, then I believe this would work fine:
Form1 myForm = new Form1();
myForm.UpdateLabel();
With the Application.Run(Form1) that the Windows Form Application provides, how do I access the form object that is being created? Also, is this the approach I should be taking for this type of problem, or is there a better method?
Well, you could integrate your last example in this way....
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 myForm = new Form1();
myForm.UpdateLabel();
Application.Run(myForm);
}
but let me ask you: Why your logic dictates to do this outside the Form1 constructor?
public Form1()
{
InitializeComponents();
label1.Text = "Changed Text";
}
EDIT: Following the comments below I think you should work on something like this:
MyApplicationCode appCode;
public Form1()
{
InitializeComponents();
appCode = new MyApplicationCode();
this.Text = appCode.GetFormText();
label1.Text = appCode.GetLabelText();
cmdSave.Enabled = appCode.UserHasSavePermission();
...... // and so on for other decisions on
}
You could fix the immediate issue with:
Form1 form = new Form1());
form.UpdateLabel();
Application.Run(form);
As for the second question, it really depends on what you are trying to ultimately achieve.
So you have some fairly complicated logic that is used to determine the initial value of some controls in your form. Due to the complexity of that logic, you would prefer to extract that code from the definition of Form1 and move it to another class. That is all good so far.
You can create some other class, have Form1 use that class, and have it provide a value to Form1. Rather than having some other class that has-a Form1 (which would be the effect of putting your code in Main, Form1 should have that other class.
Implementing this is fairly simple. You create another class, you give it an instance or static method that returns a string. Form1 either calls the static method, or creates an instance of the class and calls the instance method. It then sets a label based on the results of that method.
Application.Run is typically used to launch a WinForms application until such a point that that application closes. What function of your Program.cs class is supposed to call UpdateLabel? Is your application launched externally with label value parameters?

Get values from a control in form1 into a new form C#

I am trying to have a click event on a new form that gets the text property of controls in form1.
I have made a public method that returns the values that I need but the returned values are always null. I have looked everywhere for this.
Form1:
public List<string> returner()
{
List<string> thevalues = new List<string>();
thevalues.Add(textbox1.Text);
thevalues.Add(textbox2.Text);
return thevalues;
}
Form2:
Form1 x = new Form1();
List<string> values = x.returner();
label1.Text = values[0];
label2.Text = values[1];
My issue is that there are no values returned because Im declaring a new instance of Form1 rather than using the one that has values in it (I guess).
Yes, that would explain what's going wrong. Basically you need to tell Form2 about the relevant instance of Form1. Exactly how you do that will depend on what constructs everything. For example, you might have:
Form1 form1 = new Form1();
Form2 form2 = new Form2();
form2.Form1 = form1;
Or you could pass the reference in the constructor to Form2.
If those are really the names of your forms, by the way, I'd strongly advise you to rename them to something more meaningful - something which indicates the purpose of the form. Likewise returner not only violates .NET naming conventions, it also doesn't explain what it's doing.
you've messed up with your codes.. if you want to get a value of text just use this. string textValue = form1.textbox1.Text
or.. since you didnt post the full code here.. try this rather than creating the object form1.returner();

Categories

Resources