Firstly, my English is bad, I'm sorry.
I want textbox (in Form2.cs) text to displaying MainForm.cs
When I apply the following codes, displaying blank message.
MainForm.cs
private void btnFilitre_ItemClick(object sender,DevExpress.XtraBars.ItemClickEventArgs e)
{
...
Form2 f2 = new Form2();
f2.Show();
}
private void workingFunction()
{
CommClass com = new CommClass();
MessageBox.Show(comm.Sorgu );
}
Form2.cs
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
CommClass comm = new CommClass();
comm.Sorgu = textBox1.Text;
f1.workingFunction();
Hide();
}
CommClass.cs
public string Sorgu { get; set; }
What is the problem?
You need to pass the argument in. In Form1 make this change:
public void workingFunction(CommClass comm)
{
MessageBox.Show(comm.Sorgu );
}
And in Form2, you will need to keep track of your Form1 reference, instead of creating a new one, and then pass in your CommClass object:
private void button1_Click(object sender, EventArgs e)
{
CommClass comm = new CommClass();
comm.Sorgu = textBox1.Text;
f1.workingFunction(comm);
Hide();
}
Related
I have two form, FolderBrowserDialog in Form1 and I want to use SelectedPath in Form 2.
I don't know why but when I click on Form1 Button to show Form2 ,This Error will be appear :
Invalid URI: The URI is empty
I set Form Control Modifiers: Public and FolderBrowseDialog Seletedpath not be transferred to Form2
Form 1 :
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog MyFolderBrowse = new FolderBrowserDialog();
if(MyFolderBrowse.ShowDialog()==DialogResult.OK)
{
txtpath.Text = MyFolderBrowse.SelectedPath;
}
}
private void Showfrm2Btn_Click(object sender, EventArgs e)
{
Form2 Frm2 = new Form2();
Frm2.ShowDialog();
}
Form 2 :
private void Form2_Load(object sender, EventArgs e)
{
Form1 Frm1 = new Form1();
webBrowser1.Url = new Uri(Frm1.txtpath.Text);
}
any solution...?
Welcome to StackOverflow!
The problem is, if I understand it correctly from the code you post it, is that you create a new instance of Form2 and then in the Loadevent of the Form2 you create a new instance of Form1 and the information you need is the instance of Form1 you already have.
You already have an instance of Form1. I would do it by two one of the 2 options:
Create a public property in Form2 and assign it when creating the Form2 instance
public class Form2: Form
{
//{...}
public string SelectedPath { get; set;}
//{...}
private void Form2_Load(object sender, EventArgs e)
{
webBrowser1.Url = new Uri(this.SelectedPath);
}
}
private void Showfrm2Btn_Click(object sender, EventArgs e)
{
Form2 Frm2 = new Form2();
Frm2.SelectedPath = txtpath.Text;
Frm2.ShowDialog();
}
Create an argument in the Form2 constructor and pass the selected path from Form1
public class Form2: Form
{
//{...}
private string _selectedPath;
public Form2(string selectedPath)
{
_selectedPath = selectedPath;
}
//{...}
private void Form2_Load(object sender, EventArgs e)
{
webBrowser1.Url = new Uri(_selectedPath);
}
}
private void Showfrm2Btn_Click(object sender, EventArgs e)
{
Form2 Frm2 = new Form2(txtpath.Text);
Frm2.ShowDialog();
}
My problem is very simple: i have a combobox in form1, i have a button that open form2 to write into a textbox the new item to add. Here my code:
Form1:
public static string new_item;
private void btn1_Click(object sender, EventArgs e)
{
Form2 f2= new Form2();
f2.ShowDialog();
}
Form2:
private void btn1_Click(object sender, EventArgs e)
{
Form1.new_item = textBox1.Text;
combobox.Items.Add(new_item);
this.Close();
}
But the new item is not added to my comobobox.
I tried to refresh th combobox but i have the same result.
Thank you.
You need to add the item to your ComboBox after closing Form2:
public static string new_item;
private void btn1_Click(object sender, EventArgs e)
{
Form2 f2= new Form2();
f2.ShowDialog();
comboBox1.Items.Add(new_item); //this is missing in your code
}
But a better way would be creating a public property in Form2 to pass the string back:
public string Value { get; set; }
private void btn1_Click(object sender, EventArgs e)
{
this.Value = textBox1.Text; //pass the TextBox value to the property
this.DialogResult = DialogResult.OK; // Cancel would mean you closed/canceled the
// form without pressing OK-Button (btn1)
this.Close();
}
Than in Form1 you can access the property and add the new item:
private void btn1_Click(object sender, EventArgs e)
{
Form2 f2= new Form2();
if(f2.ShowDialog() == DialogResult.OK) //check the result
{
comboBox1.Items.Add(f2.Value);//Add the new item
}
}
Assuming combo's name is combobox.
Form1:
private void btn1_Click(object sender, EventArgs e)
{
Form2 f2= new Form2();
if (f2.ShowDialog() == DialogResult.OK)
combobox.Items.Add(f2.ItemValue);
}
Form2:
public string ItemValue {get {return textBox1.Text;} };
private void btn1_Click(object sender, EventArgs e)
{
Form1.new_item = textBox1.Text;
this.DialogResult = DialogResult.OK;
}
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();
I understand that this thread can send strings from one form to another. But if you look at the method, it has to create a new form where the strings will then be retrieved. Is there a way to pass the string to the existing form?
For example:
public class Form2
{
...
public string MyProperty { get; set; }
private void Form2_Load(object sender, EventArgs e)
{
MessageBox.Show(this.MyProperty);
}
}
From Form1:
public void button1_Click(object sender, EventArgs e)
{
string departmentName = "IT";
Form2 frm2 = new Form2();
frm2.MyProperty = departmentName;
frm2.Show();
this.Hide();
}
When I click the button1, the new form in which the string are sent will be opened. What if frm2 is already opened and I want the string to be passed to that form instead of a new one?
To do what you want, you'll have to save a reference to your form and then simply check whether it's existing already:
private Form2 myForm;
// Then, in your button code:
if (myForm == null)
myForm = new Form2();
myForm.MyProperty = departmentName;
myForm.Show();
You can create a property on the Form1 class and use that as the reference to your Form2:
public class Form1 : Form
{
private Form2 mForm2 { get; set; }
public void button1_Click(object sender, EventArgs e)
{
string departmentName = "IT";
if (mForm2 == null)
mForm2 = new Form2();
mForm2.MyProperty = departmentName;
frm2.Show();
this.Hide();
}
}
i want to have 2 forms in which the first form has a button that will load up form2 in a dialog form. form2 will show a listview displaying the data of a student. now i need to extract the 1st index of the selected row. once i double click on the row, form2 would close and pass the data into a textbox in form1.
i have used the code below which closes my form1 and creates a new instance of form1 in form2.
from form2:
private void listView1_DoubleClick(object sender, EventArgs e)
{
var cl = listView1.Items[listView1.FocusedItem.Index].SubItems[0].Text;
Form1 wa= new Form1();
wa.loadid(cl);
wa.Show();
this.Close();
}
from form1:
private void btnReq_Click(object sender, EventArgs e)
{
Form2 f2= new Form2();
f2.Show();
this.Close();
}
public void loadid(String ms)
{
String newstring = ms;
studentid.Text = newstring;
}
I suggest using a Dialog, it makes it very easy:
This is Form1. You instantiate and open f2 as Dialog and then wait for its result.OK
private void Button1_Click(System.Object sender, System.EventArgs e)
{
var f2 = new Form2();
if (f2.ShowDialog() == DialogResult.OK) {
studentId.Text = f2.SelectedStudentId;
} else {
studentId.Text = "Select a Student!!!!";
}
}
This in Form2, where you have created your listview and a public property to expose:
public string SelectedStudentId { get; set; }
private void listView1_DoubleClick(object sender, EventArgs e)
{
var cl = listView1.Items[listView1.FocusedItem.Index].SubItems[0].Text;
SelectedStudentId = cl;
DialogResult = DialogResult.OK; //will close this dialog (form2)
}
This should work for you
In Form1 create a public variable like this:
public partial class Form1: Form
{
//New variable
public static string StudentIDVal;
Then, change the button click on Form1 to be:
private void btnReq_Click(object sender, EventArgs e)
{
Form2 f2= new Form2();
f2.ShowDialog();
studentid.Text = StudentIDVal;
}
Then, on Form2 on the item click you can simply have:
private void listView1_DoubleClick(object sender, EventArgs e)
{
var cl = listView1.Items[listView1.FocusedItem.Index].SubItems[0].Text;
Form1.StudentIDVal = cl.ToString();
this.Close();
}