Passing data from child form to parent form - c#

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

Related

ASP.NET and C#: create button that sends keyword to website with search bar

I am new to ASP.NET and C#. In a Web App, I know I can create a button that opens a webpage:
private void button1_Click(object sender, EventArgs e)
{
//Launch browser
System.Diagnostics.Process.Start("https://www.nhl.com/jets");
}
But if the landing page has a search bar, how can I send a keyword to that search bar upon clicking the button? For clarity, say that my code-behind declares that keyword like this:
string keyword = Keyword.Text. How can I make sure that this keyword is automatically sent to the search bar, so that users can see the results without having to type the keyword?
Try this, you should use webBrowser automation. i adapted this methods i wrote before to your website, just add your little adjustments:
... string keyboard = Keyword.Text
public String GetKeyboardValueForSearch() {
return keyboard;
}
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.DocumentCompleted += new
WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
webBrowser1.Navigate("https://www.nhl.com/jets");
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HtmlElement search = webBrowser1.Document.GetElementById("top-nav__search-
autocomplete__input");
if(search != null)
{
search.SetAttribute("value", GetKeyboardValueForSearch());
foreach(HtmlElement ele in search.Parent.Children)
{
if (ele.TagName.ToLower() == "input" && ele.Name.ToLower() == "go")
{
ele.InvokeMember("click");
break;
}
}
}
}

Why wont my method append this richTextBox

Im currently trying to make an instant messaging application.
There is a client and a server. the server works perfectly but for some reason when I call a certain function to update the UI the TextBox doesn't get text added to it.
Below is an example of my code - The Update UI is called from a different form in my applicaiton:
public ChatWindow()
{
InitializeComponent();
Thread timerThread = new Thread(Main.ReceiveLoop);
timerThread.Start();
}
private void txtChatLog_TextChanged(object sender, EventArgs e)
{
}
private void btnSendMessage_Click(object sender, EventArgs e)
{
string clientReply = txtReply.Text;
string Message = "ClientMsg§" + clientReply;
var time = DateTime.Now;
txtChatLog.AppendText($"{time} client: {clientReply}");
txtChatLog.AppendText(Environment.NewLine);
Main main = new Main();
main.ChatResponse(Message);
txtReply.Text = "";
}
public void UpdateChatLog(string message)
{
var time = DateTime.Now;
string newMessage = message.Split('$')[1];
string messageToDisplay = $"{time} Server: {newMessage}";
MessageBox.Show(messageToDisplay);
txtChatLog.AppendText(messageToDisplay);
txtChatLog.AppendText(Environment.NewLine);
}
private void ChatWindow_Load(object sender, EventArgs e)
{
}
The client is defiantly receiving the message from the server as I checked with a messagebox.show();
Also when the send message button is pressed the rich textbox is updated. But for some reason it just wont update through the UpdateChatLog method.
Any Help would be really appreciated.
Thankyou advance!
From code which you have pasted, you don`t call UpdateChatLog method.
Try to add UpdateChatLog(message); to btnSendMessage_Click method.
Try refreshing textbox : txtChatLog.Refresh()

Pass parameter in an already opened form in C#

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!

get user input in form2 and display data in form1 in c#

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

c# call function from class

now i have the current code o MainUC.cs:
private void tsbNoviRacun_Click(object sender, EventArgs e)
{
if (racunuc == null)
{
racunuc = new RacunUC();
racunuc.Dock = DockStyle.Fill;
Controls.Add(racunuc);
}
racunuc.BringToFront();
The thing i want to do is clean the code from main page/form. I have 2 taskbar and 2 toolbar buttons that are calling the same form (this one above), so i don't want to write the code 4 times. I tried to make new class.cs with properties and do it with return value, but it didn't work. Can someone help me with it, or, is there possiblity to call the same code on current page/form. Something like
private void tsbStariRacuni_Click(object sender, EventArgs e)
{
call tsbNoviRacun();
}
"( this isn't working, i know :p)
EDiT: Oh damn me, thanks guys!
In c# there is no "call" keyword for invoking functions. You just type the name and all required arguments in round brackets.
private void tsbStariRacuni_Click(object sender, EventArgs e)
{
tsbNoviRacun_Click(sender, e);
}
This should do it:
public void tsbNoviRacun()
{
if (racunuc == null)
{
racunuc = new RacunUC();
racunuc.Dock = DockStyle.Fill;
Controls.Add(racunuc);
}
racunuc.BringToFront();
}
private void tsbNoviRacun_Click(object sender, EventArgs e)
{
tsbNoviRacun();
}
You can call that method from all the event handlers you want it to run on. Obviously this function is depended on Controls and DockStyle so you must put it within scope of this.

Categories

Resources