Activate treeview again - c#

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.

Related

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 do I pass text from child to main form via a user control?

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;
}
}

Application Windows Form C# use menu (User control)

I have an application with 2 Forms, for those forms I have create a Menu which I depose on the two forms.
There is only a menuStrip item on the menu, I just want when I click on "test1" to redirect to Form1 and when I click to "test2" I want to redirect to Form2.
But if test1 is already open/display I don't want to show him again and the same for test2.
My code in my Menu :
public partial class Menu : UserControl
{
public Menu()
{
InitializeComponent();
}
private void test1ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
Form2 f2 = new Form2();
f2.Hide();
f1.Hide();
f1.ShowDialog();
}
private void test2ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
Form2 f2 = new Form2();
f1.Hide();
f2.Hide();
f2.ShowDialog();
}
}
My Form1 :
My Form2 :
I just want the same result like my Buttons in Form1 and Form2 :
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
Form1 f1 = new Form1();
f1.ShowDialog();
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
Form2 f2 = new Form2();
f2.ShowDialog();
}
}
I thought that the property Visible for forms could help me but not...
The problem is when I click on my buttons it's open a new window but when my form is already open I don't want to open it again.
Thanks for your reply, I hope that I am clear sorry for my english in advance.
You are currently creating a new form each time the click handler code is executed.
Here is one way, but its nasty and I wouldn't really recommend it. I've assumed that form1 is the entry to your application and that its also the exit of the application. This solution uses a singleton to hold the f1/f2 instances.
public static class Global
{
static Global()
{
f2 = new Form2();
}
public static Form f1;
public static Form f2;
}
Your menu altered:
public partial class Menu : UserControl
{
public Menu()
{
InitializeComponent();
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
Global.f2.Hide();
Global.f1.Hide();
Global.f1.Show();
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
Global.f1.Hide();
Global.f2.Hide();
Global.f2.Show();
}
public void SetForm1(Form form)
{
Global.f1 = form;
}
public void SetForm2(Form form)
{
Global.f2 = form;
}
}
And the forms:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Global.f1 = this;
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
Global.f2.ShowDialog();
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
Global.f1.Show();
}
}
Hope this helps.
Your logic is wrong.
It seems to me that you want to display different content depending on the user selection on that 'menuStrip'. You need to look at dynamic controls loading not different forms loading.
You can have just a MainForm with that 'menuStrip' and a Panel. Define some User Controls and dynamically add them to that panel based on the user selection.
Snippet
public MainForm : Form
{
public MainForm()
{
// code
}
public void MenuStrip_OptionSelected(object sender, EventArgs e)
{
Panel1.Controls.Clear();
switch(MenuStrip.SelectedValue)
{
case "UserControl1" : Panel1.Controls.Add(new UserControl1()); break;
...
}
}
}
public UserControl1 : UserControl
{
// code
}

Add combobox item from another form won't work

I want to add an item to my combobox in form1 from form2. But if I press the button, form2 closes but the item is not added in form1. I hope you can help me! I found no solution for this problem in the internet.
Form 2:
public void button5_Click(object sender, EventArgs e)
{
Form1 main = new Form1();
main.AddItem("Item");
this.Close();
}
Form 1:
public void AddItem(object item)
{
comboBox1.Items.Add(item);
}
First of all: You have to change comboBox access modifier to public. Then:
Form 2:
public void button5_Click(object sender, EventArgs e)
{
Form1 main = new Form1();
main.AddItem("Item");
this.Hide(); // This will hide Form2 ("this." is redundant)
main.ShowDialog(); // This will show Form1
}
Reference the name property of your 'Form1' don't create a new instance. Then reference that forms combobox control.
Your original code creates a NEW instance. Meaning its creating a new version of Form1 and not accessing the original!
The code below should help
public void button5_Click(object sender, EventArgs e)
{
Form1 myForm = whateverYourFormsNamePropertyIs;
myForm.Combobox.AddItem("Item");
this.Close();
}
In form2...
Form1 f;
public Form2(Form1 parent)
{
InitializeComponent();
f = parent;
}
private void Add_Click(object sender, EventArgs e)
{
f.comboBox1.Items.Add("item");
}
In form1
public void AddItem(object item)
{
comboBox1.Items.Add(item);
Form2 f = new Form2(this);
f.Show();
}
orginal form
private void FormPeople_Load(object sender, EventArgs e)
{
populateComboBoxTitles();
}
public void populateComboBoxTitles()
{
comboBox2.Items.Clear();
comboBox2.Items.Add("mr");
comboBox2.Items.Add("miss");
}
private void button5_Click(object sender, EventArgs e)
{
FormAddTitle formAddTitle = new FormAddTitle(this);
formAddTitle.Show();
}
secondary form
FormPeople formPeople;
public FormAddTitle(FormPeople formPeople)
{
InitializeComponent();
this.formPeople = formPeople;
}
private void button1_Click(object sender, EventArgs e)
{
if (formPeople != null)
formPeople.populateComboBoxTitles();
}

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