Best way to copy a comboBox from form1 to form2 - c#

First I set the modifier property to "Internal" of comboBox1 on form1.
I used the following code:
form1 f1 = new form1();
object[] obj = new object[f1.comboBox1.Items.Count];
f1.comboBox.Items.CopyTo(obj, 0);
comboBox2.Items.AddRange(obj);`
Is it the best way to do this?
PS: I couldn't make this: Best way to access a control on another form in Windows Forms? to work.
PPS: Making controls is public is not what I like and neither preferred.

If you are wanting two drop-down lists with the same items in them it is much better that you store those items somewhere common and build up both combo boxes from there.
e.g.
public class Context{
...
...
public List<Foo> FooItems {
get{...}
}
}
public class Form1 {
...
combobox.AddRange(this.context.FooItems);
...
}
public class Form2 {
...
combobox.AddRange(this.context.FooItems);
...
}
This way you prevent coupling between your different forms, and still have only one place where you derive the values that go into the list.

Related

C# Get field from Main Form

I have a main form and I need in other class get or set field from Main Form. I created public property in Main Form:
public partial class MainForm : Form
{
public string get_txt()
{
return phone.Text; //phone - textbox
}
}
But in other class I can't get this property:
MessageBox.Show(MainForm.get_txt); //there error. It doesn't seemed get_txt property.
I'm sorry for this simpliest question, but I really don't know this. Everywhere wrote that simpliest way to do it - in class of Main Form create public property for needed private field in Main Form. What do I wrong?
So the MessageBox.Show(MainForm.get_txt) won't compile as you're trying to refer to a static property, not an instance method.
I'm not a winforms dev, so this is a bit of a guess:
Somewhere in your code you will have a call like var main = new MainForm(). On that instance i.e. main you will be able to call main.get_txt().
As a side note, given that you're working in C# it'd be good to name the method to something a little more idiomatic, perhaps public string GetPhoneNumber() or better yet make it a property:
public string PhoneNumber
{
get { return phone.Text; }
}
Make instance of MainForm and than call it.
Mainform _mainform=new Mainform();
MessageBox.Show(_mainform.Get_text);
Your MainForm is not static class, you can not use like that. If u want access the MainForm(methods or variables), you have to assing the variable.
MainForm mForm = new MainForm();
MessageBox.Show(mForm.getText());
Thanks to you all.
but when I create a new istance of Main Form all controls in it will be initializing like in my current Main Form?

Passing tabConrol properties from on form to another

I am using two forms in a windows form application in C#.I want to pass the tabControl's properties like its "Tabpage count" from first form to second form. Can anyone help me here?I can't create object of first form in second form and call a function beacuse for a new forn object, the tabcontrol gets refreshed.
Inside your first form create an instance of your second Form class as this
Form frm= the instance of your secand form
after that show the instance of your secand form, now you exactly have an instance of your secand form inside your first form and can use all the public properties of it
You can create static public functions exposing desired control properties like in below code.
public static Color TabColor()
{
return Form1.Fom1TabControl1.SelectedTab.ForeColor;
}
and you can access Form1 properties like below;
private void Form2_Load(object sender, EventArgs e)
{
this.Fom2TabControl1.SelectedTab.ForeColor = Form1.ForeColor;
}
First Check your class accessibility and set to public if not work set public static, maybe your namespaces is different
hope it helps
This can be achieved in two ways
Aprroach 1:
Create a public variable in Form2
public int intTabCount=0;
and in Form1, you should call Form2 like
Form2 objForm2 = new Form2();
objForm2.intTabCount = tabPageCountVariable;
objForm2.Show()
Aprroach 2:
Create a parameterized constructor and public variable in Form2
public int intTabCount=0;
public Form2(int TabCounts)
{
intTabCount = TabCounts; // and use intTabCount for your class
}
and call from Form1 like
Form2 objForm2 = new Form2(tabPageCountVariable);
objForm2.Show();
Now if you want to pass value through any events like clicking an button in Form1 which updates anything in Form2, use the below link
Passing Values Between Windows Forms c#

How to access buttons in other class from Form1.cs

I have 9 buttons in Form1.Designer.cs and I want to access them in another class, Puzzle.cs because later on I need to modify button changes in model class. The code below is what I attempted.
private Button[,] buttons = new Button[3, 3]
{ { Form1.button1, Form1.button2, Form1.button3 },
{ Form1.button4, Form1.button5, Form1.button6 },
{ Form1.button7, Form1.button8, Form1.button9 } };
It fails as the modifier for buttons is not static. I changed them into static type but this causes errors for buttons.
Can anyone give some advice?
You need a reference to an instance of the Form1 class. If it were called form, you could then access the button like form.button1.
But I'm not sure accessing buttons of the form from another class is a good design.
To get things compiling correctly, you'll need to make the following changes:
public partial class Puzzle : Form
{
private Button[,] buttons;
public Puzzle(Form1 form1)
{
buttons = new Button[,]
{
{ form1.Button1, form1.Button2, form1.Button3, },
{ form1.Button4, form1.Button5, form1.Button6, },
{ form1.Button7, form1.Button8, form1.Button9, },
}
};
}
The idea here is that Form1 is a class, not an instance of a class... A class is like a blueprint of a house, and a house is like an instance of that blueprint.
To get furniture out of the house, you first must instantiate and initialize that house (build, if you will), and then access the house's furniture instances.
In this case, you'll need to instantiate a new Form1, and pass that to the Puzzle form's constructor.
Form1 myForm = new Form1();
Puzzle myPuzzle = new Puzzle(myForm);
Puzzle will now be able to access that instance of Form1's buttons.
Note:
You can find the Program's instance of Form1 by looking in the Program.cs file of your solution.

calling method of parent form in c#

i have opened a mainform and call a child form like
Form4 f = new Form4();
f.Owner = this;
f.Show(this);
in form4, user selects a text file, the contents of which are to be displayed in a textBox1 of mainform
i was trying something like
Owner.textBox1.Text = "file contents";
but it does'nt work
The best way to link different forms together is via events. Create an event in Form4 like FileSelected and then do something like this:
Form4 f = new Form4();
f.FileSelected += (owner, args) => {
textBox1.Text = args.FileName;
};
f.Show(this);
Besides this is really bad design, you need to make textBox1 a public member of your main form and cast f.Owner to the main form type.
Like:
Form4 f = new Form4();
f.Owner = this;
f.Show(this);
// Inside Form4
MainForm main = this.Owner as MainForm;
if (main != null) main.textBox1.Text...
A best practice would be to define yourself a property that would itself set the Text property of your private control. Here's an instance:
public partial class MainForm : Form {
public string ContentDescription {
set {
textBox1.Text = value.trim();
}
}
}
Then after, you'll be able to access this property through type-casting to your particular type:
public partial class SecondaryForm : Form {
public MainForm OwnerForm {
get {
return (MainForm)this.Owner;
}
}
public void someMethod() {
OwnerForm.ContentDescription = "file contents";
}
}
Remember that in C#, every Control is declared private. So, to access it, the best practice is to define a property that will grant you the required access to it. Making a member public is generally not a good idea, depending on what you're trying to achieve.
EDIT For the parse method, perhaps should you consider making it public or internal so that you may access it through the correctly type-casted Owner property of your child form.
Making a hlper class might be the right solution though, so it is not GUI dependent.
In Form4 you can cast Owner to the correct type:
var o = (Form1) this.Owner;
o.textBox1.Text = "file contents";
For this to work, the owner must be of type Form1 and textBox1 on that type must be a public member or property.
As Andrew already gave the correct solution for event driven, there is also a sync (or blocking) method available:
Form4 f = new Form4;
if(f.ShowDialog() == DialogResult.OK)
{
textBox1.Text = f.FileName;
}
You will need to set the "modifiers" to at least public for the properties of the control to be able to have access to it.
alt text http://gabecalabro.com/gabe/Capture.PNG

C# WinForms - How Do i Retrieve Data From A Textbox On One Form Via Another Form?

I have a method that executes inside one form, but I need to retrieve data from another form to pass into the method.
Whats the best way of doing this?
You can expose a property on one form and call it from the other. Of course you'll need some way of getting the instance of form1. You could keep it as a static property in the program class or some other parent class. Usually in this case I have a static application class that holds the instance.
public static class Application
{
public static MyForm MyFormInstance { get; set; }
}
Then when you launch the first form, set the application MyFormInstance property to the instance of the first Form.
MyForm instance = new MyForm();
Application.MyFormInstance = instance;
Add a property to the second form.
public String MyText
{ get { return textbox1.Text; }
set { textbox1.Text = value; }
}
And then you can access it from your second form with:
Application.MyFormInstance.MyText
On the form that has the textbox you need data from, expose either a Property or a Method that returns the text. IE:
internal string TextBoxTest
{
get{ return this.textBox1.Text;}
}
There is a similar post here
The videos below will clear up a lot of your concepts about passing data between 2 forms.
There are multiple ways to pass data between 2 forms check these links which has example videos to do this
FormToForm Using Properties - http://windowsclient.net/learn/video.aspx?v=108089
FormToForm Using Parameters - http://windowsclient.net/learn/video.aspx?v=105861
HTH
Assuming that formB is initialized in formA I would recommend adding a string to the constructor of formB sending the Texbox1.Text
as in
class formB: Form{
private string data;
public formB(string data)
{
InitializeComponent();
this.data = data;
}
//rest of your code for the class
}
Don't do this.
Longer version: Why is your view directly interacting with another view?
Much longer version:
Rather than making a public property that exposes the field, it would provide better encapsulation and insulation from change if the form with the field of interest interacted with some form of data object, which was then passed to the interested method.
The location of the interested method should be carefully considered - if it controls aspects of the view (WinForm, in your case), then it may be appropriately a member of that class - if not, perhaps its real home is in the data object?

Categories

Resources