472,951 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Changing properties of form controls

Hi,

I am trying to write a simple application to retrieve data from the
Windows registry and insert it into textboxs on a windows form.

So far I have one namespace containing two classess.

The first class handles the form generation - (this was done using GUI
form designer).

The second class handles the retrieval of the data from the registry.

What I want to be able to do is enter the retrieved data into the form
textboxs. I have tried creating a method to change the value of the
textbox text property in the first class and calling this from the
second class. The code runs but doesn't show the updated text value
although the code is executing as a console writeline in the same
method executes ok.

I'm relatively new to C# but do have some college experience of C++.
Any help would be most appreciated. Any comment on the basic
application structure would also be appreciated.

TIA

Tony Wilkinson
Nov 16 '05 #1
4 2385
Tony W wrote:
Hi,

I am trying to write a simple application to retrieve data from the
Windows registry and insert it into textboxs on a windows form.

So far I have one namespace containing two classess.

The first class handles the form generation - (this was done using GUI
form designer).

The second class handles the retrieval of the data from the registry.

What I want to be able to do is enter the retrieved data into the form
textboxs. I have tried creating a method to change the value of the
textbox text property in the first class and calling this from the
second class. The code runs but doesn't show the updated text value
although the code is executing as a console writeline in the same
method executes ok.

I'm relatively new to C# but do have some college experience of C++.
Any help would be most appreciated. Any comment on the basic
application structure would also be appreciated.

TIA

Tony Wilkinson

Sounds like bad design to me.
Maybe you should have a class that handles getting the keys from the reg
and writing changes back and another (presumably winform) to display and
edit.
In this case, your main class (form) would create an instance of your
reg class and use its methods to retrieve the relevant keys and would
then populate its fields from there.

If you dont want to change...
In your second class, how are you accessing the form.
If you are doing it by creating a new instance, then bear in mind that
this is not the same instance that is displayed.

If you create a singleton form, your method will work as you will always
be accessing 1 instance.

Post back with more details if you need.

JB
Thunderbird and loving it.
Nov 16 '05 #2
Hi,

Thanks for replying to my post. I'm still unsure where I am going wrong so have taken the liberty of posting an edited version of my code below. I
have removed code relating to other controls and registry entries as well as comments etc.

As you can see I have a class called FormUserInfo that handles form creation and changes. I have another class for getting registry data and
manipulating it prior to displaying via the form form the first class.

The settboAppVer method appears to work as the passed string value displays correctly prior to the control property change and the control text value
reflects the new value after changing the value.

The only problem I have is showing the changes on screen.

Once again any advice will be most welcome, I've searched the web and can't find an answer to this anywhere.

Thanks again,

Tony W

// code starts here~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

using System;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Text;

namespace AVMonitor
{
public class FormUserInfo : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox tboAppVer;

public FormUserInfo()
{
InitializeComponent();
}

private void InitializeComponent() {
this.tboAppVer = new System.Windows.Forms.TextBox();
this.SuspendLayout();

// tboAppVer
this.tboAppVer.BackColor = System.Drawing.SystemColors.Control;
this.tboAppVer.Location = new System.Drawing.Point(128, 184);
this.tboAppVer.Name = "tboAppVer";
this.tboAppVer.ReadOnly = true;
this.tboAppVer.TabIndex = 12;
this.tboAppVer.Text = "textBox1";

// FormUserInfo
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(528, 302);
this.Controls.Add(this.tboAppVer);
this.Name = "FormUserInfo";
this.Text = "AVM";
this.ResumeLayout(false);
}

public void settboAppVer(string strAppVer)
{
MessageBox.Show (strAppVer); //displays correct value as passed to method
tboCurrentDatVer.Text = strAppVer;
MessageBox.Show(tboAppVer.Text); //displays correct value as passed to method
}

}

public class AVDetails
{

//? ATTRIBUTES
string strAppVersion;

//?METHODS
public string getAppVersion()
{
//? get the Application version
AVDetails AVD = new AVDetails();
strAppVersion = AVD.getRegistryEntry("<Registry path>", "Version").ToString();

return strAppVersion;
}

public object getRegistryEntry(string strKeyName, string strKeyElement)
{
//? read the registry
RegistryKey pRegKey = Registry.LocalMachine;
pRegKey = pRegKey.OpenSubKey(strKeyName);
Object strKeyValue = pRegKey.GetValue(strKeyElement);

return strKeyValue;
}

public static void Main()
{
string strAppVer;

AVMonitor.AVDetails AVD = new AVDetails();
AVMonitor.FormUserInfo FUI = new FormUserInfo();

//? get the AppVer
strAppVer = AVD.getAppVer();

//? apply values to form
FUI. settboAppVer (strAppVer);
Application.Run(new FormUserInfo());

}
}
}

Nov 16 '05 #3
Hi Tony
Firstly, if you change
Application.Run(new FormUserInfo());
to
Application.Run(FUI);
your program should work.
This is because you are creating an instance of FormUserInfo and setting
the data on it but then showing a new instance that has not had the data
set.

BUT..
You have not got a very good design there.
I would suggest that you move your entry point into FormUserInfo and
then in the load event of your form, create an instance of your
avdetails class and use the methods to load itself.

PSEUDOCODE

public class frmMain : sys.win.forms.form
{
...........
this.load =+ new eventhandler(this.load);

private void load(obj sender, eventargs e)
{
AVDETAILS avd = new AVDETAILS();
this.txtAppVersion.text = avd.getAppVersion();
}
}

Do you know how to use events?
It is better for a form / class to use other classes to load itself than
to rely on them to load it. This allows for easier reuse of the class.

Please see my inline comments.
HTH
JB
Im a Starship Trooper
This is my letter to dad
Transferred from Saigon to Baghdad
And now Im dead --Ozi Batla, Starship Trooper

Tony W wrote:
Hi,

Thanks for replying to my post. I'm still unsure where I am going wrong so have taken the liberty of posting an edited version of my code below. I
have removed code relating to other controls and registry entries as well as comments etc.

As you can see I have a class called FormUserInfo that handles form creation and changes. I have another class for getting registry data and
manipulating it prior to displaying via the form form the first class.

The settboAppVer method appears to work as the passed string value displays correctly prior to the control property change and the control text value
reflects the new value after changing the value.

The only problem I have is showing the changes on screen.

Once again any advice will be most welcome, I've searched the web and can't find an answer to this anywhere.

Thanks again,

Tony W

// code starts here~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

using System;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Text;

namespace AVMonitor
{
public class FormUserInfo : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox tboAppVer;

public FormUserInfo()
{
InitializeComponent();
}

private void InitializeComponent() {
this.tboAppVer = new System.Windows.Forms.TextBox();
this.SuspendLayout();

// tboAppVer
this.tboAppVer.BackColor = System.Drawing.SystemColors.Control;
this.tboAppVer.Location = new System.Drawing.Point(128, 184);
this.tboAppVer.Name = "tboAppVer";
this.tboAppVer.ReadOnly = true;
this.tboAppVer.TabIndex = 12;
this.tboAppVer.Text = "textBox1";

// FormUserInfo
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(528, 302);
this.Controls.Add(this.tboAppVer);
this.Name = "FormUserInfo";
this.Text = "AVM";
this.ResumeLayout(false);
}

public void settboAppVer(string strAppVer)
{
MessageBox.Show (strAppVer); //displays correct value as passed to method
tboCurrentDatVer.Text = strAppVer;
MessageBox.Show(tboAppVer.Text); //displays correct value as passed to method
} Should not need to expose public method like this.
}

public class AVDetails
{

//? ATTRIBUTES Not attributes but fields or private / member variables.

Fields are public varaibles
Private variables are private and not accessible through a property.
Member variables are private but are accessible through property accessors.
string strAppVersion; What is this for?
is it public / private, do you want outside classes to be able to 'see'
this?

You can wrap this in a region such as

#region Member Variables
private int m_MyID;
#endregion

#region Properties
public int MyID
{
get
{
return m_MyID;
}
set
{
m_MyID = value;
}
#endregion

//?METHODS
public string getAppVersion()
{
//? get the Application version
AVDetails AVD = new AVDetails(); You do not need to create an instance here as you already have an
instance (this).
strAppVersion = this.getRegistryEntry("<Registry path>",
"Version").ToString();
or
strAppVersion = getRegistryEntry("<Registry path>", "Version").ToString(); strAppVersion = AVD.getRegistryEntry("<Registry path>", "Version").ToString();

return strAppVersion; }

public object getRegistryEntry(string strKeyName, string strKeyElement)
{
//? read the registry
RegistryKey pRegKey = Registry.LocalMachine;
pRegKey = pRegKey.OpenSubKey(strKeyName);
Object strKeyValue = pRegKey.GetValue(strKeyElement);

return strKeyValue;
}

public static void Main()
{
string strAppVer;

AVMonitor.AVDetails AVD = new AVDetails();
AVMonitor.FormUserInfo FUI = new FormUserInfo();

//? get the AppVer
strAppVer = AVD.getAppVer();

//? apply values to form
FUI. settboAppVer (strAppVer);
Application.Run(new FormUserInfo()); You are creating a new instance that has no relation to FUI, the one you
have previously loaded.
}
}
}

Nov 16 '05 #4
Hi,

Thanks for the input. I have tried the easy suggestion and that has worked for now.

I have a background in VBA and VB so know about re-usable code but not to the same strict rules as C whatever. Different names for roughly the same
things doesn't help either or the same name for completely different things.

I dived into C# as I needed to create a simple app that would run as a service and this is almost impossible to do in VB6.
I have been using some online tutorials to try and get what I needed to sort out my very simple service. I guess it shows.

Do you know of any GOOD online tutorials that go further than single class examples without forms etc that I could use? If not how about a GOOD book
recommendation?
After I've bodged my way through this I'll go back to basics and start learning over again.

Thanks again,

Tony W
Nov 16 '05 #5

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

Similar topics

8
by: deko | last post by:
I'm trying to find a way to set form/control properties programmatically. In a nut shell: 1. Open a database 2. Open a form in design view 3. Do something like this: For Each prp In...
2
by: ColinWard | last post by:
Hi. I have a form which has as its recordsource an SQL string. The SQL String is as follows: SELECT * from CONTACTS where false. this ensures that there is no data loaded in the form when the...
8
by: deko | last post by:
I'm hoping someone can sanity check my understanding of the Object Model for Forms/Controls. I'm having trouble drilling down into Control properties. First, I have a record set with the...
3
by: Gerry Abbott | last post by:
Hi all, Im wondering why the following will work Me.Controls("subnonstorePos").visible = False while Me.Controls("subnonstorePos").AllowEdits = False give me an 'property not support by...
3
by: qwerty | last post by:
I´m new to ASP.Net. My workmate has some experience with it. He claimed that in ASP.Net working with frames is much simpler than it was ASP. I asked explanation but he couldn't give me such. (a...
1
by: Firewalker | last post by:
I am attempting to change the backColor property on the previously instantiated buttons FROM a listbox_doubleClick event. I thought it would be something like this: If...
7
by: Sakharam Phapale | last post by:
Hi All, How to preserve the old font properties while changing new one? I posted same question 2 months back, but I had very small time then. eg. "Shopping for" is a text in RichTextBox and...
32
by: deko | last post by:
I have a popup form with a textbox that is bound to a memo field. I've been warned about memo fields so I'm wondering if I should use this code. Is there any risk with changing the form's...
3
by: Dan Soendergaard | last post by:
I have this code: for (int i = 0; i < this.Controls.Count; i++) { if (this.Controls is ValidationTextBox) { // Change the property of the control here. } }
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

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.