display comboBox value of fom1 to another form in Textbox - c#

I have three forms Form1, Form2 and Form3. I have a comboBox in form1 which I want it to be displayed in Form 3.
Question: So what I want is that when I select the value of comboBox in form1 and click the button, it should open form2 and in form2 when I click the button again, It should show the comboBox selected value in a textbox in Form3.
private void button1_Click(object sender, EventArgs e) //form1
{
Form2 f2 = new Form2(); // don't know what to put in this parameter
f2.ShowDialog();
}
ComboBox CB1;
public Form2(ComboBox cb1)
{
InitializeComponent();
CB1= cb1;
cb1.Text.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
Form3 f3 = new Form3(CB1);
f3.ShowDialog();
}
public partial class Form3 : Form
{
ComboBox Combo;
public Form3(ComboBox combo)
{
InitializeComponent();
Combo = combo;
Combo.Text = combo.ToString();
}

SOLUTION1:
One simple solution is defining a static property in your first window for the selected item,
public static string SelectedItem;
public MainWindow()
{
InitializeComponent();
}
And in Window3, you can assign the value like this,
public Window3()
{
InitializeComponent();
txtresult.Text = MainWindow.SelectedItem;
}
SOLUTION2:
If you have to pass the value through the constructor just pass the selectedItem as a String through each window, something like this,
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(combobox.SelectedItem.ToString());
f2.ShowDialog();
}
string result = null;
public Form2(String selected)
{
InitializeComponent();
result = selected;
}
In form 3,
public Form3(string result)
{
InitializeComponent();
Combo.Text = result();
}

In the event from form1 to form2
Form2 obj = new Form2(ComboBox1.SelectedValue);
this.Hide();
Form2.Show();
In form2:
string someName="";
public form2(string name)
{
InitializeComponent();
someName=name;
}

Related

Change value textbox from another form and form position is Showing C#

I want to change the value of textbox1 in form2 from another form (form1). If the position of the form2 is closed the code can be executed (there is no problem), but if the position of the form2 has been opened then the value textbox does not change when the button on form1 is clicked.
Form2
public string Fullname
{
get
{
return textbox1.Text;
}
set
{
textbox1.Text = value;
}
}
Form1
Form2 frm = new Form2();
frm.Fullname = textBox1.Text;
frm.Show();
Form2 frm = new Form2();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
frm.Show();
}
private void button2_Click(object sender, EventArgs e)
{
frm.textBox1.Text = "Changed";
}
this Code worked just fine for me . or you can implement INotifyPropertyChanged interface in your property's class
it should detect the changes

How to show existing form from another form?

i have Form1 and Form2, in Form1 i have some textboxes like username,password and more... and a textbox "region". when user hits "region"(Form1.hide()), then Form2 opens witch has 5 labels with names of regions on it.
so how can i make that when user clicks on a name of region in Form2, Form1 will have the region on it? and keep all the data that the user entered before the region click.
something like this(in form 2):
private void center_Click(object sender, EventArgs e)
{
this.Hide();
Form1.region = "center";
Form1.show();
}
Try creating an instance of Form2 and calling ShowDialog() method to show it
Form2 form2= new Form2();
form2.ShowDialog();
When creating Form2, just pass Form1 as a parameter and edit the textbox value in your click event.
On form1:
private void click_on_region(object sender, EventArgs e)
{
this.Hide();
Form2 frm2 = new Form2(this);
Form2.Show();
}
on form2:
Form1 _frm1;
public Form_Main(Form1 frm)
{
InitializeComponent();
_frm1 = frm;
}
private void center_Click(object sender, EventArgs e)
{
this.Hide();
_frm1.textBox_region.Text = whateverobject.Text;
_frm1.Show();
}
This might not be the prettiest but It'll do for starters.
Form 1 Code
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 objForm2 = new Form2();
objForm2.ShowDialog();
textBox_Region.Text = objForm2.RegionName;
}
}
And Form 2 Code
public partial class Form2 : Form
{
public string RegionName
{
get
{
return textBox_Form2_Region.Text.ToString();
}
set { }
}
public Form2()
{
InitializeComponent();
}
}
On Form 2
private void center_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
frm1.textbox_region = whateverobject.text;
this.hide();
frm1.show();
}
this will bring up a form1 with the region text on it.

C# - Setting a Value in a Button then passing it into a ListView

I have two forms, form1 and form2.
In form1, there is are two buttons, button1 and button2.
In form2, there is a listview, ListView1.
button1 should hold a string value called "Vanilla".
When button2 is pressed it opens form2.
On form2, in listview1 it should show "Vanilla" in the first column.
Form1
public partial class form1 : Form
{
public static string buttonValue = "";
public form1()
{
InitializeComponent();
}
public void button1_Click(object sender, EventArgs e)
{
buttonValue = "Vanilla";
}
public void button2_Click(object sender, EventArgs e)
{
form2 form2 = new form2();
form2.Show();
this.Hide();
}
Form2
public partial class form2 : Form
{
public form2()
{
InitializeComponent();
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
You can design the second form as bellow:
public partial class form2 : Form
{
public form2()
{
InitializeComponent();
}
private string _passedValue = "";
public form2(string passedValue)
{
InitializeComponent();
_passedValue = passedValue;
listView1.Items.Add(_passedValue);
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
You can pass the value stored in the first button using the bellow code.
public void button2_Click(object sender, EventArgs e)
{
form2 form2 = new form2(buttonValue);
form2.Show();
this.Hide();
}

How to send date from a listbox to textbox of another form

I have a listbox with multiple items in a form. I need to select listbox item and click a button and then selected item should appear in another form's textbox. How can I do this?
I use this code to put items to my form 1 list box after I click a button.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection conn2 = new SqlConnection(
"<path>\\Database1.mdf\";Integrated Security=True");
conn2.Open();
ArrayList al = new ArrayList();
SqlCommand commandtwo = new SqlCommand(
"SELECT name FROM [dbo].[Table2]", conn2);
SqlDataReader dr2 = commandtwo.ExecuteReader();
while (dr2.Read())
{
al.Add(dr2[0].ToString());
}
try
{
listBox1.Items.Clear();
listBox1.Items.AddRange(al.ToArray());
conn2.Close();
}
catch (Exception)
{}
}
public void button2_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();
}
}
I need to select item in this listbox and click Update button in this form, it will open another form called Form2. I need to get selected item from previous form (Form1) and show it in a textbox in another form (Form2).
This is the code of my second form (Form2)
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
Form1 f1 = new Form1();
textBox1.Text = f1.listBox1.SelectedItem.ToString();
}
}
There's many ways to do it. One is to have public property on first form that you access from the other form. But as said, this is only one approach (you could use events, pass value in overloaded constructor, etc.)
// Form1
private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
SelectedItem = (sender as ComboBox).SelectedItem.ToString();
}
private void button_Click(object sender, EventArgs e)
{
var frm2 = new Form2() { Owner = this };
frm2.Show();
}
public string SelectedItem { get; private set; }
And then...
// Form2
protected override void OnActivated(EventArgs e)
{
base.OnActivated(e);
textBox1.Text = (Owner as Form1).SelectedItem;
}
You have to pass the value to the Form2 in the constructor.
public Form2(string value)
{
InitializeComponent();
textBox1.Text = value;
}
In your Form1 just call it like this:
// get the item from listbox into variable value for example
Form2 = new Form2(value);
f2.Show();

ListBox SelectedItem edit from another form

I tried below code. First I select a listbox item and then I click the edit button for text name change.
Form1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnOpenForm2_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(this);
f2.ShowDialog();
}
public string ListBoxValue
{
get { return listBox1.SelectedItem.ToString(); }
}
}
Form2
public partial class Form2 : Form
{
Form1 f1;
public Form2(Form1 f1)
{
this.f1 = f1;
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
textBox1.Text = this.f1.ListBoxValue;
}
}
Above code works well but after I open form2 for editng the text, it's not updated the same text in listbox selected item. It's added as a new item in listbox.
You have nowhere code where you actually change the value of the selected item from the listbox. You have to move the changes back to Form1 and update the selected item of the listbox. You do this through a method SetSelectedItemValue for example:
Code of Form1:
private Form2 _form2;
private void button1_Click(object sender, EventArgs e)
{
_form2 = new Form2(this);
_form2.Show();
}
public string ItemValue
{
get { return listBox1.SelectedItem.ToString(); }
}
public void SetSelectedItemValue(string value)
{
listBox1.Items[listBox1.SelectedIndex] = value;
}
Code of Form2:
public Form2(Form1 form1)
{
_form1 = form1;
InitializeComponent();
}
private readonly Form1 _form1;
private void Form2_Load(object sender, EventArgs e)
{
textBox1.Text = this._form1.ItemValue;
}
private void button1_Click(object sender, EventArgs e)
{
_form1.SetSelectedItemValue(textBox1.Text);
Close();
}
This code is for demo only, just to show how it works. You'll have to build in validation of user input in the textbox and whether an item from the listbox is selected. Hope this helps!

Categories

Resources