By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,654 Members | 1,816 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,654 IT Pros & Developers. It's quick & easy.

Changing properties of form controls

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.