Hi members,
I have a simple question about how to pass data to Page from MainWindow.
I searched on google/ and here... But I am not found exact answer.
I would like modify, change the Page1( SW_GUI.xaml) elements such as label content, or later change other GUI element content.
However I only can reached (myself) this way: firstly make change on label of SW_GUI.xaml and then create instance of this Page(SW_GUI.xaml) and load in a frame of MainWindow content.
But if this Page loaded once, I cannot modify /update a label automatically, without loading the SW_GUI Page again into the frame.
You can find the very simple and initial code below.
MainWindow.xaml.cs file
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
// ACTION one - just try reason
SW_GUI sW_GUI = new SW_GUI();
sW_GUI.RESULT_MAKER(true);
CenterFrame.Content = sW_GUI;
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
// ACTION two - just try reason
SW_GUI sW_GUI = new SW_GUI();
sW_GUI.RESULT_MAKER(false);
CenterFrame.Navigate(sW_GUI);
}
}
-----------------
PAGE1 aka SW_GUI
SW_GUI.xaml.cs file
public partial class SW_GUI : Page
{
public SW_GUI()
{
InitializeComponent();
}
public void RESULT_MAKER ( bool results)
{
if (results==true)
{
RESULT_BOX.Background = new SolidColorBrush(Color.FromRgb(0, 245, 95));
RESULT_BOX.Text = "(PASS)";
}
else
{
RESULT_BOX.Background = new SolidColorBrush(Color.FromRgb(245, 53, 0));
RESULT_BOX.Text = "(FAIL)";
}
}
}
SHORT logic: Button_Click on mainwin call the sW_GUI.RESULT_MAKER method and make very simple change on a label. Then, I load the sW_GUI instance into the CenterFrame.Content.
QUESTION: Ca you provide a little guide or give a simple example what I have to add, change to achieve that I don't need to load the page into the frame each case when I want to update a label content etc.
Many thanks for it.
Hello I have made a small sample in MainWindow.cs:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private SW_GUI SW_GUIPage = new SW_GUI();
private void ActivateSW_GUI(bool SetResult)
{
SW_GUI SW = SW_GUIPage;
SW.RESULT_MAKER(SetResult);
if (!(CenterFrame.Content is SW_GUI))
{
CenterFrame.Content = SW_GUIPage;
}
}
int ActionSetter = 0;
private void Button_Click(object sender, RoutedEventArgs e)
{
if (ActionSetter == 0)
{
ActivateSW_GUI(true);
ActionSetter = 1;
}
else if (ActionSetter == 1)
{
ActivateSW_GUI(false);
ActionSetter = 0;
}
}
}
Related
There are two forms, a MainForm and a GraphicsForm.
In MainForm, there are "New" and "Save", "Open" buttons. When clicking the "New", a GraphicsForm created (When the "New" is clicked multiple times, multiple GraphicsForms are created).
The question is, when created multiple GraphicsForms, and the user only wants to save the content in one of them or open a content file to one of them, How to implement this?
MainForm.cs
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private ToolStripMenuItem _winMenuItem = new ToolStripMenuItem();
private GraphicsForm _graphicsForm;
private int _counter = 1;
private ContentDoc _contentDoc = new ContentDoc();
private void New_Click(objec sender, EventArgs e)
{
_winMenuItem.Name = "Win";
_winMenuItem.Text = "Windows";
int item = MainMenuStrip.Items.IndexOf(_winMenuItem);
if (item == -1)
{
MainMenuStrip.Items.Add(_winMenuItem);
MainMenuStrip.MdiWindowListItem = _winMenuItem;
}
_graphicsForm = new GraphicsForm(_contentDoc);
_graphicsForm.Name = string.Concat("Win_", _counter.ToString());
_graphicsForm.Text = _graphicsForm.Name;
_graphicsForm.MdiParent = this;
_graphicsForm.Show();
_graphicsForm.WindowState = FormWindowState.Maximized;
_counter++;
}
private void Save_Click(object sender, EventArgs e)
{
... // here
}
private void Open_Click(object sender, EventArgs e)
{
... // here
}
}
GraphicsForm.cs
public partial class GraphicsForm : Form
{
//ContentDoc is a class to manage all the graphics drawn by the user in the form.
private ContentDoc _contentDoc = new ContentDoc();
public GraphicsForm(ContentDoc contentDoc)
{
InitializeComponent();
_contentDoc = contentDoc;
}
private Canvas_MouseDown()
{
}
private Canvas_Paint()
{
}
...
The parent form has an ActiveMdiChild property, so you can use the to access the currently-selected GraphicsForm instance:
var activeGraphicsForm = ActiveMdiChild as GraphicsForm;
There are other variations you might use, e.g. pattern matching, depending on the specific details and your preference.
You can then put your saving logic in a public method in GraphicsForm and call it from the parent form. Alternatively, you can put your saving logic in the parent form and expose the data to be saved via one or more public properties in GraphicsForm.
How do i pass data from a form i opened in userControl back to the userControl?
userControl code:
public AdminControl()
{
InitializeComponent();
SetGridColomns();
}
private void btnEditInfo_Click(object sender, EventArgs e)
{
Form editForm = new EditForm();
if (editForm.ShowDialog() == DialogResult.OK)
{
// trying to get Owner2 here but failing
}
}
Form Code:
public partial class EditForm : Form
{
public CompanyOwner Owner2;
public EditForm()
{
InitializeComponent();
Owner2 = new CompanyOwner();
}
private void btnSave_Click(object sender, EventArgs e)
{
Owner2.Address = tbAddress.Text;
Owner2.CompanyName = tbCompanyName.Text;
Owner2.Email = tbEmail.Text;
Owner2.Phone = tbPhone.Text;
}
}
When i try to get editForm.Owner2 nothing shows up. I tried making it static, using strings instead of class and still nothing. Where im going wrong?
Nvm i find the mistake ... i should use EditForm instead of Form:
EditForm editForm = new EditForm();
I am new in Xamarin and I want to go to another screen from one screen. Here i have Two sliders, 2 labels and a button. I want to open another screen after clicking on that button and I should display the values of two sliders in another xaml page.. like slider1 = 2.5 slider2=4. How can i do this?
my cs code:
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private void MainSlider1_OnValueChanged(object o, ValueChangedEventArgs e)
{
Double slider1 = MainSlider1.Value + 1;
MainLabel1.Text = Convert.ToString(slider1);
}
private void MainSlider2_OnValueChanged(object o, ValueChangedEventArgs e)
{
Double slider2 = MainSlider2.Value + 1;
MainLabel2.Text = Convert.ToString(slider2);
}
private async void NavigateButton_OnClicked(object o, EventArgs e)
{
await Navigation.PushAsync(new Page1());
}
You can pass the parameters by having a class as below:
private async void NavigateButton_OnClicked(object o, EventArgs e)
{
var yourClass = new YourClass {
Slider1Text = MainLabel1.Text,
Slider2Text = MainLabel2.Text
};
await Navigation.PushAsync (new Page1(yourClass));
}
For this we have to change in the Constructor method of Page1:
public Page1(YourClass yourClass)
{
}
And you can use the data from YourClass as required.
here on Button click to close mordenwindow..thats doesn't work
private void Button_Click_1(object sender, RoutedEventArgs e)
{
// this MainWindow is like this --> <mui:ModernWindow x:Class="FirstFloor.ModernUI.App.MainWindow1" ....>
MainWindow1 mw = new MainWindow1();
// this is my login Page..
Login lg = new Login();
lg.Show();
mw.Close(); //here code is not working
}
What you did in that Button_Click_1 event is you created a new ModernWindow1 then you closed that newly created ModernWindow1, . Now, you technically have two ModernWindow1 in the start of that event. What you need is to close the currently running ModernWindow1, and not the newly created ModernWindow1. to do that, you need to reference the old ModernWindow1 before going to another window.
This is the Second ModernWindow
public partial class ModernWindow2 : ModernWindow
{
public dynamic ReferencedWindow2; //you will put the original Window here
public ModernWindow2()
{
InitializeComponent();
}
public ModernWindow2(dynamic referencedWindow) // second constructor with a parameter
{
InitializeComponent();
ReferencedWindow2 = referencedWindow; // the original modernwindow being put in here
}
private void Button_OnClick(object sender, RoutedEventArgs e)
{
ReferencedWindow2.Close();
}
}
THIS IS THE ORIGINAL OR PRIMARY MODERNWINDOW
public partial class ModernWindow1 : ModernWindow
{
public ModernWindow1()
{
InitializeComponent();
}
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
/*
this will show the second modernwindow using the second constructor with parameter
*/
ModernWindow2 newWindow2 = new ModernWindow2(this);
newWindow2.Show();
}
}
I'm a newbie in c# and visual studio, but not programming in general.
I searched for answer to my question for 3 days and I found plenty of them, but for some weird reason (I'm sure I'm missing something very obvious) I cannot get it to work.
I think it's the most basic question newbies like me ask.
I have a form (Form3) with a text box and a button (I set it up is just for testing purposes).
I want to populate and read this text box from another class. I understand the most proper way to do this is to create a property in Form3.cs with GET and SET accessors. I did that but I cannot get it to work. I'm not getting any error messages, but I'm not able to set the value of the text box either. It just remains blank.
Here's my sample code:
namespace WindowsFormsApplication1
{
public partial class Form3 : Form
{
public string setCodes
{
get { return test1.Text; }
set { test1.Text = value; }
}
public Form3()
{
InitializeComponent();
}
private void Form3_Load(object sender, EventArgs e)
{ }
private void button1_Click(object sender, EventArgs e)
{
a.b();
}
}
public class a
{
public static void b()
{
Form3 v = new Form3();
v.setCodes = "abc123";
}
}
}
Can someone lend me a hand solving this?
The problem is you are setting the value to a new instance of the form. Try something like this:
public partial class Form3 : Form {
public string setCodes
{
get { return test1.Text; }
set { test1.Text = value; }
}
private A a;
public Form3()
{
InitializeComponent();
a = new A(this);
}
private void button1_Click(object sender, EventArgs e)
{
a.b();
}
private void Form3_Load(object sender, EventArgs e)
{
}
}
public class A
{
private Form3 v;
public a(Form3 v)
{
this.v = v;
}
public void b()
{
v.setCodes = "abc123";
}
}
You're creating a brand new Form3() instance.
This does not affect the existing form.
You need to pass the form as a parameter to the method.
Try this:
public partial class Form3 : Form
{
/* Code from question unchanged until `button1_Click` */
private void button1_Click(object sender, EventArgs e)
{
a.b(this);
}
}
public class a
{
public static void b(Form3 form3)
{
form3.setCodes = "abc123";
}
}
This passes the current instance of the form to the other class so that it can update the setCodes property. Previously you were creating a new form instance rather than updating the current form.
Sending form instance to other other class
Form1 objForm1=new Form1();
obj.Validate(objForm1);
Easy way to access controls in another class by modifying Controls Private to Public in the Form(Designer.cs)