How can I navigate and pass data between Pages? - c#

I'm a bit of a beginner with this so i'll try and keep it simple.
I have a a xaml page with a button click event linking it to another xaml page. What I'm trying to do is on the click event take two strings and pass them to a text box on the second page. Can you please show me a simple code example of how to do this?

On the button click event of the first page you do something like the following
private void button1_Click(object sender, RoutedEventArgs e)
{
string urlWIthData = string.Format("/Page2.xaml?name={0}", txtName.Text);
this.NavigationService.Navigate(new Uri(urlWIthData, UriKind.Relative));
}
On the desintation page, you do the following:
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
myTextBox.Text = this.NavigationContext.QueryString["name"].ToString();
}

Related

Winform copy button text to textbox using universal method

So this is a fairly straightforward thing, and I am just curious if there is a better way to do it to save lines of code. For class we are making a teletype machine. Basically there is a textbox, and a series of buttons A-Z and 0-9. When you click the button it adds the corresponding letter/number to the textbox. When you click send, it adds the contents of the textbox to a label and resets the textbox. Everything works and it only took a few minutes to build. However there is a mess of redundant lines and I was curious if there is a way to clean up the code with a method.
This is my current code.
private void btn_A_Click(object sender, EventArgs e)
{
box_UserInput.Text = box_UserInput.Text + "A";
}
As you can see, it is very simplistic and straight forward. Click A, and "A" gets added to the textbox. However the Text property of the button is also just "A" and I want to know if there is a way to just copy the text property of that button and add it to the textbox string.
Something like this, except with a universal approach where instead of having to specify btn_A it just inherits which button to copy based on the button clicked. That way I can use the same line of code on every button.
private void btn_A_Click(object sender, EventArgs e)
{
box_UserInput.Text = box_UserInput.Text + btn_A.Text;
}
You can use this which is more universal as the Control class contains the Text property. Also, using the best practice $"".
private void btn_A_Click(object sender, EventArgs e)
{
box_UserInput.Text = $"{box_UserInput.Text}{((Control)sender).Text}";
}
You can also assign the same event to each button. Create an event, say addControlTextOnClick and assign the same event to each button.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void addControlTextOnClick(object sender, EventArgs e)
{
box_UserInput.Text = $"{box_UserInput.Text}{((Control)sender).Text}";
}
}
You can even shorten this more using this C# construct:
private void addControlTextOnClick(object sender, EventArgs e) =>
box_UserInput.Text = $"{box_UserInput.Text}{((Control)sender).Text}";

Relevant Tabpage show when click the button in c#

I insert tab-control in my windows application.It has 4 tab-pages.I wanted to show only relevant tab-page when I click the relevant button..my code as follows
private void Form1_Load(object sender, EventArgs e)
{
tabControl1.TabPages.Remove(tabPage1);
tabControl1.TabPages.Remove(tabPage2);
tabControl1.TabPages.Remove(tabPage3);
tabControl1.TabPages.Remove(tabPage4);
this.tabPage1.Hide();
this.tabPage2.Hide();
this.tabPage3.Hide();
this.tabPage4.Hide();
}
first every tab-page removed when form load
Here is the code for button click and I coded for 4 buttons...
private void button2_Click(object sender, EventArgs e){
tabControl1.TabPages.Insert(0, tabPage1);
this.tabPage1.Show();
tabControl1.TabPages.Remove(tabPage2);
tabControl1.TabPages.Remove(tabPage3);
tabControl1.TabPages.Remove(tabPage4);
this.tabPage2.Hide();
this.tabPage3.Hide();
this.tabPage4.Hide();
}
I again used ....
tabControl1.TabPages.Remove(tabPage2);
tabControl1.TabPages.Remove(tabPage3);
tabControl1.TabPages.Remove(tabPage4);
this.tabPage2.Hide();
this.tabPage3.Hide();
this.tabPage4.Hide();
these code. if another tabpage is opened when I click the button it should be remove and show relevant tabpage.Its working.
My problem is.. if I click the same button again and again same tabpages adding continuously
Can anyone give me a solution for it..
I founded a one way...In button click I modify like this.
private void button1_Click(object sender, EventArgs e)
{
tabControl1.TabPages.Remove(tabPage1);
if (tabControl1.TabPages.Count <= 1)
{
tabControl1.TabPages.Insert(0, tabPage1);
this.tabPage1.Show();
tabControl1.TabPages.Remove(tabPage2);
tabControl1.TabPages.Remove(tabPage3);
tabControl1.TabPages.Remove(tabPage4);
this.tabPage2.Hide();
this.tabPage3.Hide();
this.tabPage4.Hide();
}
}
first code is tabControl1.TabPages.Remove(tabPage1).Then if tab-page is open It removed.If statement responsible for when button click relevant tab page is opened.then always show only one tab-page for button click moment

redirection from one user control to another user control

I have a usercontrol.ascx in a aspx page. And in my user control I have a button click event, and I need to open another user control from this button click. Please give me some suggestions on how to achieve this.
Thanks.
You can use Visible property. Let's call the user control with the button ucControl1 and the other ucControl2.
// code-behind of ucControl1
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
ucControl2.Visible = false;
}
protected void btn_Click(object sender, EventArgs e)
{
ucControl2.Visible = true;
}

If touch a control, how can I get this control name?

After I touch a control, such as Button, How can I get this button's name?
If you mean "click" instead of "touch", put something like this in your View XAML file:
<Button x:Name="MyButton" MouseDown="MyButton_MouseDown">
Then add following lines to the View CODE file:
void MyButton_MouseDown(object sender, MouseButtonEventArgs e) {
base.OnMouseUp(e);
var control = sender as FrameworkElement;
Console.WriteLine(control.Name);
}
When you click on a control you are raising an event which called : Control_Click
Then you can do whatever you want in that even. As you asked about button I'm gonna show you how you can get button's name . You can have the similar code for other controls :
private void button1_Click(object sender, EventArgs e)
{
string control_name = button1.Name;
MessageBox.Show(control_name);
}

Bind textbox to webbrowser?

Could you show me how to bind textBox1 (Address Bar) to webBrowser1 (Web Page) so what ever the user navigates to on the page will show in the box? Or is their another way to do this?
You can have the events for WebBrowser like DocumentCompleted, Navigating, Navigated,
Please see the sample code , let me know if you have any queries.
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Navigate("http://www.google.com");
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
textBox1.Text = webBrowser1.Url.ToString();
}

Categories