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!
Related
I have two forms. The main form contains a treeview. After I show the second form, the treeview loses focus. That's okay, but I want to activate the treeview when the second form closes.
Form1.cs
namespace ex
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (Form2 form2 = new Form2(this))
{
form2.StartPosition = FormStartPosition.CenterParent;
form2.ShowDialog();
}
}
internal void example()
{
treeView1.SelectedNode = treeView1.Nodes[1];
}
private void Form1_Load(object sender, EventArgs e)
{
TreeNode node = new TreeNode("aaaa");
treeView1.Nodes.Add(node);
node = new TreeNode("bbbb");
treeView1.Nodes.Add(node);
node = new TreeNode("cccc");
treeView1.Nodes.Add(node);
}
}
}
Form2.cs
namespace ex
{
public partial class Form2 : Form
{
Form1 form1;
public Form2(Form1 form1)
{
InitializeComponent();
this.form1 = form1;
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
form1.example();
//not working
form1.treeView1.Focus();
form1.treeView1.Select();
}
}
}
Form2 really shouldn't get so intimate with Form1. Try turning your code around like this:
private void button1_Click(object sender, EventArgs e)
{
using (Form2 form2 = new Form2(this))
{
if (form2.ShowDialog(this) == DialogResult.OK) {
treeView1.Select();
example();
}
}
}
If Form2 is supposed to supply any information to add to your TreeView control, you would set up a property on Form2 and access it from within this same code block.
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();
}
I am new to c#. I have the following in my project in windows forms:
Form1 with button and textbox.
User control with a buttton.
Form2 with button and textBox.
As shown in the screenshot: In form1, I click "Show User Control1" User Control1 pops up. Then in User Control1 I click Show Form2 form2 pops up.
In Form2 I enter values in textBox and when click "Send to textbox in form1" I want this text to be inserted into the textbox in Form1.
My question is: How can I send text from form2 to textbox in form1 via user control1?
I just need to know some steps to follow or some code if it is possible to achieve this.
Please help me. Thank you
Form1:
public partial class Form1 : Form
{
UserControl1 UC1 = new UserControl1();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Controls.Add(UC1); //add a userControl
UC1.Visible = true;
}
}
User Control1:
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
}
}
Form2:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// I want to send text to form1 when this button is clicked
}
}
You can do it by trigger event and event handler.
In Form2,
public delegate void SendTextF2(string YourStringFromTextBox);
public partial class Form2 : Form
{
public event SendTextF2 UISendTextHandlerF2;
public Form2(TextBox s)
{/*unchange*/}
private void button1_Click(object sender, EventArgs e)
{
if(UISendTextHandlerF2!=null)
UISendTextHandlerF2(textBox1.Text);
}
}
In UserControl1,
//New
public delegate void SendTextUC(string YourStringInTextBox);
public partial class UserControl1 : UserControl
{
//New
public event SendTextUC UISendTextHandlerUC;
public UserControl1(TextBox r)
{
InitializeComponent();
this.r = r;
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2(r);
frm2.Show();
//Add event handler
frm2.UISendTextHandlerF2 += SendText123;
}
//Event Handler for the event trigger in Form2
void SendText123(string YourStringFromTextBox)
{
//Trigger Event
if(UISendTextHandlerUC!=null)
UISendTextHandlerUC(YourStringFromTextBox);
}
}
In Form1,
public partial class Form1 : Form
{
UserControl1 UC1;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (UC1 == null)
{
UC1 = new UserControl1(textBox1);
//Add event handler
UC1.UISendTextHandlerUC += FinallyWeGetTheString;
}
Controls.Add(UC1);
UC1.Visible = true;
}
//New
void FinallyWeGetTheString(string YourStringFromTextBox)
{
textBox1.Text = YouStringFromTextBox;
}
}
Add those line to your code:
public partial class Form1 : Form
{
UserControl1 UC1;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (UC1 == null)
{
UC1 = new UserControl1(textBox1);
}
Controls.Add(UC1);
UC1.Visible = true;
}
}
User Control:
public partial class UserControl1 : UserControl
{
TextBox r;
public UserControl1(TextBox r)
{
InitializeComponent();
this.r = r;
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2(r);
frm2.Show();
}
}
And Form2:
public partial class Form2 : Form
{
TextBox s;
public Form2(TextBox s)
{
InitializeComponent();
this.s = s;
}
private void button1_Click(object sender, EventArgs e)
{
String str = textBox1.Text;
s.Text = str;
}
}
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;
}
This question is the followup to the following question:
C# Text don't display on another form after double clicking an item in listbox
Now I have typed my value in the textbox of form3. How am I going to pass back the value to form1 to show it in the listbox10 after pressing "OK" in form3? Below is my form3 coding but it don't work:
private void button1_Click(object sender, EventArgs e)
{
//This is the coding for "OK" button.
int selectedIndex = listBox10.SelectedIndex;
listBox10.Items.Insert(selectedIndex, textBox1.Text);
}
You can put public property on form3:
public partial class form3 : Form
{
public String SomeName
{
get
{
return textbox1.Text;
}
}
...
private void buttonOK_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.OK;
Close();
}
private void buttonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
}
In form1, where you are open form3, after ShowDialog, you will write:
if (form3.ShowDialog() == DialogResult.OK)
{
int selectedIndex = listBox10.SelectedIndex;
if (selectedIndex == -1) //listbox does not have items
listbox10.Add(form3.SomeValue);
else
listBox10.Items.Insert(selectedIndex, form3.SomeName);
}
do do something like that:
//form1:
public void add(int num)
{
//add num to the list box.
}
now, form3 should get an instance of form1 in the constructor, and save it:
//in form3:
private form form1_i
public form3(form i_form1)
{
.
.
.
form1_i = i_form1;
}
and on button click in form3, call the fumction add in form1.
It should go like this, this is the safest way to do it, in fact if you are working on Windows Mobile this is the only way that won't crash the application. In desktop versions it can crash in debug versions.
public partial class Form1 : Form
{
public string name = "something";
public Form1()
{
InitializeComponent();
}
public delegate void nameChanger(string nme);
public void ChangeName(string nme)
{
this.name = nme;
}
public void SafeNameChange(string nme)
{
this.Invoke(new nameChanger(ChangeName), new object[] { nme });
}
private void button2_Click(object sender, EventArgs e)
{
Form3 f3 = new Form3(this);
f3.Show();
}
}
public partial class Form2 : Form
{
Form1 ff;
public Form2(Form1 firstForm)
{
InitializeComponent();
ff = firstForm;
}
private void button2_Click(object sender, EventArgs e)
{
ff.SafeNameChange("something different from the Form1");
this.Close();
}
}