How can I reference my Bing Maps element from another form? - c#

I've got a Bing Map element in UserControl1.xaml file:
<UserControl x:Class="MyMaps.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:m="clr-namespace:Microsoft.Maps.MapControl.WPF;assembly=Microsoft.Maps.MapControl.WPF">
<Grid>
<m:Map CredentialsProvider="Gr8GooglyMoogly" x:Name="myMap" />
</Grid>
</UserControl>
I can access it like so from Form1, on which it sits:
this.userControl11.myMap.Mode = new RoadMode();
...but when I try to access it from another form, none of these attempts work:
userControl11.myMap.Children.Add(pin); // does not exist in the current context
Form1.userControl11.myMap.Children.Add(pin); // inaccessible due to its protection level
UserControl1.myMap.Children.Add(pin); // object reference is required for the static field, ...
How can I get a handle on the UserControl from another form?
UPDATE
Using Reza's comment to change the Modifier property of the map from Private to Public, and utilizing the method shown at the link provided, the following works:
var frmMain = new Form1();
frmMain.userControl11.myMap.Children.Add(pin);
UPDATE 2
Reza's idea worked perfectly. This is how I tested it to verify:
In "Form 2" (mdlDlgFrm_AddNewLocation):
// to access map on main form (Form1)
private Form1 frmMain;
// second constructor so as to access map on main form (to add pushpins)
public mdlDlgFrm_AddNewLocation(Form1 f1)
{
InitializeComponent();
this.frmMain = f1;
// test
AddPushpin("blaJustATest");
}
private void AddPushpin(string fullAddress)
{
Pushpin pin = new Pushpin();
// "brute-forcing" the coordinates for this test
pin.Location = new Location(37.1481402218342, -119.644248783588); // Interesting location: out in the "boondocks" between Oakhurst and Auberry
this.frmMain.userControl11.myMap.Children.Add(pin);
}
...and "Form 2" being invoked from the main form (Form 1):
private void addLocationToolStripMenuItem_Click(object sender, EventArgs e)
{
mdlDlgFrm_AddNewLocation frmAddNewLocation = new mdlDlgFrm_AddNewLocation(this);
frmAddNewLocation.ShowDialog(this);
frmAddNewLocation.Dispose()
}

When you create a new form, you must send the current form as a parameter to the creator of the new form, where you can make changes to that instance using the instance you submitted.

In addition to all the options that I've already explained in the linked post and has been told in other posts, including the one that I mentioned in the comments, you can consider the following options here:
If you use ShowDialog to show Form2, you don't need a reference to Map or to Form1. Just return Pushpin from Form2 and use it.
OR Just pass the dependency, the Map to your second form. (No need to make the usercontrol public or pass the whole form1).
Example 1 - Return Pushpin
If you use ShowDialog to show Form2, you don't need a Map in Form2. Just create the Pushpin and return it back to Form1 and use it there.
In Form2, define a Pin property:
//using Microsoft.Maps.MapControl.WPF;
//...
public Pushpin Pin {get; set;}
In Form2, when you want to create the Pushpin, assign it to Pin property:
//...
this.Pin = new Pushpin(){Location = location};
this.DialogResult = DialogResult.OK;
Then in Form1 when you want to show Form2:
using(var f2 = new Form2())
{
if(f2.ShowDialog() == DialogResult.OK)
{
this.userControl11.myMap.Children.Add(f2.Pin);
}
}
Example 2 - Pass the Map
Just pass the dependency, the Map to your second form. You don't need to make the user control public or you don't need to pass the whole form1:
Change Form2 constructor to accept a Map:
//using Microsoft.Maps.MapControl.WPF;
//...
private Map map;
public Form2(Map map)
{
InitializeComponent();
this.map = map;
}
In Form1, when you want to show Form2:
var map = this.userControl11.myMap;
using(var f2 = new Form2(map))
f2.ShowDialog();
In Form2, when you want to use the map:
//...
var pin = new Pushpin(){Location = locatin};
map.Children.Add(pin);

Related

Access parameters from an object listed in a listbox c#

I have several classes in my program and I'm using Windows Form to create the objects from the different classes and list them in different listboxes.
Right now I have a form with all the listboxes (Form 1) and another form (CreerVoiture) where i put all the info to create my selected object.
An example is when i click on the button "Voiture!" my second form opens, when I add all the info and push on "Ajouter" it adds the object to the listbox selected.
Code from Form1:
private void button1_Click(object sender, EventArgs e)
{
CreerVoiture creervoiture = new CreerVoiture();
if (creervoiture.ShowDialog(this) == DialogResult.OK)
{
// Read the contents of form2's TextBox.
Voiture voiture = new Voiture(creervoiture.GetMarque(), creervoiture.GetPrix(), creervoiture.GetConsommation(), creervoiture.GetReservoir());
this.list_voiture.Items.Add(voiture);
this.list_voiture.DataSource = null;
}
creervoiture.Dispose();
}
What I would like to know is how I can access a parameter I added in my second form once I added it to the listbox.
I was thinking to use something like:
list_voiture.SelectedItem.Prix
Prix is a getter from my class Voiture
public double Prix
{
get { return this.prix; }
set { this.prix = value; }
}
But that doesn't seem to be possible. Is this possible and if so, how?
Thanks in advance,
Jeremy
If the type of object is Voiture then you can cast the object to that type:
Voiture voiture = (Voiture)list_voiture.SelectedItem;
double prix = voiture.Prix;
Or in one line:
double prix = ((Voiture)list_voiture.SelectedItem).Prix;

Keep data on multiple form WindowsForms c#

I 'am making a WindowsForms application. I have 2 Forms :
On the first form (Form1), There are many fields (Textboxs) that should be filled by the user, then click to a button (Transfer).
This button should show all the input datas of Form1 in a new Form (Form2).
I'm not sure how to begin to move data from one form to another. Can somebody guide me how to do it?
If Form1 creates Form2 specifically to show the data, then you can use a non-default constructor to pass the information as you create the form.
First, let's consider an example of the information you want to transfer, and name it Form2Info:
// This class is an example of the information you want to transfer
public class Form2Info
{
public string text1;
public int number1;
}
Then, you modify Form2's constructor to take the information:
public partial class Form2 : Form
{
private Form2Info info;
public Form2(Form2Info information)
{
InitializeComponent();
info = information;
// Do something with this information, such as populate a TextBox or Label on the form.
}
}
Finally, you want to create a Form2 instance from your Form1:
// Create the information you want to pass; we fill it with some placeholder data here.
Form2Info info = new Form2Info();
info.text1 = "Hello"
info.number1 = 5;
// Now create the form and pass the data
Form2 form2 = new Form2(info);
form2.ShowDialog(); // Show modal dialog.
Each Textbox has a value (Textbox.Text property). You will have to transfer the content of these different properties to the new controls in your second form.
The easiest way will be through a custom Form constructor for the second form.
public Form2(string textBox1Value, string textBox2Value)
// etc... add as many as you like or use an object that holds all values in properties
{
InitializeComponent(); // Required by WinForms
this.TextBox1.Text = textBox1Value;
this.TextBox2.Text = textBox2Value;
}
Make sure you match the text boxes you want using the names. Finally when creating the form on the Transfer button code, call this constructor instead.

Accessing Data Controls from a different form

i have two forms, form A and form B, to access form B from from A i pass form B in the constructor of form A, that way i am able to access a gridview.
Suggestions were made directing me towards exposing my gridview in a public property and passing it to the other form i want to access it from.
This is my understanding of what was suggested to me:
public RadGridView Grid
{
get { return GridViewDisplay; }
}
then i pass this property to my second form:
Form1 f1 = new form1();
Form2 f2 = new form2(f1.Grid);
This is my issue here:
public void DockAllWindows()
{
SideBar sb = new SideBar();
Summary sm = new Summary();
SalesPoint sp = new SalesPoint(sb, sm); // This is where my issue is, Point A
StartPage start = new StartPage();
radDock.DockControl(sp, (DockPosition.Fill), DockType.Document);
radDock.DockControl(start, (DockPosition.Fill), DockType.Document);
radDock.DockControl(sm, (DockPosition.Right), DockType.ToolWindow);
}
At Point A i am passing an object instance of my summary form to my SalePoint form.
therefore i cannot execute the following code as it would generate an error:
Summary sm = new Summary(sp.Grid); // Error right here
SalesPoint sp = new SalesPoint(sb, sm);
I would like some help getting around the above error.
You need to set the Summary.Grid property after you've created your SalesPoint class.
Summary sm = new Summary();
SalesPoint sp = new SalesPoint(sb, sm);
sm.Grid = sp.Grid.
To be clear you need a public grid property on your SalesPoint class and one on your Summary class. And make you implement the set on the property of the Summary class so you know when someone else changes it.
public RadGridView Grid
{
get { return grid; }
set
{
if (grid != value)
{
grid = value;
// Add any special processing that summary needs to do to pull data from the SalesPoint Grid property.
}
}
}

Creating Form Inside the Form

I'm new to Visual Studio 2010 and I'm planning to create a Timekeeping system. I'm just want to ask how could I create a form that compose 2 forms in it. For example, if I will click a button it will open a new form inside a form. Please help. Thanks
Form formA = new Form();
formA.IsMdiContainer = true;
Form formB = new Form();
formB.MdiParent = formA;
formB.Show();
You have to work with MDI (Multiple Document Interface), have alook at this article that might help.
You could create custom form, remove all borders, and toolbars to make it look as closely to a panel as possible. Then make that new custom form a MdiContainer / MDI-panel and show forms in that panel, something like the code below will do the job
Mdi-Panel definiton:
public class MdiClientPanel : Panel
{
private Form mdiForm;
private MdiClient ctlClient = new MdiClient();
public MdiClientPanel()
{
base.Controls.Add(this.ctlClient);
}
public Form MdiForm
{
get
{
if (this.mdiForm == null)
{
this.mdiForm = new Form();
/// set the hidden ctlClient field which is used to determine if the form is an MDI form
System.Reflection.FieldInfo field = typeof(Form).GetField("ctlClient", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
field.SetValue(this.mdiForm, this.ctlClient);
}
return this.mdiForm;
}
}
}
Usage:
/// mdiChildForm is the form that should be showed in the panel
/// mdiClientPanel is an instance of the MdiClientPanel
myMdiChildForm.MdiParent = mdiClientPanel1.MdiForm;
I think, this is a very easy way:
Form1 form= new Form1 ();
form.TopLevel = false;
this.Controls.Add(form);
form.Show();
Maybe MDI interface will do what you want..
Here's a tutorial to do that.

Sharing a variable between two winforms

I have a winforms application.
I have a textbox on one form (call F1) and when a button is clicked on this form (call F2), it launches another form.
On F2, I want to set a string via a textbox (and save it to a variable in the class), and then when I close this form, the string will appear in a label in F1.
So I am basically sharing variables between both forms. However, I can't get this to work correctly. How would this code look?
I would add a new property to form2. Say it's for a phone number. Then I'd add a friend property m_phone() as string to form 2. After showing an instance of form2 but before closing it, you can refer to the property m_phone in form1's code.
It's an additional level of indirection from Matthew Abbott's solution. It doesn't expose form2 UI controls to form1.
EDIT
e.g.:
public string StoredText
{
get;
private set;
}
inside the set you can refer to your UI control, like return textBox1.text. Use the get to set the textbox value from an earlier load.
And:
public string GetSomeValue()
{
var form = new F2();
form.ShowDialog();
return form.StoredText;
}
Just ensure that StoredText is populated (or not, if appropriate) before the form is closed.
Are you showing the second form as a dialog, this is probably the best way to do it. If you can avoid doing shared variables, you could do the following:
public string GetSomeValue()
{
var form = new F2();
form.ShowDialog();
return form.TextBox1.Text;
}
And called in code:
Label1.Text = GetSomeValue();
This might not be the most efficient way of approaching, but you could create a class called DB (database). Inside this class, create variables like
public static bool test or public static bool[] test = new bool[5];
In your other forms, you can just create an instance. DB db = new DB(); then grab the information using db.test = true/false. This is what I've been doing and it works great.
Sorry, I'm only like a year late.

Categories