PassingValues from a class to a form

  • Thread starter Thread starter Nick Beenham
  • Start date Start date
N

Nick Beenham

Can anyone give me a tip on how to pass a value from a
class to a windows form.

e.g. adding text to a textbox.

public class AnotherClass
{
//usual class code
public void AddText()
{
Form1.TextBox.Text = "InsertText here";
}
}

It keeps asking for an object reference :(

Hope someone can help :)

Nick
 
Hi Nick,

Your problem is that AnotherClass has no clue on which Form1 (Form1 in your
case is a class type and not an isntance) instance to pass value to.
One way would be:
public void AddText(Form1 f)
{
f.TextBox.Text = "InsertText here";
}
 
Hi,

Yes it should ask for the object reference...

you have several options here

1. Use singleton pattern to allow to access the main form from any where in
your project.
2. pass the main form obejct reference to the other class and use the
reference to do the modification

in any case you have to set the TextBox access level to internal (set the
Property Modifiers : Internal)

But if you feel uncomfortable of doing this drop me a Email..

Nirosh.
 
Thanks for the help guys though now i have another related problem when
trying to AppendText to a multiline textbox.

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred
in mscorlib.dll

Additional information: Index and length must refer to a location within the
string.

This is the error I get when trying to add text to a multiline textbox

Many many thanks

Any ideas on how to correct this
 
Hi,

I think you have not set the multiline property of the textbox to TRUE

but I feel that you need to use a list box and it will better suit your need

Nirosh.
 
Basically what im trying to do is create an on-screen log.
As events are fired between a set of objects I'm trying to get messages
posted up on screen sort of like a log of whats happening.

I was using a multiline textbox for this because it seemed to have the
functionality i required.

A listbox will have a collection and make this more complicated.
Any suggestions??
 
if you feel so then do it this way

set the multiline property to true

then change your method as

Note that I guess you are using singleton

This way it should work

Nirosh.
 
DOH! I was being a quality NUMPTY :( Got it sorted, was an issue with the
singleton and main, thanks for the tips, watch this space for delegate and
event questions next

Nick
 
Back
Top