473,785 Members | 2,219 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PassingValues from a class to a form

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.T ext = "InsertText here";
}
}

It keeps asking for an object reference :(

Hope someone can help :)

Nick
Nov 15 '05 #1
7 1358
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";
}
--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com

"Nick Beenham" <ni**********@b lueyonder.co.uk > wrote in message
news:07******** *************** *****@phx.gbl.. .
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.T ext = "InsertText here";
}
}

It keeps asking for an object reference :(

Hope someone can help :)

Nick

Nov 15 '05 #2
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.

"Nick Beenham" <ni**********@b lueyonder.co.uk > wrote in message
news:07******** *************** *****@phx.gbl.. .
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.T ext = "InsertText here";
}
}

It keeps asking for an object reference :(

Hope someone can help :)

Nick

Nov 15 '05 #3
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.Argumen tOutOfRangeExce ption' 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
"Nick Beenham" <ni**********@b lueyonder.co.uk > wrote in message
news:07******** *************** *****@phx.gbl.. .
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.T ext = "InsertText here";
}
}

It keeps asking for an object reference :(

Hope someone can help :)

Nick

Nov 15 '05 #4
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.

"Nicholas Beenham" <ni**********@b lueyonder.co.uk > wrote in message
news:Ox******** ******@TK2MSFTN GP11.phx.gbl...
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.Argumen tOutOfRangeExce ption' 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
"Nick Beenham" <ni**********@b lueyonder.co.uk > wrote in message
news:07******** *************** *****@phx.gbl.. .
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.T ext = "InsertText here";
}
}

It keeps asking for an object reference :(

Hope someone can help :)

Nick


Nov 15 '05 #5
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??
"Champika Nirosh" <cn*****@textce ntric.lk> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
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.

"Nicholas Beenham" <ni**********@b lueyonder.co.uk > wrote in message
news:Ox******** ******@TK2MSFTN GP11.phx.gbl...
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.Argumen tOutOfRangeExce ption'

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
"Nick Beenham" <ni**********@b lueyonder.co.uk > wrote in message
news:07******** *************** *****@phx.gbl.. .
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.T ext = "InsertText here";
}
}

It keeps asking for an object reference :(

Hope someone can help :)

Nick



Nov 15 '05 #6
if you feel so then do it this way

set the multiline property to true

then change your method as
> public class AnotherClass
> {
> //usual class code
> public void AddText(string myText)
> {
> Form1.getTheMai nForm().statusT ext.AppendText( myText +"\r\n");
> }
> }
Note that I guess you are using singleton

This way it should work

Nirosh.

"Nicholas Beenham" <ni**********@b lueyonder.co.uk > wrote in message
news:e5******** *****@TK2MSFTNG P11.phx.gbl...
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??
"Champika Nirosh" <cn*****@textce ntric.lk> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
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.

"Nicholas Beenham" <ni**********@b lueyonder.co.uk > wrote in message
news:Ox******** ******@TK2MSFTN GP11.phx.gbl...
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.Argumen tOutOfRangeExce ption'

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
"Nick Beenham" <ni**********@b lueyonder.co.uk > wrote in message
news:07******** *************** *****@phx.gbl.. .
> 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.T ext = "InsertText here";
> }
> }
>
> It keeps asking for an object reference :(
>
> Hope someone can help :)
>
> Nick



Nov 15 '05 #7
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
"Champika Nirosh" <cn*****@textce ntric.lk> wrote in message
news:O0******** *****@tk2msftng p13.phx.gbl...
if you feel so then do it this way

set the multiline property to true

then change your method as
> > public class AnotherClass
> > {
> > //usual class code
> > public void AddText(string myText)
> > {
> > Form1.getTheMai nForm().statusT ext.AppendText( myText +"\r\n");>> }
> > }
Note that I guess you are using singleton

This way it should work

Nirosh.

"Nicholas Beenham" <ni**********@b lueyonder.co.uk > wrote in message
news:e5******** *****@TK2MSFTNG P11.phx.gbl...
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??
"Champika Nirosh" <cn*****@textce ntric.lk> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
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.

"Nicholas Beenham" <ni**********@b lueyonder.co.uk > wrote in message
news:Ox******** ******@TK2MSFTN GP11.phx.gbl...
> 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.Argumen tOutOfRangeExce ption'
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
> "Nick Beenham" <ni**********@b lueyonder.co.uk > wrote in message
> news:07******** *************** *****@phx.gbl.. .
> > 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.T ext = "InsertText here";
> > }
> > }
> >
> > It keeps asking for an object reference :(
> >
> > Hope someone can help :)
> >
> > Nick
>
>



Nov 15 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
17576
by: Jon Hyland | last post by:
Ok, I'm a little rusty on this, it should be a simple problem but I can't figure it out. How can I handle form events in my main code page?? I'm creating a Windows App in C#. Rather than make my main form the startup object, I'd rather put my Main() function in a class or code file. Why? Because thats what I always do in standard C++ or VB. In my opinion, writing all my application logic in the form itself is sloppy and only for...
3
1537
by: John A. Prejean | last post by:
This one has me stumped. I have a base form I am trying to wrap up, but I have one problem. In two functions I am opening a "record detail" form. I would like to keep the code in the base form and pass info to the form telling it which from to actually open. Any ideas how to do this? Here is an example of what I had in mind if the text isn't clear enough... Base Form Function EditRecord() Dim frmRecord as New Form()
19
1981
by: hamil | last post by:
I have a form with one button, Button1, and a Textbox, Textbox1 I have a class, class1 as follows. Public Class Class1 Public DeForm As Object Sub doit() DeForm.Textbox1.text = "It works" End Sub End Class
13
537
by: genetic.error | last post by:
I'm moving from Vb6 to VB.Net. I have a feeling this has come up before... The VS.Net MSDN file seems to state that the following should work: Form1.Show Form1.Visible = True Form1.Hide Form1.Visible = False Load (Form1)
5
1902
by: timothy.pollard | last post by:
Hi I'm having a bit of bother trying to make a questionnaire do what I want it to. I have put it up on www.web-iq.co.uk/test.htm. Basically the user of the final form (when I've tarted it up) will have to choose 1 answer per line, which are added up in the totals below. I've made four separate forms each of which totalises the number of checked elements in it - ie. column one is its own form, similarly column 2 etc.
10
7070
by: menashay | last post by:
Hello, I am absolute beginner in C# so pardon me if the following question is too easy. I have a form with a one button. When I click the button I want to display a message that reads "Button was clicked" However, I do not want to handle the click event from that form, instead I want to "transfer" that event to a "controller" class that shows that message.
16
4174
by: Mike | last post by:
Hi, I have a form with some controls, and a different class that needs to modify some control properties at run time. Hoy can I reference the from so I have access to its controls and therefore being able to modify its properties?
6
2273
by: Thom Little | last post by:
Using C# 3.5 I have a form that calls many other sub-forms. Typically there will be five forms open at the same time. If the main form is closed all the sub forms are also closed. Is there a standard way to gain control within the sub-forms to do individual clean-up prior to their removal?
3
2213
by: ibeehbk | last post by:
Hi. I have a form made in xhtml. I test via vbscript to make sure none of the fields are empty and properly formatted (ie email). All the regular fields work. However, I have two drop down menus that are SELECT elements (javascript-- one named arrivalcity and one named returncity). Basically they ask for a departure and return city and another menu appears with options based on that city. I can't seem to get the input from these fields in the...
0
9646
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9483
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10346
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9956
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8982
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5386
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5514
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4055
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2887
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.