|
Access to Form1 data label1.Text?
using abc;
namespace WindowsForm
{
class Form1:Form
{
private label Label1;
}
}
using Form1;
namespace abc
{
class classtest
{
public method1
{
Form1.label1.Text="text";
}
}
}
modified 29-Oct-14 11:45am.
|
|
|
|
|
What happens when you try it?
|
|
|
|
|
You seriously need to read this[^].
You import namespaces, not classes (expect in special cases). Your "using" statement is worthless and should be removed.
Also, Your class files shouldn't know ANYTHING at all about a Form. Your data should be stored in classes and the forms should reference that data, not the other way around, like you're doing.
modified 29-Oct-14 12:03pm.
|
|
|
|
|
Yes, but has to be some ways to get access from child to parent in OOP.
|
|
|
|
|
That's actually a violation of OOP principles. No "child" object should know anything about its parent container. Doing so very tightly couples the two classes together to the point that they cannot be used separately in other projects.
|
|
|
|
|
Okay, if the question you are asking is why can you not access the Text property of Form1.label1, there are many problems with your code. Some are code bugs, while others are architectural. Let's start with the code bugs in your example:
1. Form1 is not public. This means that it will not be seen outside the WindowsForm namespace.
2. Even if Form1 was public, you haven't instantiated it anywhere.
3. Assuming you manage to instantiate Form1, Label1 is private, so cannot be accessed outside of Form1.
4. You have a mismatch between the case of Label1 and label1.
5. Assuming that Form1 and Label1 were public, Label1 has not been instantiated, so it cannot be used anywhere as the Windows Forms Label control (I'm guessing that's what this is), is not a static class. So, you would need to instantiate it.
6. method1 is missing the scope and parentheses.
Okay, now let's get to the big architectural issue.
You are attempting to manipulate a control on another form from method1. This is a big OO violation. You aren't just exposing the text property of the label, you're exposing all the other properties that the Label has made public. The standard way to deal with this would be to expose something inside Form1 that can be used to set this value without exposing the control that's consuming it. You might end up with something like this:
namespace WindowsForm
{
public class Form1 : Form1
{
private Label Label1;
public string LabelText
{
get { return Label1.Text; }
set { Label1.Text = value; }
}
}
}
namespace abc
{
class classtest
{
public void method1()
{
Form1 form = new Form1();
form1.LabelText = "text";
}
}
} By the way, it's a good idea to rename your controls to be something more meaningful.
|
|
|
|
|
Pete O'Hanlon wrote: Form1 is not public. This means that it will not be seen outside the WindowsForm namespace.
Technically, that should be outside of the assembly which contains the class. Other classes in a different namespace within the same assembly would still be able to access it.
However, that's probably a bit too advanced for the OP at the moment.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Problem with access from child ro parent class has to have some solution. Especially in multy file project
|
|
|
|
|
Thank you. I've got it. Actually excellent involving and understanding.
modified 30-Oct-14 11:01am.
|
|
|
|
|
Good for you. That's excellent.
|
|
|
|
|
Now its working, but don;t change label1.Text. I think problem is in second instance of Form1. label1 is changing only second instance. But shown are first instance and label1 is in first instance.
Problem 2 - Two side or class on class declaration.
PS. Namespaces are in different files in project. New file is included in project with "Add exist element" and use with "using" with both side declaration.
using NamespaceTest;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string LabelText
{
get { return Label1.Text; }
set { Label1.Text = value; }
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Button was pressed");
ClassTEST d = new ClassTEST();
d.Changelabel();
}
..
And added file
using System;
using WindowsFormsApplication3;
namespace NamespaceTest
{
public class ClassTEST
{
public void Changelabel()
{
Form1 form = new Form1();
form.LabelText = "text";
}
}
modified 30-Oct-14 12:30pm.
|
|
|
|
|
Post this section of code please.
|
|
|
|
|
I fix it. It's working, but still no result, don't change label1.Text.
But look how strange are both classes calls -on each other class calls.
modified 30-Oct-14 12:47pm.
|
|
|
|
|
The issue here is that you are creating a new instance of Form1 in your Changelabel method. So, you have one instance of the form which allows the user to press the button. They then press it and it creates an instance of ClassTEST. In that class, you create a new instance of Form1 and you call LabelText on that new instance (which won't be visible because you aren't showing it). If you want to work with the original instance, you're going to have to change a couple of things:
public void Changelabel(Form1 form)
{
form.LabelText = "text";
} and
private void button1_Click(object sender, EventArgs e)
{
new ClassTEST().ChangeLabel(this);
}
|
|
|
|
|
YES!Works.Many thanks.Second week i was searching way to do it.
PS.But i suspect problem is not fully fixed for all cases.
|
|
|
|
|
|
In example child ->parent i found parent-> child. So there are 2 examples parent->child
|
|
|
|
|
The DGV is bound to a table. I have an inserted TextBoxColumn which is meant to display values from a related table.
I have my grid displaying nicely, with the empty text box column.
I have the values to insert into the grid.
But when do I do it? With which event handler?
I saw a suggestion to use DataBindingsComplete, but the column hasn't been inserted by the time that is called.
EDIT: Just had a thought. Don't auto-generate the columns. Manually create all the columns. Then add the rows, one-by-one, populating the columns as I go.
|
|
|
|
|
Nigel Mackay wrote: With which event handler? Depends on "how" you'd want to do so. One option would be the CellFormatting[^] event. Get's called whenever the value of a cell is needed. Check if the cell is in the last column, and if so, fetch the value - otherwise, display the default.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Yes, that would work. In fact it rings a bell from many years ago, along with CellValueNeeded and CellValuePushed.
Because I only have 2 bound and 1 unbound column and the grid is Read Only I created the grid manually. That way I can populate the grid while it is being built.
|
|
|
|
|
I am using c# to read and write my computer's parallel port. I am using a software named "winlpt" to manage it, and it works fine. Even I have attached an LED to my LPT1 port and it turns on if a use this software. But the issue I am facing is through programming. I am using inpout32.dll and also inpoutx64.dll but I can read my port using the address 888 but can not write to it with any value like 0 - 255 or even in binary form. My led do not lights up when I start my program. I have seen several samples on line all with similar code which I have and also downloaded any tried few of those demo projects but still not getting my led top anybody.
The c# code I am using:
PortInterop.cs file:
using System;
using System.Runtime.InteropServices;
public class PortAccess
{
[DllImport("inpout32.dll", EntryPoint = "Out32")]
public static extern void Output(int adress, int value);
[DllImport("inpout32.dll", EntryPoint = "Inp32")]
public static extern int Input(int adress);
}
Program.cs file (giving three different output values):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace leds
{
class Program
{
public static void Main(string[] args)
{
int address = 888;
int value;
value = PortAccess.Input(address);
Console.WriteLine("Input Value: " + value);
//FF or 255 D0-D7 on
PortAccess.Output(address, 255);
Console.WriteLine("\nOutput Value: 255");
Console.ReadLine();
//FF or 255 D0-D7 on
PortAccess.Output(address, 200);
Console.WriteLine("Output Value: 200");
Console.ReadLine();
//FF or 255 D0-D7 on
PortAccess.Output(address, 100);
Console.WriteLine("Output Value: 100");
Console.ReadLine();
//00 or 0 D0-D7 off
PortAccess.Output(address, 0);
Console.ReadLine();
}
}
}
And I am using windows 8.1, does it even works on win7++ OS?
Please help anybody.
Salman Malik
modified 29-Oct-14 8:41am.
|
|
|
|
|
obviously the hardware seems to work if 'winlpt' can light the LED - but you post no code, so we couldn't tell if anything looks wrong or not - I'd go back to your question/edit it, and post the smallest portion of code that you can, that illustrates you attempting to write to the port
also, if 'winlpt' is open source or has any useful notes, you might be able to glean how it works and apply that to you program
|
|
|
|
|
I have edited my post, can you help me now?
|
|
|
|
|
|
I have downloaded and tested this one but nothing seems to work
|
|
|
|