473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Control Object in second form

32
Hi,

I am currently learning c#.net. the following command to retrieve data from another component on another form:
to change it's value and stuff..

In vb.net I would use
lblMax.Text = frmMain.txtMax.text - to get the value
frmMain.txtMax.text = 1234 - to change it's value.

I am trying to find out how I do this in C#.
when I writing it using C# (with c# syntax) I'm getting the following errors:
"An object reference is required for the non-static field, method, or property"
I have changed to object (textbox) to public and the modifier to public as well.

Thanks....
Jul 31 '09 #1
3 3494
ThatThatGuy
449 Expert 256MB
Actually the way of accessing values that you are using is not appreciated...
you're declaring a static member of your the form.......your form becomes dependent on each other and needs to be changed if included in different projects...

it cuts out the flexibility of your application to be used in several projects..
And doesn't really follow the Resuability concept of object oriented programming...

But i've got a better a better way to do it....

Using your examples....here's what i got for you....

There are 2 forms Form1 and Form2...
lblMax is in Form1
txtMax is in Form2
Form1 acts as the Main UI or main Form
Form2 acts as a dialog where settings need to be done so that it could reflect in Form1....

So what you would do here is put up a lauch button you can call it as settings
to open Form2...and when data is enetered in Form2 and Form2 is closed the data in lblMax in Form1 will get updated to whatever is entered in txtMax in Form2....

code of Form1
this code should be written at button click event
Expand|Select|Wrap|Line Numbers
  1. private void Button1_Click(object sender,EventArgs E)
  2. {
  3. Form2 frm2 =new Form2()'
  4. frm2.ShowDialog(); // show Form2
  5. lblMax.Text=frm2.MaxValue; //getting the value of txtMax via a Form2 property after the Form2 gets closed...
  6. }
  7.  
code of Form2
this should be wriiten at OK button click event
Expand|Select|Wrap|Line Numbers
  1. private void btnOK_Click(object sender,EventArgs E)
  2. {
  3. _maxVal=txtMax.Text;
  4. }
  5.  
this code should be declared as a class property in Form2
Expand|Select|Wrap|Line Numbers
  1. private string _maxVal="";
  2. public string MaxValue
  3. {
  4. get{return _maxVal;}
  5. }
  6.  
Now that's the most correct solution.....you'd ever find....and show it to those personnel who recommended you to code like that....

Why did i explained you these because...accessing values to memebers like that is not a good practice....and if you go on programming like that ...you'll land up in trouble wh en youre into an industry....
And if you want some help about good programming issues you can download my ebook which is written by me of course...which is exclusively for students...
here is the link...
http://thatthatsite.yolasite.com/
Reply me if this book was useful....
Jul 31 '09 #2
liadmz
32
I really appreciate you help...
I have downloaded your book and I replay to you soon..

Thanks again.
Jul 31 '09 #3
JnrJnr
88
Is MaxValue a declared variable?

Hi there ThatThatGuy Ive also got more or less the same problem.
Im righting in Visual C# and want to carry out commands for controls(buttons,labels) from form2 onto form1.
For instance: I want to to use form1 as my main form...form1 must have a label and form2 must have a button.
When the button on form2 is clicked I want the label on form1 to be disabled.
I just want o know how to interact with controls on different forms.



Your help would be much appreciated.
Oct 9 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: MP | last post by:
I have a main form that has a subform which also has a subform: the main form is the first subform is the second subform is When I click on the button »AddNewSubSubRecord« (add a new record...
9
by: Guy | last post by:
I have extended the datetimepicker control to incorporate a ReadOnly property. I have used the new keyword to implement my own version of the value property, so that if readonly == true then it...
1
by: Kris van der Mast | last post by:
Hi, been a while since I posted a question myself instead of trying to help others out. I'm refactoring an existing web app that uses dynamic loading of user controls and a lot of...
1
by: Steve Booth | last post by:
I have a web form containing a button that when selected adds a user control to a place holder. The user control contains a button. The first time the user control is added the contained button...
3
by: VB Programmer | last post by:
I have a SIMPL HTML form that displays a javascript clock. I want to take this component and put it in a web user control so that I can reuse it easily. The problem is I don't know what to do...
8
by: Arpan | last post by:
Consider the following code snippet (my main intention is to display the current time in a Label control as & when this ASPX page is accessed/refreshed): <script runat="server"> Class Clock...
5
by: rn5a | last post by:
Consider the following user control which resides in Address.ascx: <script runat="server"> Public Property Address() As String Get Address = txtAddress.Text End Get Set(ByVal value As String)...
9
by: timnels | last post by:
I have an issue where I have a user control that is launched into a floating form. At some point later, I allow the user to "unfloat" the user control by reparenting it on a split container in...
3
by: Joseph Gruber | last post by:
Hi all -- I have two questions. First, I'm adding a control to my form at runtime and the control/form seems to ignore the anchor property of the runtime control. Any idea how I can get a...
5
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hallo, I have a radiobuttonlist control that is added on a custom Web User Control. This control has a property that exposes the SelectedIndex property of the embedded radiobuttonlist. When...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.