I have a combobox on form1 that I need to call on form2 to get the user selection. Can some one please give me an example on how to do this?
EDIT: Forgot to explain what Im trying to do. I have a readonly textbox....a user clicks edit to edit the text but I want the text they want/chose to edit to pop up right when form2 is called.
I have this code on form1
public string SelectedComboValue
{
get { return comboBox1.SelectedItem.ToString(); }
}
And this code on form 2
EDIT: Added Form1 form1 = null; BUT its still not returning the SelectedComboValue
public Form2(Form1 parentForm1) : this()
{
form1 = parentForm1;
}
But it gave me an error saying that form1 is not in this context
I suppose that Form1 is the parent of Form2, so when you create the Form2 you use code like this
Form2 f = new Form2(this);
then in the Form2 class you should have a declaration like this
Form1 _parentForm = null;
and in the Form2 constructor
public Form2(Form1 parentForm1)
{
_parentForm = parentForm1;
}
If this is true then you can call
_parentForm.SelectedComboValue ;
to get the result required
in c#
Form 2:
create a combobox here
public string strDecVal{
set{ combobox1.text = value; }
}
in Form 1:
for example you have a textbox and a button that will go to form2
put these code on your button
Form2 frmShow = new Form2(); //Calling the form2
frmShow.strDecVal = textbox1.text;
frmShow.ShowDialog;
In VB it is much more automated:
Form1:
textbox and button
in clicking the button in form1
put the code:
Form2.Show()
in Form2:
on the Load put this code:
ComboBox1.Text = Form1.TextBox1.Text
You can wrap the combobox an object of the ComboBox class like this:
internal static ComboBox CB=comboBox1;
Then you can call it in the other form, and access all of the methods and attributes of the ComboBox class. If you want to add items to that CB, you can do it easily as you do in the parent form. It doesn't matter if it's internal or static, it's just for the example.
Related
'Form1' and 'form2' are open in mdi. when i press button in 'form1' it should call evevt or methods of 'form2', e.g. like 'checkbox' checked ,refresh grid
for that I have code for form2(child form):
public partial class Form2: Form
{
private Form1 Form1_Obj1;
public Form2(Form1 Form1_Obj2)
{
InitializeComponent();
Form1_Obj1 = Form1_Obj2;
}
public Form2()
{
InitializeComponent();
}
for calling events from form1 I have code
Form2 obj=new Form2(this);
obj1.chkSortPlace.Checked = true;
or
obj1.chkSortPlace_CheckedChanged(null, null);
problem is event is call but code in event i.e.assign datasource to gridview is not occur.it will not give error but result is not display grid not refresh
A trick to call a method of some other forms in c#.Net is to use Application.OpenForms here is a sample code
foreach (Form frm in Application.OpenForms)
{
if (frm is Form2)
{
//Put your code here.
}
}
Edit: changed the answer because I misunderstood what OP was trying to do.
The reason you are unable to manipulate chkSortPlace is because by default all controls that you add to a form are marked as private.
Go to Form1, right click on it, select "View Code" and add the following:
public bool SortPlaceChecked
{
get { return chkSortPlace.Checked; }
set { chkSortPlace.Checked = value; }
}
Then, when you want to change the state of chkSortPlace from your other form, simply use the public property you added above.
After entering in the appropriate text and pressing enter, I want txtbA text in Form1 to display in txtbB in Form 2.
I already have the key events code written, but I can't seem to figure out the rest.
Visual basic seems to be more straightforward with this, and I am new to C#.
This is using WinForms.
I am really only familiar with visual basic's way of handling this:
txtbA.text = My.Forms.Form2.txbB.text
Thank you for any help you can give!
You can use a static variable
In Form1
public static string txtbAtext
{
get { return txtbAtext ; }
set {txtbAtext = value; }
}
OnTextChanged Event
txtAtext = txtA.Text;
In Form2
Form1 f1 = new Form1();
txtbB.Text = Form1.txtAText;
The simpliest and not very elegant way is to do ist like this:
Make a property in the second form, that can hold a Textbox and set the Text property in the TextChange event of the property in Form2
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public TextBox TextBoxWithSameText { get; set; }
private void textBox2_TextChanged(object sender, EventArgs e)
{
if (TextBoxWithSameText != null)
TextBoxWithSameText.Text = textBox2.Text;
}
}
All you have to do now is to set the property to the Form1.textbox1 when the second form (Form2) is created:
Form2 form = new Form2();
form.TextBoxWithSameText = textbox1;
In order to exchange strings between two Forms classes, you can use a third class. Once added to your project, you need to instantiate this new Class1 in your Form1 and Form2:
Class1 class = new Class1;
Make sure Class1 has a constructor. Now, you can create properties in your Class1, with get and set properties:
public string TextA
{
get
{
return textA;
}
set
{
textA = value;
}
}
You can then call these properties from any other class that has instantiated your Class1, like so:
class.TextA = txtbA.Text;
In order to show the text in the other textbox, you need an event to trigger changing the txtbB.Text value. You could use txtbA.ValueChanged event, or a button with a Click event. You should figure out what event is most appropriate for your project.
I hope this helped! Good luck.
Based on your comment:
Right, Form1 will open an instance of Form2 upon pressing the Enter
key. Thanks!
You can setup Form2 to receive the initial value for the TextBox via its Constructor:
public partial class Form2 : Form
{
public Form2(string InitialValue)
{
InitializeComponent();
this.txtbB.Text = InitialValue;
}
}
Then in Form1 you'd do something like:
Form2 f2 = new Form2(this.txtbA.Text);
f2.Show();
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#
I have a program that has two forms in it. Is there a way that I can make a variable in Form1 show up in Form2 without having to make a class or function?
I made form2 by doing:
Form2 form2 = new Form2();
private void button1_Click(object sender, EventArgs e)
{
form2.show();
}
If you open Form2 from Form1, you can do it in several ways:
Create a constructor on Form2 that would accept a value you want to pass.
Create a property on Form2 and set it before showing Form2.
You may opt to pass a reference to Form1 (via a constructor or property) and use it in Form2 to read values of properties of Form1.
You can "share" a object between two forms is through their constructors (dependency injection).
e.g.
Form1Ctor(SharedObject obj)
Form2Ctor(SharedObject obj)
var obj = new SharedObject();
var form1 = new Form1(obj);
var form2 = new Form2(obj);
If the property is only used in a single form you could make it a static
e.g.
public static string s { get; set; }
Well normally I am quite good at figuring and researching problems without guidance however I have come across a snag. I am trying to create an "Event" with C# (which I have not done before) everything I have looked up has nothing to do with what I need.
I am trying to call a class on my main form when form2 is hidden. I found some code which was supposed to check to see if form2 closed - Either I didn't integrate it into my code properly or closing is different to hiding.
So just to clarify I want to run through the program like this:
Form1 runs
Click setting button on Form1 which opens Form2
Form2 opens where settings can be changed
Click the "Ok" button on Form2 (here is where I want Form1 to realise Form2 has been hidden
Hide form one and run a class called Refresh which refreshes button names and URLs
Also,
you can use VisibleChanged event in Form2
private Form2_VisibleChanged(object sender, EventArgs e)
{
if (!this.Visible) { Refresh(); }
}
This might be more elegant...
Open the second form as modal
Form2 form2 = new Form2();
DialogResult result = form2.ShowDialog();
check the result and refresh:
if (result == DialogResult.OK)
Refresh();
What you also need to do in this case is when closing the form set DialogResult of the form, for example if you have an OK button, in the button handler set:
this.DialogResult = DialogResult.OK;
This will automatically close the form as well as I remember correctly.
What you can also do is set DialogResult.Cancel on cancel button if you need that.
If I understand correctly, you want to have a class that stores settings information that both Form1 and Form2 can access. Let's call that class Form1Settings, and implement as:
public static class Form1Settings
{
public static string ButtonText;
public static string Uri;
}
For simplicity, I made this class and its properties static, so both Form1 and Form2 have direct access to it, removing the need for a refresh method.
Form1 would call Form2 in a blocking way, and only update its display if the OK button was clicked.
public partial class Form1 : Form
{
private Form2 form2 = new Form2();
public Form1()
{
InitializeComponent();
}
private void buttonSettings_Click(object sender, EventArgs e)
{
if (form2.ShowDialog() == DialogResult.OK)
{
this.button1.Text = Form1Settings.ButtonText;
this.textBoxUrl.Text = Form1Settings.Uri;
this.Update();
}
}
}
And finally, Form2 will update the settings values with input from the user:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void buttonOK_Click(object sender, EventArgs e)
{
Form1Settings.ButtonText = this.textBoxButton.Text;
Form1Settings.Uri = this.textBoxUri.Text;
this.DialogResult = DialogResult.OK;
this.Hide();
}
}
Why not open Form2 as a modal dialog using ShowDialog()? That way you may return a value if Form2 was closed by OK or by Cancel and act accordingly in Form1 after the call returned.