What I'm trying to do it load some information from a database.
To do this I open a form that lists everything that can be loaded.
When you click load I want to pass the ID back to the original form.
However I can't seem to be able to call a method in that form.
Any help would be appreciated.
I would flip this around:
Make the selection form into a modal dialog that is created and displayed by the form where you want to load something
Expose the selection made in the dialog through a property or method in the dialog form
This way the selection form will be decoupled from the caller, and can be reused wherever it makes sense without the need to modify it.
In the selection dialog form class:
public string GetSelectedId()
{
return whateverIdThatWasSelected;
}
In the calling form:
using(var dlg = new SelectionDialogForm())
{
if (dlg.ShowDialog() == DialogResult.OK)
{
DoSomethingWithSelectedId(dlg.GetSelectedId());
}
}
You could add a property to your form class and reference it from your other form.
eg.
public class FormA : Form
{
private string _YourProperty = string.empty;
public string YourProperty
{
get
{
return _YourProperty;
}
set
{
_YourProperty = value;
}
}
}
public class FormB : Form
{
public void ButtonClick(object sender, EventArgs args)
{
using (FormA oForm = new FormA)
{
if (oForm.ShowDialog() == DialogResult.OK)
{
string Variable = oForm.YourProperty;
}
}
}
You just need to set your property on a button click on form A then you can access it from form B
}
Why not create a public property for the selected item in the dialog form, something like this.
public int SelectedItemId {get;private set;}
//In your item selected code, like button click handler..
this.SelectedItemId = someValue;
Then just open the form as a Dialog
//Open the child form
using (var form = new ChildForm())
{
if (form.ShowDialog(this) == DialogResult.OK)
{
var result = form.SelectedItemId;//Process here..
}
}
The proper way to do this is to introduce a Controller class which is used by both forms. You can then use a property on the Controller, when that is set will trigger the NotifiyPropertyChanged event.
see INotifyPropertyChanged for more info
Related
I'm having trouble transporting information from one form to another, is to make a single save, but the information is distributed in 2 forms and I have to do it using dto. I know that for this I have to send the data that I want by the form builder method, as you can see in the code below:
public FrmModalFornecedor(int providerId, int providerDoc)
{
InitializeComponent();
CbxListarFornecedor();
providerDoc = Convert.ToInt32(txtDoc.Text);
providerId = Convert.ToInt32(((Provider)cbxFornecedor.SelectedItem).ProviderId);
}
But now my questions are:
How to make these variables take their respective text box and combo box values?
How to make the next form have access to this data?
You can create additional properties in the second form and you can pass the values from first form to second form.
Hope this gives some ideas for you.
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.MyName = "Pass my name here";
frm.Show();
}
public partial class Form2 : Form
{
public string MyName { get; set }
public Form2()
{
InitializeComponent();
}
}
I am trying to code a WPF desktop Application. Currently i have a Main Window (MainWindow) and a page (Pageone) under the same solution. From my MainWindow.xaml.cs page, i have a variable (proc1) which i want to pass to my Pageone.xaml.cs page and maybe even more pages in the future to access and use for some calculation.
However i cant seem to find a method to successfully do this, i have tried making my variable "public", and instantiate the MainWindow object for my page to access it but it doesn't seem to work. (A field initializer cannot reference the non-static field, method, or property 'Pageone.pog')
MainWindow.xaml.cs
public string proc1;
public void startTroubleshootButton_Click(object sender, RoutedEventArgs e)
{
try
{
var selectedProcess = listViewProcesses.SelectedItems[0] as myProcess;
if (selectedProcess == null)
{
MessageBox.Show("no selection made");
return;
}
proc1 = selectedProcess.processName;
MessageBox.Show($"you have selected the {proc1} application ");
Pageone pg = new Pageone(this);
this.Content = pg;
}
catch(ArgumentOutOfRangeException)
{
return;
}
}
Pageone.xaml.cs
public partial class Pageone : Page
{
public Pageone(MainWindow mainWindow)
{
InitializeComponent();
}
MainWindow pog = new MainWindow();
string procName = pog.proc1;
...
I've heard that i will maybe need to use something called the MVVM or code a parameterized constructor but i'm not sure if its related to the code i'm doing. Is there a better way to go about coding this? Thanks.
It can be done like:
var window = (MainWindow)Application.Current.MainWindow;
I am trying to develop a program in which it could create forms and add controls to it at runtime.
It also should be able to save, (Open and Edit) the forms created with the new controls added it at Runtime.The Application starts In the Main form.
CODE BEHIND MAIN Form
private void Btn_CREATE_FORM_Click(object sender, EventArgs e)
{
Form_Properties fp = new Form_Properties();
fp.Show();
}
private void BTn_ADD_BTN_Click(object sender, EventArgs e)
{
/// WHAT CODE SHOULD I ENTER TO ADD BUTON TO NEW FORM
}
Basically the main form is used to create/open/save new forms and add controls to it.
When the user clicks on Create New Form button the user will be presented with the following form (FORM_PROPERTIES) in which the user can customize the name, width and height of the new form.
CODE BEHIND FORM_PROPERTIES Form
public partial class Form_Properties : Form
{
public Form_Properties()
{
InitializeComponent();
}
String form_name;
int form_width;
int form_height;
private void Btn_OK_Click(object sender, EventArgs e)
{
form_name = TBox_NAME.Text;
form_width = Convert.ToInt32(TBox_WIDTH.Text);
form_height = Convert.ToInt32(TBox_HEIGHT.Text);
New_Form nf = new New_Form();
nf.Text = form_name;
nf.Width = form_width;
nf.Height = form_height;
nf.Show();
}
}
The following image shows what happens at runtime based on the code I have written so far.
ISSUES
Need help to Write Code
To add controls to new form created.
To Save/Open/Edit Functionalities.
I also need to know the method to access properties of added controls at runtime.
eg: If the user adds a text box to the NEW FORM and decides to type some text in it, I need a method to save that text.
Is there a way for me to name the added controls?
It seems you want to build some kind of WinForms' form designer. Your program would be similar to Glade (though Glade is much more powerful).
I'm afraid the question is too broad, though. There are many questions to answer, for example, how do you describe the created interface.
While Glade uses XML, you can choose another format, such as JSON. Let's say that you have a TextBox with the word "example" inside it.
{ type:"textbox" text:"example" }
It seems you want to add your components to the form as in a stack. Maybe you could add its position. For example, a form containing a label
("data"), a textbox ("example"), and a button ("ok"), would be:
{
{ pos:0, type:"label", text:"data" },
{ pos:1, type:"textbox", text:"example" },
{ pos:2, type:"button", text:"ok" },
}
But this is just a representation. You need to a) store this when the form is saved, and b) load it back when the form is loaded.
For that, you will need a class representing the components, such as:
public class Component {
public override string ToString()
{
return string.Format( "position:{0}, text:{1}", this.Position, this.Text );
}
public int Position { get; set; }
public string Text { get; set; }
}
public class TextBoxComponent: Component {
public override string ToString()
{
return base.ToString() + "type:\"textbox\"";
}
}
...and so on. This is a big task, I'm afraid, with no simple answer.
I have the following static method that adds a selected product into an order.
public static void addToOrderFromPicture(string product, string qty, string _price)
{
//I've cut the code as it's not important to the question.
order.Add(product);
}
The products are displayed as Controls on a flow layout panel. The user will click the 'Add to Order' button on the control. This activates the following method.
private void btn_add_Click(object sender, EventArgs e)
{
if (Main.sessionInProgress == true)
{
OrderQty qty = new OrderQty(lbl_caseSize.Text.ToString(), lbl_wholesale.Text.ToString(), lbl_product.Text, lbl_volume.Text.ToString(), lbl_physical.Text, lbl_available.Text, lbl_oo.Text, lbl_inner.Text, lbl_pltQty.Text, lbl_repeat.Text);
qty.StartPosition = FormStartPosition.CenterParent;
DialogResult result = qty.ShowDialog();
if (result == DialogResult.Yes)
{
if (Main.roundCheck == true)
{
// MessageBox.Show(qty.qtyReturn.ToString());
qty.qtyReturn = autoRoundToCaseSize(qty.qtyReturn);
//MessageBox.Show(qty.qtyReturn.ToString());
Main.addToOrderFromPicture(lbl_product.Text.ToString(), qty.qtyReturn.ToString(), qty.priceReturn.ToString());
}
else
{
Main.addToOrderFromPicture(lbl_product.Text.ToString(), qty.qtyReturn.ToString(), qty.priceReturn.ToString());
}
btn_add.Text = "X";
btn_add.BackColor = Color.FromArgb(236, 112, 99);
}
}
}
The reason for the main function being static is so I can call it from this method. In Swift I would use a delegate to pass data between forms etc. However, I'm unsure on how to do this within C#.
Is there a similar method for passing data between forms as there is in Swift. How would I go about doing it? Or is there a way for me to call the method in Main without the need for it to be static.
EDIT: I don't think I've explained the forms etc very well so will try clear it up.
The addToOrderFromPicture method is contained within Main. This function adds products to the order list which is also static and contained within Main.
The 'btn_add_Click' method is contained in Product.cs which is a UserControl. This user control is displayed on a flowPanel which sits on the main form.
So the user clicks activates a function on Product.cs, this creates an instance of OrderQty.cs which is returns a qty to Product.cs - From Product.cs the addToOrder method within Main is called and the data like qty etc is passed to it.
So Product.cs -> OrderQty.cs -> Product.cs -> Main.cs
Your form is still accessible after you've called this.Close() and ShowDialog has returned, so you can do this:
OrderQty qty = new OrderQty(lbl_caseSize.Text.ToString(), lbl_wholesale.Text.ToString(), lbl_product.Text, lbl_volume.Text.ToString(), lbl_physical.Text, lbl_available.Text, lbl_oo.Text, lbl_inner.Text, lbl_pltQty.Text, lbl_repeat.Text);
qty.StartPosition = FormStartPosition.CenterParent;
DialogResult result = qty.ShowDialog();
if (result == DialogResult.Yes)
{
qty.addToOrderFromPicture(lbl_product.Text.ToString(), qty.qtyReturn.ToString(), qty.priceReturn.ToString());
}
Maybe you can keep your logic in a separate class instead of Main.
public class Service
{
public List<Product> Order { get; set; }
public void addToOrderFromPicture(string product, string qty, string _price)
{
Order.Add(product);
}
}
3 methods to do this.
First as the main window is created once use singleton pattern on it, declare a public static instance of form and access public methods,
Second pass a reference of main window to usercontrol and use its public methods.
Third add an event to usercontrol and hook handler in main window, whenever user clicks button, fire an event.
I got four windows.
Window1 has Textboxes and radiobuttons.
Basing on window1 , window2 opens.
basing on window2 , window 3 opens.
and window4 opens on basis of window1 information but the button is on window3 which opens window4.
what i am trying to do is to send window1 information to window3 so that i can open window4.
but i am unable to do it.
i know how to pass values from one form to the second one. but this is complex and i tried it.
I have even tried the following link as well please enlighten me with the answer.
https://www.daniweb.com/software-development/csharp/threads/370098/hold-a-text-box-string-to-another-window-form
Window2 opening Code:
if ((SeismicLevel_TextBox.Text == "Low") && (LevelOfPerformance_LS_RadioButton.IsChecked == true))
{
Region_of_Low_Seismicity t1_1 = new Region_of_Low_Seismicity();
t1_1.ShowDialog();
}
Window 3:
Window3 w3 = new Window3();
s.ShowDialog();
Window 4 but the button lies on window 3:
if ((w1.SeismicLevel_TextBox.Text == "Low") && (w1.LevelOfPerformance_LS_RadioButton.IsChecked == true))
{
Window4 W4 = new Window4();
W4.ShowDialog();
}
Ideally, you would want to create a ViewModel for each Window, and then inject Window1's ViewModel into the other three via Dependency Injection. Though, for the sake of getting this working (and assuming you are not familiar with MVVM concepts yet), you can simply do the following:
As shown below, just create a POCO that is passed into the constructor of Window3 upon opening. The POCO will be populated with the control values from Window1. You will need to update the constructor for Window3, but it's as simple as adding an new parameter to its constructor.
// POCO to store Window1 info
public class Window1Values
{
public string TextBoxValue1 { get; set; }
public bool CheckBoxValue1 { get; set; }
}
// Update Window 3 Ctor to look like the following, simply just add a parameter to the existing Ctor
//
public Window3(Window1Values window1Values)
{
// ...
}
// When Window 3 is going to open, do the following
//
var w3 = new Window3(new Window1Values
{
TextBoxValue1 = myTextBox.Text;
CheckBoxValue1 = myCheckBox.IsChecked;
});
w3.ShowDialog();
I would pass parameters through the pages.
For example, I'm a bit rusty but you should call a navigator to PageName(). So, pass arguments like PageName(ButtonVal, Text) and give PageName() a constructor accepting everything passed to it, and everything subsequent pages need.
Parameters would be a good idea, but it could get messy having to pass the same data through multiple windows. Instead an option could be to create a "Storage" class which you can save data to and access later on in the program.
public class Storage
{
public static string textBoxValue1 { get; set; }
public static bool checkBoxValue1 { get; set; }
}