I want to pass a parameter to the textbox. I have the following code and it is passing the parameter but not the way I want.
My main form in already open and I want to pass the parameter from my search form. when I do with the code below it opens mt 1 more main form and the parameter is shown in there. I want to by able to show in the opened main form.
When I erase frmMain.Show(); nothing happens.
Main frmMain = new Main();
artikal = "TEST TEST";
frmMain.ed_artiakal.Text = artikal;
frmMain.Show();
any suggestions?
You have many variants to solve your problem.
Option 1
Define and use custom event.
Search form code:
public event EventHandler ArtikalTextChanged;
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (ArtikalTextChanged != null)
ArtikalTextChanged(this, EventArgs.Empty);
}
Main form code:
private void button1_Click(object sender, EventArgs e)
{
Search search = new Search();
search.ArtikalTextChanged += OnArtikalTextChanged;
search.Show();
}
private void OnArtikalTextChanged(object sender, EventArgs e)
{
this.ed_artiakal.Text = (sender as Search).textBox1.Text;
}
Don't forget to make textBox1 of Search form public.
Option 2
Get instance of your main form in search form:
Search form code:
private void textBox1_TextChanged(object sender, EventArgs e)
{
var mainForm = Application.OpenForms.OfType<Main>().FirstOrDefault();
mainForm.ed_artiakal.Text = textBox1.Text;
}
Main form code:
private void button1_Click(object sender, EventArgs e)
{
Search search = new Search();
search.Show();
}
Don't forget to make ed_artiakal control public in your Main form.
Option 3
Share data between forms (recommend)
But if you application is large and you want to make it scaleable and flexible I recommend you to use data-binding technique to share data between forms without coupling them. You can read more at articles: http://msdn.microsoft.com/en-us/library/h974h4y2(v=vs.90).aspx
I have solved my problem in the following way.
On my Search Form I created a public string and when I showed the form I referenced to that string in my case GetItemCode.
The key here was to use ShowDialog() and not to use Show().
SEARCH FORM
Search frmSearch = new Search();
frmSearch.ShowDialog();
ed_artiakal.Text = frmSearch.GetItemCode;
MAIN FORM
public string GetItemCode
{
get { return Artikal; }
}
Now when I close the search form the value is shown in the TextBox on my main form.
Thanks for your answers and comments!
Related
I am making a program for a friend and I need it to be were when she types in a number corresponding to the form that opens when she hits 'go' that form will open. I have that part done. The issue I have is when the new form opens I have a series of text boxes that she needs to input data into like '1 can of fresh beans' and some other stuff in some other text boxes on that form. Now when she is done typing in all the required things she will hit a submit button that will then format the code accordingly to the way I have it set to like this
richTextBox1.Text += "This is some text that I type before" + AmntItemsTxtBox + " and this is some other stuff";
So that is what I kind of want to happen. Now I know I may have confused some people but what I ran into as an issue is how do I take what she typed in form 2 and send it to the richtextbox in form 1 when she hits the button so she can copy and paste it into something else later on. I know my code seems a bit "new" but I am just starting out with C# and wanting to learn more. Any help is appreciated.
I have done it in my sample project. It may help you.
Form 1:
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.txtmessagechanged += new EventHandler(txt_messagechanged);
frm2.ShowDialog(this);
}
private void txt_messagechanged(object sender, EventArgs e)
{
txtMessage.Text = (string)sender;
}
Form 2 :
public EventHandler txtmessagechanged { get; set; }
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
if (textBox1.Text != string.Empty)
{
string a = textBox1.Text;
if (txtmessagechanged != null)
txtmessagechanged(a, null);
}
else
{
MessageBox.Show("Fill some data in textbox");
e.Cancel = true;
}
Image of form 1 (on button click event it open form 2):
Input in form 2 (put some value in text box) :
Get text in form 1 on form 2 close event.
You can modify it according to your needs.
Form 1
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2(textBox1.Text);
frm2.Show();
}
From 2
public Form2(string msg)
{
InitializeComponent();
textBox1.Text = msg;
}
That is how you pass data (or at least one way of doing it). Then you can do whatever you want with it and format it however you like.
//in main form there is a public listbox "lstMain"
//in addTask form
Main main = new Main();
private void btnTaskAdd_Click(object sender, EventArgs e)
{
main.lstMain.Items.Add(lstAddTask.SelectedItem.ToString());
this.Close();
}
this code doesnt pass the selected item in lstAddTask to lstMain in the main form
any help would be great thanks :-)
Your code isn't working because you don't have a reference to the first form in the second form.
You could use Hadi's answer, or modify your second form to have a property where you can store a reference to the first form.
For example,
Main MainForm {get;set;}
private void btnTaskAdd_Click(object sender, EventArgs e)
{
main.lstMain.Items.Add(lstAddTask.SelectedItem.ToString());
this.Close();
}
And then in your main form
var form = new addTaskForm();
form.MainForm = this;
form.ShowDialog()
//etc.
you should use the following:
// this function should be written in the main form
private void btnTaskAdd_Click(object sender, EventArgs e)
{
var form=new addTaskForm();
if(form.ShowDialog()==DialogResult.Ok)
{
// in the form addTaskForm you add a string property called SelectedItem,
// and on selection change in the lstAddTask then you set the SelectedItem,
// the lstAddTask_SelectedIndexChanged will be written in addTaskForm
lstMain.Items.Add(form.SelectedItem);
this.Close();
}
}
hope this will help you
regards
I have a Winform (C#), how can i receive a value from another form without form load event. Is any possibility to pass the value?
I have a Patient Entry Form. i have one search button link to get a Patient ID. Search button have a separate form. it gets open and i choose the ID from the Search Form. how can i send the selected id value to the patient entry form with out form load event.
Add a public property to Patient Entry Form
public string ID { get; set; }
Within the button click event of the first Form set the value of this property and then open the form. Then you can access the ID directly within Patient Entry Form.
private void button1_Click(object sender, EventArgs e)
{
PatientEntryForm entryForm = new PatientEntryForm();
entryForm.ID = "selected ID";
entryForm.ShowDialog();
}
Forms can be instantiated without being shown. The load event is a form being shown for the first time. So if you have functions/getters/public variables, you'd be able to access them from another form, provided you aren't cross-threading.
there are 2 ways to do that.
1)
Keep in the caller form a reference to the other form like:
private void button1_Click(object sender, EventArgs e)
{ var f = new Form2();
f.id = "value you want";
f.Show();
}
2)
when you create the form pass in the costructor a reference to the current form
private void button1_Click(object sender, EventArgs e)
{ var f = new Form2(this);
f.Show();
}
now you can access the "pather" form in the "children form"
3) when you call show() method
private void button1_Click(object sender, EventArgs e)
{ var f = new Form2();
f.Show(this);
}
now the current form is the owner of the Form2 instance.
you can access in the Form2 calling
var owner = this.Owner as Form1;
}
I have a web browser control in a child form and it captures some data from the displayed web page. I need to use this data in the browser form to be passed to the parent form, but without having to start a new instance of it as it's already open.
The parent form needs to recieve this data from the browser and update some textboxes with the variables set from parsing the page.
I have this in the parent form:
private void browserToolStripMenuItem_Click(object sender, EventArgs e)
{
System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(RunBrowser));
t.SetApartmentState(ApartmentState.STA);
t.Start();
}
public static void RunBrowser()
{
Application.Run(new BrowserForm());
}
I have tried many things in the child form, but I cannot get it to work at all. The best I can get is to pass a variable to the parent and display it via a MessageBox, but it refuses to update the TextBox at all.
BTW I have been trying to solve this now for nearly 12 hours straight, that is the only reason I am asking here.
I solved it finally, but not in an ideal way.
In the parent I open it like this:
private void BrowserToolStripButton_Click(object sender, EventArgs e)
{
using (BrowserForm form = new BrowserForm())
{
form.ShowDialog(this);
}
}
I have this method in the Parent also:
public void SendStringsToParent(string s, string s2, string s3)
{
textBox.Text = s;
textBox2.Text = s2;
textBox3.Text = s3;
}
Then in the Child (Browser) form I have this:
private void Button1_Click(object sender, EventArgs e)
{
string stringToSend = "sending these";
string stringToSend2 = "strings to the";
string stringToSend3 = "parent form";
MainForm parent = (MainForm)this.Owner;
parent.SendStringsToParent(stringToSend, stringToSend2, stringToSend3);
}
This is working, although I have had to work around the fact that it is a modal form. If there is any way to do this this while still having full control over both forms, I would love to hear from someone.
Please check For this Method..
But if you are passing private data this not will be helpful.
In Your Browser page:
protected void Button1_Click(object sender, EventArgs e)
{
string modulename = "Agile Software Development ";
string url;
url = "page2.aspx?module=" +modulename;
Response.Redirect(url);
}
In Your Parent page
string RetrievedValue;protected void Page_Load(object sender, EventArgs e)
{
this.TextBox1.Text = Request.QueryString["module"];
// RetrievedValue = this.TextBox1.Text;
}
in the left picture, there is search button. when click, it will popup the second form (right picture).
when entering the keyword on search form (form2), the data will appear at the form1. how to pass the word enter by user in form2 to form1?
this is the code in form1.
private void button5_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.ShowDialog(); //open form2-search form
//kene get data input dr form2
XmlDocument xml = new XmlDocument();
xml.Load("C:\\Users\\HDAdmin\\Documents\\SliceEngine\\SliceEngine\\bin\\Debug\\saya.xml");
XmlNodeList xnList = xml.SelectNodes("/Patient/Patient/Name");
foreach (XmlNode xn in xnList)
{
string name = xn.InnerText;
listBox21.Items.Add(name);
}
}
this is the code in form2.
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("Please enter keyword to search");
}
else
{
//send data input to form1.
}
can anyone help me with this? thank you
===EDIT===
i am referring to this link to solve this problem. There are two ways and i am using the second method and it works perfectly. I am crying out loud for this. thank you to the blogger owner.
i also found that, in order to view the data, i need to view it in TextBox and not ListBox. what i did before is im trying to view this in ListBox. i am not sure why but that is it. anyway, this problem SOLVE! thanks again for those who help me with this topic. i am grateful.
You can, for example, simply use a property:
Form2:
public string UserText { get; set;}
...
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("Please enter keyword to search");
}
else
{
UserText = textBox1.Text; // set the Text
}
Form1:
private void button5_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.ShowDialog(); //open form2-search form
string text = from2.UserText; get the Text
....
Create a property (or properties) on Form2 exposing the values of the controls you want. So if you want the search term do it like:
public string SearchTerm
{
get { return this.textBox1.Text; }
}
Also, on a side-note; don't forget to check if the user actually did press search.
The way you have it now, when a user closes the form with the x it will also search. That doesn't seem logical to the user.
Make the button on your Form2 ModalResult.OK and do it like this:
if (form2.ShowDialog() == ModalResult.OK)
{
// Do your thing
}
You can sign for Form2 button clicked event in Form1 class:
// Form1's button5 clicked event handler.
private void OnButton5Clicked(object sender, EventArgs e)
{
form2.button1.click += this.OnSearchButtonClicked;
}
// form2.button1 clicked event handler.
// this method will rise when form2.button1 clicked.
private void OnSearchButtonClicked(object sender, EventArgs e)
{
if (form2.textBox1.Text == "")
{
MessageBox.Show("Please enter keyword to search");
}
else
{
// unsign from event!!!
form2.button1.click -= this.OnSearchButtonClicked;
// here you can use form2.textBox1.text
string searchRequest = form2.textBox1.Text;
}
// your business-logic...
}
However, answers proposed by #BigYellowCactus and #Gerald Versluis are simpler and more preferable.
By the way, do not use default button names. It'll be hard to understand their purposes in future. You can rename form1.button5 in form1.showFindWindowButton and form2.button1 in form2.startSearchButton.
I used a simple solution in my project and few days ago.
I recommend using inner-class form.
create a normal form to get the seach string (just like you did), for example fSearch, then use ShowModal to display it instead of Show().
here is an example (psuedo c#):
class MainClass : form
{
String search = String.Empty;
private void button5_Click(object sender, EventArgs e)
{
SearchString s = new SearchString();
s.ShowModal();
search = s.search;
}
.
.
class SearchString : Form
{
public String strString = String.Empty;
private void btnOK_Click(object sender, EventArgs e)
{
this.strString = text1.text;
this.close();
}
}
}