I'm really confused by now.
I've got a WinForm which holds a large Array
int[,] map = new int[1000, 1000];
I've also got a class containing a method "Draw".
The draw method now needs to get the value of a position in the array in the form. I tried doing the following thing:
In my Form class, I added
public int mapContentAtXY(Point mapPosition)
{
return map[mapPosition.X, mapPosition.Y];
}
Now if I try to perform
myInt = Ingame.mapContentAtXY(myPoint);
//Note: Ingame is the name of my Form
It says
Error 2 'Neu.Ingame' does not contain a definition for 'mapAtPositionXY'
This is really confusing, I just added that definition, it's also set as public. So why the hell doesn't it work?
You will need to pass the instance of the Form to the draw-class by using this when you create or call that instance of the draw-class. Then you can access that method through that form instance.
OR
Store the map not in the form, but in that draw-class. Move the mapContentAtXY method to the draw-class. Use the one instance of the draw-class in your form to update that map.
Your method mapContentAtXY is an instance method.
Change Ingame to this, if you're calling it from within your form instance.
myInt = this.mapContentAtXY(myPoint);
Otherwise use the form instance
myInt = frmInGameInstance.mapContentAtXY(myPoint);
Related
I have one textbox1 in form1, I have another class called ClassDemo(), this class does not have a form. I have one method in this class, called HelloWorld() inside hello world I have one string val variable.
I want to pass the textbox1 value to ClassDemo's variable when the HelloWorld(), method is called.
How can I do that?
You can try multiple ways here.
Make the method as parameterized method. Put a string parameter inside the method declaration and then whenever you call that method, pass that string value that you want to pass.
Store the string value in global variable or Session(only if needed). This wasy, your value can flow across the application and can be accessed in any area wherever its needed.
Pass by Value or Pass by reference. You can opt for either one in C# to pass variables/values across connected areas.
Hope this clears the idea.
I am really confused and hope somebody will be able to help me with the issue I'm having. I want to use the GET command to obtain a value from a new Form, but my code is overwriting the parameters I pass in the constructor and I am not sure why. Not very familiar with C#.
Here the script I use when I click on a specific button. It is a new Form where I pass into parameters a list of interfaces (the parameters will be modified and I do not want to):
private void btn_t1_Click(object sender, EventArgs e) {
InterfaceT1 Formulaire_T1 = new InterfaceT1(**this.Liste_T1**);
if (Formulaire_T1.ShowDialog() == DialogResult.OK) {
//I WOULD WANT TO USE THE GET COMMAND HERE ONLY IF I CLICK 'OK' ON THE FORM
}
Formulaire_T1.Dispose();
}
Here is the constructor of my Form Formulaire_T1 for reference:
public InterfaceT1(List<T1> Liste_T1) {
InitializeComponent();
this.Liste_T1s = new List<T1>(Liste_T1); //suggested, does not change anything
UpdateView(0);
}
The methods I use in Interface_T1 modify the Liste_T1s but why it is also changing Liste_T1 in the main function? I do not return any value. It seems those value are now linked? I know it must be a simple thing but can't figure it out.
The reason your method in InterfaceT1 modifies the list that you pass in is that the constructor stores the reference to the list, rather than copying it. Change the constructor as follows to fix this:
public InterfaceT1(List<T1> Liste_T1) {
InitializeComponent();
this.Liste_T1s = new List<T1>(Liste_T1); // Make a copy
UpdateView(0);
}
Note that this change would make a copy of the list itself, but not its elements: they would remain the same. Adding / deleting elements from the copied list will be OK, but modifying individual elements will be visible in the original. To overcome this problem, change the code as follows:
public InterfaceT1(List<T1> Liste_T1) {
InitializeComponent();
this.Liste_T1s = Liste_T1.Select(t => new T1(t)).ToList();
UpdateView(0);
}
In the code above I am assuming that a new instance of T1 can be created by calling a "copy" constructor that takes another instance of T1, and copies its fields.
List is a reference type. That means that if you pass it as a parameter into another method, you're not passing a copy of the list, you're passing a reference that points back to the original list.
Pretty short question: How can I find out what is the name of the main form object?
I want to know this, because I want to call some of the functions or get some of the variables from the main form object. Of course, I need to know the name of the object to do so (is this a good idea?).
You can use the Application.OpenForms property to get all open forms, and the get the Name from that. As noted by DaveShaw in the comments, the main form is often the first one in the list, at index 0.
string name = Application.OpenForms[0].Name;
what do you mean?
1. the name of the type:
class form1
{}
and it returns "form1"
2.the name of the object
class form1
{}
form1 obj
and it returns "obj"
I'm trying to pass values between a few winforms, I've got a total of 6 winforms, that the user will cycle through. I'm passing values between the forms using TextBox and Label Controls.
When I open the Primary winform, then click a button to load the second winform, everything works fine (I can pass values to the First Form). My problem is that once I direct the user to another form and this.Hide(); the current (2nd Winform) then try to use the Third form to pass values to the first, I get the following error:
Object reference not set to an instance of an object.
I'm confused because the control that the should be passing the value is passing the value to the first Form isn't NULL
I'm using the same code to connect all the forms together.
public MainForm MainForm;
Then I'm trying to pass the values like so:
MainForm.textBox1.Text = txt_FileName.Text;
Note: All the TextBox and Label controls that are passing values between the forms are public
Anyone run into this? Or any Ideas?
.
You need to make sure that all your forms are instantiated (through new MyForm1()...). Just declaring a variable of type MainForm won't create a form instance - you'll have to do it. My guess is that one of your forms is not created yet when you try to access a control.
This is yet another reason to not to use public controls (see my comment too), since the lifetime of your controls are tied to the lifetime of your form. It's better to hide controls from public access and send data to the form through data objects - the form will set all those values to its own controls. This also makes validation a lot easier, since a control's value can only be set to values allowed by the form. If you set control values from the outside, you'll have a tough time validating them in all scenarios.
I assume you're trying to use modal forms that work similar to a wizard where users go from one form to the next, following a clear path. If so, you can do something like this:
// Data class to set data in Form2
internal class Form2Data
{
public string Name;
...
}
...
internal class Form2 : Form
{
public static DialogResult ShowDlg ( Form2Data oData )
{
Form2 oFrm = new Form2 ();
oFrm.SetData ( oData );
DialogResult nResult = oFrm.ShowDialog ();
if ( nResult == DialogResult.Ok )
oFrm.GetData ( oData );
return ( nResult );
}
private void SetData ( Form2Data oData )
{
// Set control values here
}
private void GetData ( Form2Data oData )
{
// Read control values here
}
}
...
// You call this as such:
Form2Data oData = new Form2Data ();
oData.Name = "...";
DialogResult nResult = Form2.ShowDlg ( oData );
// after the call, oData should have updated values from Form2
if ( nResult == DialogResult.Ok )
{
// show your next form in a similar pattern - set up data
// call form's static method to pass it and then wait for
// the form to finish and return with updated data.
}
You'd have to use a similar pattern in your other forms, too. This does require more work since you need to set up a different data object for all the forms but this way you can easily do validation before and after the form is shown (in SetData and GetData). It also encapsulates your program better, since controls are not accessible from the outside.
.Net 2.0 and later has a feature for windows forms called the "default instance", where it gives you an instance with the same name as the type. The purpose of this feature is for compatibility with code migrated from old vb6 apps. If you're not migrating from an old vb app, it's usually better to avoid the default instances. They will get you in trouble, such as you have now. Instead, create a variable to hold form instances you construct yourself.
You should pass the value by using the instance value of the form.
for example:
SecondForm secForm2 = new SecondForm();
secForm2.textBox1.Text = txt_FileName.Text
so if you pass the value from SecondForm to ThirdForm:
ThirdForm thiForm = new ThirdForm();
thiForm.textBox1.Text = textBox1.Text
How can I update and get values in a Windows Forms application while moving one form to other form (like cookies)?
I need to update the values to some variable and again I am going to refer stored values and need to do some calculations.
I have used cookies in ASP.NET but I am not able to find out the same concept in .NET Windows Forms (C#).
How can these issues be resolves?
You can use object references.
You can decalre a read/write Property for each variable you want to be available in another form and the use them for sharing your data.
One way to do this is to declare variables to be public, either in a global module or in any form.
public x as double
If it is declared in a module, you can access it with the variable name only. To access data declared in another form, use that form name with the variable: form1.x = 7
Another way is to declare a property in a form or other class.
A really simple way of getting cookie-like functionality would be to declare a static string dictionary in Program (Program.cs)
public static System.Collections.Specialized.StringDictionary SortOfLikeCookies = new System.Collections.Specialized.StringDictionary(); and read/write string values using Program.SortOfLikeCookies["Name"] = "Value";