I am creating a WindowsPhone app and (at the moment) I have three pages. Page1, page2 and page3. Page1 has two (2) textboxes that accepts the fist and last name, txtFN and txtLN and a button. The button takes you to the form2, that asks you the nickname. That has one textbox and a button. When the user clicks the button, it takes him the form3 that has a textblock. The nickname the user inputs in form2, displays in form3, on the textblock. I have gotten that part correct. Now, I am trying to do this: if the user does not input anything in the nick name section, I would like to show the first name in form three. Here's my code for form2->form3 event:
in form2:
private void Button_Click(object sender, RoutedEventArgs e)
{
Frame.Navigate(typeof(Page4_phone_Number_extraction), "Alright " + Texbox_nickname_extraction.Text + "!");
}
in form3,
protected override void OnNavigatedTo(NavigationEventArgs e)
{
Textblock_nickname_display.Text = e.Parameter.ToString();
}
And this works like a charm..
Again, for the life of me, i can't think of how to have the first name show up in form3, if there is no user input.
One option would be to store the data in a local app settings like:
Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
localSettings.Values["FirstName"] = txtFN.txt;
in page 1.
You can then extract it in page 3 as:
Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
Textblock_nickname_display.Text=localSettings.Values["FirstName"].ToString();
You can also use OnNavigatedFrom() to achieve the same functionality, but it involves more work:
In Page1:
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
Page2 destinationPage = e.Content as Page2;
if (destinationPage != null)
{
destinationPage.FirstName = txtFN.Text;
}
}
Make sure to have a string FirstName {get;set;} in Page2. Now use the same method to send the variable to Page3 too. You can then use the FirstName variable in Page3.
Needless to say, the first method is simpler.
Microsoft's documentation contains an example that shows that you shouldn't use OnNavigatedTo(NavigationEventArgs e) like you did but instead:
private void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
{
string name = e.NavigationParameter as string;
if (!string.IsNullOrWhiteSpace(name))
{
tb1.Text = "Hello, " + name;
}
else
{
tb1.Text = "Name is required. Go back and enter a name.";
}
}
Related
I have a entry/Textbox. And i have 10 number buttons as a calculator.(0-1-2-3-4....) When my page loaded entry having a number. Its showing me start value.( For example when my page loaded entry text being 10 already without any click to button) So i want to when i click a number button It will delete first entry value that loaded with page and it will start write my button clicks. How can I do it ?
I tried with:
private void btn1_Clicked(object sender, EventArgs e)
entry.Text="";
entry.Text="1";
But of course it didn't work because if i want to write 123 I cant write its always deleting. I just want delete first value which is coming with page loaded.
As #Jason suggested use the Placeholder property to display the initial value and in the click events append the value to the entry text:
Note: you can have a single event delegate that handle the click event for all buttons:
Constructor()
{
//set initial value here or use binding
//entry.Placeholder =
}
private void btn_Clicked(object sender, EventArgs e)
{
//you can remove the initial value if you want
//entry.Placeholder = string.Empty;
string buttonText = ((Button)sender).Text;
entry.Text = $"{entry.Text}{buttonText}";
}
An alternative way is to add a boolean flag out of the method to avoid this.
public class MyCalculator ()
{
private bool _isNewNumber;
protected override void OnAppearing()
{
_isNewNumber = true; //whenever open the page, needs to refresh
}
private void btn1_Clicked(object sender, EventArgs e)
{
if (_isNewNumber)
{
entry.Text=""; //clear the number for the first time
}
entry.Text="1";
_isNewNumber = false;
}
}
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!
I'm wondering what is my issue on passing a variable from page to page using asp.net session.
I've stripped the code down to just one text box to see whats going on. I'm just trying to take the value of a text box and display it on a confirmation page. When the button is clicked it transfers me to the second page but there label is blank. Yes my post back url is pointing to the second page.
Here is the button click:
protected void submit_Click(object sender, EventArgs e)
{
string name = txtFirstName.Text.Trim();
Session["name"] = name;
}
Here is the page load of the second page:
protected void Page_Load(object sender, EventArgs e)
{
lblName.Text = (string)(Session["name"]);
}
Unless I've been looking at this to long and missed something. I've already read "How to: Read Values from Session State" from MSDN.
You say that you've set the PostBackUrl to your second page. If you're going to do it that way, you need to use Page.PreviousPage to get access to your textbox. But this is the easiest way:
Firstly, leave the PostBackUrl alone. Setting the PostBackUrl to your second page means that you're telling the SECOND PAGE to handle your button click, not the first page. Hence, your session variable never gets set, and is null when you try to pull it.
This should work for ya.
And yes, you can also do this with a QueryString, but if its something that you don't want the user to see/edit, then a Session variable is better.
protected void submit_Click(object sender, EventArgs e)
{
string name = txtFirstName.Text.Trim();
Session["name"] = name;
Response.Redirect("PageTwo.aspx");
}
Then in the second page (You don't REALLY need the ToString()):
protected void Page_Load(object sender, EventArgs e)
{
if (Session["name"] != null)
{
lblName.Text = Session["name"].ToString();
}
}
EDIT -- Make sure that your button click actually gets fired. Someone can correct me wrong on this, as I do most of my work in VB.NET, not C#. But if you don't specify the OnClick value, your function won't get called.
<asp:Button ID="Button1" runat="server" Text="Click Me!" OnClick="submit_Click" />
The code you have posted looks fine, so your problem is probably with setup.
Check this link ASP.NET Session State Overview and pay particular attention to the sections on Cookieless SessionIDs and Configuring Session State.
I don't think you added the session. This is how I have done mine.
First Page
protected void btnView_Click(object sender, EventArgs e)
{
foreach (ListItem li in lbxCheckDates.Items)
{
if (li.Selected == true)
{
lblMessage.Text = "";
string checkDate = lbxCheckDates.SelectedItem.Text;
Session.Add("CheckDates", checkDate);
Page.ClientScript.RegisterStartupScript(
this.GetType(), "OpenWindow", "window.open('Paystub.aspx','_newtab');", true);
}
}
}
Second Page
protected void Page_Load(object sender, EventArgs e)
{
string checkDate = (string)(Session["CheckDates"]);
//I use checkDate in sql to populate a report viewer
}
So with yours, I think you need..
protected void submit_Click(object sender, EventArgs e)
{
string name = txtFirstName.Text.Trim();
Session.Add("Name", name);
}
I think what you have in the second page should work, but if it doesn't, add ToString() to it like..
lblName.Text = (string)(Session["name"]).ToString();
Let me know if this helps!
Are you doing a redirect after setting the session variable on the first page, if so you it will not work correctly (unless you know the trick). Checkout this article on making it work. Basically, the way to make this work is to the overload redirect method.
Response.Redirect("~/newpage.aspx", false);
The false parameter prevents .net from terminate processing on the existing page (that actually writes out the session state)
For Second Page
protected void Page_Load(object sender, EventArgs e)
{
if (Session["value"] != null)
{
Label1.Text = Session["value"].ToString();
}
else
{
Label1.Text = "Sorry,Please enter the value ";
}
}
You can use Server.Transfer() instead of Response.Redirect()
For first page, use this:
protected void Button1_Click(object sender, EventArgs e)
{
string value = TextBox1.Text;
Session["value"] = value;
Response.Redirect("~/Sessionpage.aspx");
}
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();
}
}
}