How can i load existing page? - c#

I work a lot of page. When i return the previous page, code reset the page. I want to see previous page. How can i this?
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void BtnClickP1(object sender, RoutedEventArgs e)
{
Main.Content = new KK();
}
private void BtnClickP2(object sender, RoutedEventArgs e)
{
Main.Content = new AIRFOIL();
}
private void BtnClickP3(object sender, RoutedEventArgs e)
{
Main.Content = new OB();
}
}
I use new command for every tab page. I think it is wrong. I want to see existing page when i push button. Thanks for helping.

You have to use variable if you want to store the modification in the page.
public partial class MainWindow : Window
{
private KK _kkPage;
private AIRFOIL _airfoilPage;
private OB _obPage;
public MainWindow()
{
InitializeComponent();
_kkPage = new KK();
_airfoilPage = new AIRFOIL();
_obPage = new OB();
}
private void BtnClickP1(object sender, RoutedEventArgs e)
{
Main.Content = _kkPage;
}
private void BtnClickP2(object sender, RoutedEventArgs e)
{
Main.Content = _airfoilPage;
}
private void BtnClickP3(object sender, RoutedEventArgs e)
{
Main.Content = _obPage;
}
}

Related

add pagebar in my form for to change page

I made a simple GUI, in which I would like to add an option where I can switch pages, it is the same page when I open it, I would like a bar where I click and switch pages
namespace POS
{
public partial class Form1 : Form
{
private object gunaLabel_date;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Guna.UI.Lib.ScrollBar.PanelScrollHelper flowphelper ;
gunaLabel_date = DateTime.Now.ToLongDateString();
}
private void gunaVScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
}
private void userControl17_Load(object sender, EventArgs e)
{
}
}
}

wpf how to send string or info on anther page or window or usercontrol

i have big problem visual studio wpf , when i try to send any string it doesn't work
i tried to write public string str { set; get; } This problem has been stuck with it for a long time, I tried a lot to use crooked methods, but the program or project crashes.
im using visual studio wpf c#
thank you for answer
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_MouseEnter(object sender, MouseEventArgs e)
{
Info.Content = "Hello world";
}
private void button2_Click(object sender, RoutedEventArgs e)
{
Frame.Content = new Page1();
}
private void button2_MouseLeave(object sender, MouseEventArgs e)
{
Info.Content = "Label";
}
private void button1_MouseLeave(object sender, MouseEventArgs e)
{
Info.Content = "Label";
}
}
here Page class
public partial class Page1 : Page
{
public Page1()
{
InitializeComponent();
}
private void button1_MouseEnter(object sender, MouseEventArgs e)
{
MainWindow mw = new MainWindow();
mw.Info.Content = "Test Public";
}
}
Update Page1's button1_MouseEnter code to:
private void button1_MouseEnter(object sender, MouseEventArgs e)
{
MainWindow mw = Application.Current.Windows.OfType<MainWindow>().First();
mw.Info.Content = "Test Public";
}
or
private void button1_MouseEnter(object sender, MouseEventArgs e)
{
MainWindow mw = (MainWindow)Application.Current.MainWindow;
mw.Info.Content = "Test Public";
}

Three buttons in window form opening a same another form

I have three buttons in a window form to open 3 differnet forms, but these are opening same form which should be opened by button1 only.
public partial class Home : Form
{
public Home()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Task addnew = new Task();
addnew.Show();
}
private void button2_Click(object sender, EventArgs h)
{
Task History = new Task();
History.Show();
}
private void button3_Click(object sender, EventArgs v)
{
Task Evaluate = new Task();
Evaluate.Show();
}
}
if your form name are Task , History and Evaluate then Initialize your three from in three method
public partial class Home : Form
{
public Home()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Task addnew = new Task();
addnew.Show();
}
private void button2_Click(object sender, EventArgs h)
{
History history = new History();
history.Show();
}
private void button3_Click(object sender, EventArgs v)
{
Evaluate evaluate = new Evaluate();
evaluate.Show();
}
}

get value from startup window

I changed the startup window at the app.xaml with this code:
Startup="ApplicationStart"
At the app.xaml.cs file is this method:
private void ApplicationStart(object sender, StartupEventArgs e)
{
Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;
ChooseAccountWindow chooseAccountWindow = new ChooseAccountWindow();
chooseAccountWindow.ShowDialog();
}
The Code of the window (ChooseAccountWindow()):
public partial class ChooseAccountWindow : MetroWindow
{
public ChooseAccountWindow()
{
InitializeComponent();
}
private void btnDastaschentuch2013_Click(object sender, RoutedEventArgs e)
{
//send value "dastaschentuch2013" to the main window
}
private void btnSkeptar_de_Click(object sender, RoutedEventArgs e)
{
//send value "skeptar_de" to the main window
}
private void btnAsdf_de_Click(object sender, RoutedEventArgs e)
{
//send value "asdf_de" to the main window
}
}
If I press one of the button`s, then a value should be send to the main code. How can I do that?
Answer
I had to change the MainWindow.xaml.cs code:
namespace EbayManager
{
public partial class MainWindow : MetroWindow
{
private string selectedAccount;
public MainWindow()
{
InitializeComponent();
}
public MainWindow(string selectedAccount): this()
{
this.selectedAccount = selectedAccount;
}
}
}
public partial class ChooseAccountWindow : MetroWindow
{
public string Result { get; set; }
public ChooseAccountWindow()
{
InitializeComponent();
}
private void btnDastaschentuch2013_Click(object sender, RoutedEventArgs e)
{
this.Result = "dastaschentuch2013";
this.Close();
}
private void btnSkeptar_de_Click(object sender, RoutedEventArgs e)
{
this.Result = "skeptar_de";
this.Close();
}
private void btnTrachsel_de_Click(object sender, RoutedEventArgs e)
{
this.Result = "trachsel_de";
this.Close();
}
}
In App.xaml remove StartupUri to prevent automatical main window opening
In App.xaml.cs:
private void ApplicationStart(object sender, StartupEventArgs e)
{
Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;
ChooseAccountWindow chooseAccountWindow = new ChooseAccountWindow();
chooseAccountWindow.ShowDialog();
MainWindow main = new MainWindow(chooseAccountWindow.Result);
// insert your startup uri class name instead of MainWindow;
// add constructor to this window that will take string as input parameter
main.Show();
}
If I read you correctly, you can reference the main window like this:
Application.Current.MainWindow and then set a property on it.
void Button_Click(object sender, RoutedEventArgs e)
{
Application.Current.MainWindow.MyProperty = "SomeValue";
MainWindow.ShowDialog();
this.Close();
}

How can I amend a record that I have inputting?

I am creating a To Do List using Windows Forms.
This is what I have so far.
The code for this form is as follows
namespace To_Do_List
{
public partial class To_Do_List : Form
{
public To_Do_List()
{
InitializeComponent();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e) //this is the exit button on the toolstrip
{
Application.Exit(); //exits the program when the Exit button is clicked in dropdown menu
}
private void button4_Click(object sender, EventArgs e)//This creates the button to open the about form
{
AboutMyProgram aboutForm = new AboutMyProgram();
aboutForm.ShowDialog(); //opens about form
}
private void button3_Click(object sender, EventArgs e) //This creates the button to open the form which ammends tasks
{
AmmendItem CreateForm = new AmmendItem(this);
CreateForm.Show();
}
private void button1_Click(object sender, EventArgs e) // This creates the button to open the form which creates new tasks
{
AddItem CreateForm = new AddItem(this);
CreateForm.Show();
}
public void listView1_SelectedIndexChanged_1(object sender, EventArgs e) // This creates the table
{
}
private void button2_Click(object sender, EventArgs e) //This Allows the user to delete entries
{
if (listView1.SelectedItems != null)
{
var confirmation = MessageBox.Show(
"Are you sure you want to delete this?",
"WARNING", MessageBoxButtons.YesNo, MessageBoxIcon.Question
);
if (confirmation == DialogResult.Yes)
{
for (int i = listView1.Items.Count - 1; i >= 0; i--)
{
if (listView1.Items[i].Selected)
{
listView1.Items[i].Remove();
i--;
}
}
}
}
}
}
}
To add a task, the form looks like this
And again, the code is as follows
namespace To_Do_List
{
public partial class AddItem : Form
{
To_Do_List home;
public AddItem(To_Do_List parent)
{
InitializeComponent();
home = parent;
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label1_Click_1(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
}
private void openListToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void sortByDateToolStripMenuItem_Click(object sender, EventArgs e)
{
}
public void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
}
public void textBox1_TextChanged(object sender, EventArgs e)//Title Box
{
}
public void /*Description of Task*/richTextBox1_TextChanged(object sender, EventArgs e)
{
}
public void /*Priority Box*/comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close(); //causes the window to close but keeps the application running
}
private void aboutToDoListToolStripMenuItem_Click(object sender, EventArgs e)
{
AboutMyProgram aboutForm = new AboutMyProgram();
aboutForm.ShowDialog(); //opens about form displaying copyright information
}
public void button1_Click(object sender, EventArgs e) //create task button
{
ListViewItem item1 = new ListViewItem(Title.Text);
item1.SubItems.Add(Description.Text);
item1.SubItems.Add(Priority.Text);
item1.SubItems.Add(Date.Text);
string value = "";
bool isChecked = Completed.Checked;
if (isChecked)
item1.SubItems.Add(Completed.Text);
else
value = null;
home.listView1.Items.Add(item1);
this.Close();
}
private void Date_ValueChanged(object sender, EventArgs e)
{
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e) //Closes the window
{
this.Close();
}
}
}
The Amend form is exactly the same as the New Task form. How would I go about being able to select a record, pressing the amend button and actually changing?
I'm pretty stumped.
Also, this isn't really part of this question but I'll ask it just in case someone knows.
How would I go about being able to actually save these records so that they are there when I open the application back up again?
Thank you very much for reading, I know that I have just dumped everything but I'm really new to Windows Forms so I didn't want to accidentally miss something important out.
Edit
Am I on the right road with something like this?
public void button1_Click(object sender, EventArgs e)
{
To_Do_List editform = new To_Do_List(Title.Text);
editform.Description.Text = listView1.SelectedItems[0].SubItems[0].Text;
Still can't get it to work :/
This code for popup window.
public partial class frmToDoDetails : Form
{
public string TaskTitle { get; set; }
public string Description { get; set; }
public bool EditMode { get; set; }
public frmToDoDetails()
{
InitializeComponent();
}
private void btnSave_Click(object sender, EventArgs e)
{
TaskTitle = txtTitle.Text;
Description = txtDesc.Text;
this.DialogResult = System.Windows.Forms.DialogResult.OK;
}
private void frmToDoDetails_Load(object sender, EventArgs e)
{
if (EditMode)
{
txtTitle.Text = TaskTitle;
txtDesc.Text = Description;
}
else {
txtTitle.Text = string.Empty;
txtDesc.Text = string.Empty;
}
txtTitle.Focus();
}
}
And this for list
public partial class frmToDo : Form
{
public frmToDo()
{
InitializeComponent();
}
private void btnNew_Click(object sender, EventArgs e)
{
frmToDoDetails frm = new frmToDoDetails();
if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string[] arr = new string[2];
ListViewItem itm;
arr[0] = frm.TaskTitle;
arr[1] = frm.Description;
itm = new ListViewItem(arr);
listView1.Items.Add(itm);
}
}
private void frmToDo_Load(object sender, EventArgs e)
{
listView1.View = View.Details;
listView1.GridLines = true;
listView1.FullRowSelect = true;
//Add column header
listView1.Columns.Add("Title", 100);
listView1.Columns.Add("Desc", 70);
}
private void listView1_DoubleClick(object sender, EventArgs e)
{
ListViewItem currentItem= listView1.SelectedItems[0];
frmToDoDetails frm = new frmToDoDetails();
frm.TaskTitle = currentItem.SubItems[0].Text;
frm.Description = currentItem.SubItems[1].Text;
frm.EditMode = true;
if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
currentItem.SubItems[0].Text=frm.TaskTitle;
currentItem.SubItems[1].Text=frm.Description;
}
}
}
I am not added your complete fields(priority, date,etc..).
I hope it will help you.

Categories