EDIT::
This might be simple for some but right now it has me confused. I am working on a project and when the submit button is pressed,create either a person or an employee based on the above checkbox. Put all of the form data into the class object using either the constructor or properties. Display all of the class information using the ToString method and a messagebox.
My question is::
When it is asking when the submit button is pressed, create a person or an employee based on the above checkbox.Would I use what is above the checkbox or below.
Also to put all of the form data into the class object using either the constructors or properties. I'm Not to sure how to do this.
Display all of the class information using a ToString and a messagebox. I understand how to do a messagebox but not with a ToString.
Now I already have two classes and those names are Members and Employees. Under the Members I have Name, Age, and COB. Under the employees I have Salary and JobTitle. The only time that the salary and jobtitle comes up if the user check the checkbox that says is person employee.
I am sorry if I confuse people I myself is kinda confused with what is being asked. The software I am using is Microsoft Visual C# 2010 Expressed.
The code I have so far don't know if it is right or not:
private void button1_Click(object sender, EventArgs e)
{
Members obj = new Members(); <---This is what is I am assuming being asked when
obj.Name = ""; its says create either a person or an employee
obj.Age = ""; based on the above checkbox.
obj.COB = "";
Employess obj1 = new Employess(); <-- here I am trying to put all of the form
obj1.Salary = ""; data into the class object using either
obj1.JobTitle = ""; the constructor or properties.
Console.WriteLine(obj.ToString());<--- this is the messagebox I am being asked to do its not all the way done.
}
From what I get from your code is you have taken two classes for employees and members and you want to store their information in objects of respective classes based upon your checkbox selection. I suppose you are working in windows forms because you have specified the button_click event.
If that's the case:
private void button1_Click(object sender, EventArgs e)
{
if(checkbox1_Member.Checked==true)
{
Members obj = new Members();
obj.Name = "";
obj.Age = "";
obj.COB = "";
MessageBox.Show(obj.Name+ " :: " +"obj.Age.ToString());
}
else if(checkbox2_Employee.Checked==true)
{
Employees obj1 = new Employees();
obj1.Salary = "";
obj1.JobTitle = "";
MessageBox.Show (obj1.Salary.ToString()+ " ::"+obj.JobTitle.ToString());
}
}
Your question is quite vague but let's make a ton of assumptions:
You have a checkbox you've renamed to 'checkEmployess'. (I'm not sure if you meant 'employee' but let's just go with it.)
You have text boxes on your form for all of the stuff the user has entered with sensible names.
All the input is in text at the moment.
So, you need to check whether the checkbox is ticked/checked with an if statement and create the correct sort of object:
private void button1_Click(object sender, EventArgs e)
{
object dude; // if you use inheritance then this could be of the base class's type
if (this.checkEmployess.Checked)
{
// it's an employess
Employess employee = new Employess();
employess.Salary = textSalary.Text; // this copies the value of the control into your object
employess.JobTitle = textJobTitle.Text; // however for this example we've assumed every control is a text control and your object has only string properties
dude = employess;
}
else
{
// it's a member
Members member = new Members();
member.Name = textName.Text;
member.Age = textAge.Text; // this in particular should be made numeric
member.COB = textCOB.Text;
dude = member;
}
MessageBox.Show(dude);
}
That's the basic object creation done. You could add a ToString method to these two classes to format their properties for display. (This approach is a bit crude but will work so stick with this for now.)
Improvements you can then make are:
Use input controls that are more suitable for the type of input. E.g. numeric controls for numbers, &c.
Use object initializers for constructing these objects.
Use methods to correctly format these two types of objects for display).
Related
The 'Customer' form has a variable called AcctReferenceNbr (Variable that I'm trying to grab shown in yellow) which takes a two-letter abbreviation of the customer name. I am currently editing the Projects form, and I want to use this abbreviation as part of the External Ref. Nbr.
The attached image End Result I'm trying to achieve shows what the end result should look like. The number from the QuoteID is appended to the abbreviation.
I am able to successfully grab the QuoteID as it is part of the Projects table, but I am currently unable to grab the AcctReference Nbr from the Customer table.
I have a RowSelected event on the QuoteID field, which is shown below:
namespace PX.Objects.PM
{
public class ProjectEntry_Extension : PXGraphExtension<ProjectEntry>
{
#region Event Handlers
protected void PMProject_RowSelected(PXCache cache, PXRowSelectedEventArgs e)
{
PMProject row = (PMProject)e.Row;
if (row.ContractCD != null) {
PMProject item = PXSelectorAttribute.Select<PMProject.contractCD>(cache, row) as PMProject;
// The "UP" string is where the abbreviation is supposed to be,
// but I just added two letters to test if the appending works, which it does.
row.ExtRefNbr = "UP" + item.ContractCD;
}
}
#endregion
}
}
What I've tried so far:
Accessing the Customer table namespace to grab the value and pass it to the Projects form, which didn't work because it didn't accept the Customer type in the Projects form.
Adding a PXDefault attribute to the External Ref. Nbr which would try and grab the variable using SQL.
I'm a bit stuck on what else I can try. Any help would be appreciated :)
UPDATED
Below is how I went about trying to grab the AcctReferenceNbr value from the Customer table.
The reason why I tried using the PXSelectorAttribute method was that I added the AcctReferenceNbr as a column to the Quote ID selector (selector is shown in the link above called 'End Result I'm trying to achieve').
So I figured I could try and grab that value in the Customer namespace, as that is where the variable resides, and pass that up to the Project namespace above.
Then, I would call the public method below in the Project namespace to get the required abbreviation:
// instead of this
row.ExtRefNbr = "UP" + item.ContractCD;
// it would be this
row.ExtRefNbr = PX.Objects.AR.CustomerMaint_Extension.getAcctReferenceNbr(cache, e) + item.ContractCD;
namespace PX.Objects.AR
{
public class CustomerMaint_Extension : PXGraphExtension<CustomerMaint>
{
#region Event Handlers
public static string getAcctReferenceNbr(PXCache cache, PXRowSelectedEventArgs e)
{
BAccount row = (BAccount)e.Row;
BAccount item = PXSelectorAttribute.Select<BAccount.acctReferenceNbr>(cache, row) as BAccount;
return item.acctReferenceNbr;
}
}
#endregion
}
}
Is there a proper way to target the actual table?
try this. I haven't tested this but give it a go.
protected void PMProject_RowSelected(PXCache cache, PXRowSelectedEventArgs e)
{
PMProject row = (PMProject)e.Row;
if (row.ContractCD != null && row.CustomerID != null)
{
BAccount ba = (BAccount )PXSelectorAttribute.Select<PMProject.customerID>(cache, row) ;
row.ExtRefNbr = ba.AcctReferenceNbr+ row.ContractCD;
}
}
you certainly don't need to extend the CustomerMaint graph.
I'm using c# and Visual Studio. I want to access a variable from another form. I've found some things like:
textBox1.Text = form22.textBoxt17.Text;
But I don't want to access a textBox value, I just want to access a variable. I've tried this:
string myVar1 = Form2.myVar2;
But that doesn't work.
Any help?
Update
This is what I've got now:
private string _firstName = string.Empty;
public string firstName
{
get
{
return _firstName ;
}
set
{
if (_firstName != value)
_firstName = value;
}
}
In formLogin (where the variable is located), just below public partial class formLogin : Form
Then, later in code, inside button on click event:
OleDbCommand command2 = new OleDbCommand();
command2.Connection = connection;
command2.CommandText = "select firstName from logindb where username = '" + txtUsername.Text + "' and password = '" + txtPassword.Text + "'";
firstName = command2.ExecuteScalar().ToString();
I write this in formAntonyms (from where I want to access the variable) in formLoad event:
formLogin fli = new formLogin();
lblName.Text = fli.firstName;
The problem with all this is that, when formAntonyms opens, lblName is still empty, instead of showing the users name. What am I doing wrong, I've done all the steps right...
You are on the right path, you should not expose controls or variables directly to client code.
Create a read only property in the form/class you want to read the value from:
//Form2.cs
public string MyVar{get{ return textBoxt17.Text;}}
Then, being form22 the instance variable of your already loaded Form2 form class. Then, from any client code that has a reference to it, you can read the value of the property:
string myVal = frm22.MyVar;
EDIT:
Ok based in your last comment, you have a variable in Florm1 and want to access it from Form2, the principle is the same as the previous example, but instead of exposing a control property you now expose a private variable, and instead of living in Form2 it now lives in Form1:
//Form1.cs
private string _myVar = string.Empty
public string MyVar
{
get
{
return _myVar ;
}
set
{
if(_myVar != value)
_myVar = value;
}
}
The property is now read/write so you can update its value from client code
//From From2, assuming you have an instance of Form1 named form1:
string val = form1.MyVar;
...
form1.MyVar = "any";
First of all it is bad object oriented design to access variables from classes directly. It reduces maintainability and reusability.
Your problems arise, because the functionality of your objects is not clear to you.
You should not think in terms of "I want the value of this variable", but in terms of: "Suppose you have a form22, what properties should such a form have?".
Well, apparently it has a size and a position and lots of others, and apparently your form has some information that it displays, and you think that users of this form want to know the text of the displayed information.
Let's suppose the displayed information is named MyInformation. Be aware, that you can only display a description of the information. This descriptive text is not the information itself.
A proper object oriented design of your form would be
class Form22 : Form
{
public string MyInformationText {get {return ...; }
...
}
Now you are communicating to the users of Form22 that a Form22 has some MyInformation. You also communicated that you are not willing to share this information, only to share a descriptive text of the information. Furthermore users of your form can't change this information.
This gives you a lot of advantages. For instance, suppose you don't want to display the text in a TextBox, but in a ComboBox. Or maybe you don't want to display it at all anymore. The users of the form who wanted a descriptive text of MyInformation don't have to change.
Of course your design could be different if you want users of your form to change the information. Probably that would also mean that you need to change the displayed description of your information. But again: users of your form won't need to know this. Again, this gives you the freedom to change it without users having to change:
public MyInformation SpecialInformation
{
get {return this.mySpecialInformation;}
set
{
this.mySpecialInformation = value;
this.UpdateDisplay(mySpecialInformation);
}
}
It depends on your model if you should still provide a property for the displayed text or not: Should all MyInformation object in the world have the same displayed text, or could it be that the displayed text in form22 might differ from the displayed text of MyInformation in form23? The answer to this influences whether MyInformation should have a function to get the descriptive text, or whether the forms should have a function to get the descriptive text.
These are just examples to show that you should think in: what should my object do? What should users of my object be capable to do with my objects. The more you allow them to do, the less you will be able to change. You will have the greatest flexibility if you supply them with no more information than required. Besides you'll need to test much less functionality.
I got stuck here,
i have dynamic table that i want to print. So i make session to pass it into web control.
Unfortunately, it doesn't run smooth.
Here are my code :
protected void bt_print_click(object sender, EventArgs e)
{
StringWriter sw = new StringWriter();
HtmlTextWriter w = new HtmlTextWriter(sw);
panelBilling.RenderControl(w);
string s = sw.GetStringBuilder().ToString();
Session["ctrl"] = s;
ClientScript.RegisterStartupScript(this.GetType(), "onclick", "<script language=javascript>window.open('Print.aspx?rep=1','PrintMe','height=680px,width=1024px,scrollbars=1');</script>");
}
And the Print.aspx.cs code:
protected void Page_Load(object sender, EventArgs e)
{
Control ctrl = (Control)Session["ctrl"];
PrintHelper.PrintWebControl(ctrl);
}
I always got error message :
"Unable to cast object of type 'System.String' to type
'System.Web.UI.Control'."
on
(Control)Session["ctrl"]
part. I have use this method many time and no problems before. Anyone has any idea? Thanks.
This just doesn't make sense, casting a string value to a Control.
As far as I can see, you are not inserting a Control into the Sessiosn, not even the Control.ToString() value, which also wouldn't be working.
Before continuing, it might be usefull to check which type is retrieved from the Session without casting anything (remark: this is only possible with .NET 3.5 or higher):
var sessionValue = Session["ctrl"];
When doing so, you will find out the value is of type object, containing the value:
"<div>\r\n\r\n</div>"
The above output is not a Control, but a string/html value.
There are two solution paths which u can follow:
Change the "SET" call on the Session
Change the "GET" call on the Session
In the second example, you keep to the fact that you are inserting a string and therefor you would like to read out that string value:
string sessionValueAsString = string.Empty;
Object sessionValue = Session["ctrl"];
if (sessionValue != null)
sessionValueAsString = sessionValue.ToString();
The above example could suit ur needs, never the less I'm guessing you are in the need to save an object of type "Control" in the session. This can be achieved like so:
MyControl myControl = new MyControl {Title = "My custom control"};
Session["myControl"] = myControl;
// Read the Session value cross Postback
MyControl mySessionControl = (MyControl)Session["myControl"];
Apart from the above example, I'm not 100% sure what you are trying to do.
My guess is you made a mistake inserting the wrong value in the Session.
One remark: I'm not a fan of storing the WebControls in the session, it would be cleaner to store your data objects in the Session (e.g. store a Customer class instead of the CustomerUserControl, this way you can read out the Session and populate the required controls using the data found in the Session).
I have fixed my form load problem. I changed it to where the main menu wasn't being called on load event and that fixed the issue. Now my retrieve event gets the version but never passes it to my form.
Here is my code for transferring process:
Where the information is being pulled from:
public string VersionPass { get; set; }
VersionPass = rtxtBoxNewVersion.Text;
This is the main menu where the value will be stored till they click the assign button. This is where it gets the value from the form.
public string VersionNum { get; set; }
VersionEditor newV = new VersionEditor();
newV.ShowDialog();
VersionNum = newV.VersionPass;
newV.Dispose();
Form being transferred to I am using form load because the value will not change: It never get the value into the PassedVersion = passedVersion.VersionNum; field.
MainMenu passedVersion = new MainMenu()
string PassedVersion;
private void Notification_Load(object sender, EventArgs e)
{
PassedVersion = passedVersion.VersionNum;
rTxtBoxVersion.Text = PassedVersion;
}
Having to guess a bit here, but ...
1) Take out new MainMenu() from the Notification_Load call. This is probably stopping the dialog being created properly.
2) If you want to share information you either need to pass it to the new object, for example when you create it:
MyObject = new MyClass(SomeObjectToTellItAbout);
// or maybe like this:
MyObject.InterestingInfo = SomeOtherObject;
If it's truely global information (app version, for example) you could make the info to share static (and then access it like this: MainMenu.MyAppVersion).
Edit based on comments:
You want to get an understanding of classes vs objects. A class is just a design; it's a concept. For example "human". An object is an instantiation of that class/concept/design, for example me. And you (another object). I can't find your name by saying h = new human(); h.name() and nor can you find your version by making a new MainMenu and asking it what version it is! Hope that helps.
I have a query regarding maintaining a List in between two windows forms. It's for a project where I need to create an address book.
I have chosen to maintain the contact details in the form of a List. My first windows form (form1) contains a master copy of a list AddressBook, which contains the address book.
I hardcoded 4 entries into the address book list in order to experiment and get the simple functions such as 'add' and 'edit' working.
I have a second windows form called Add, in which I can add new entries to the list. This works fine. I can add a new contact in the ADD form and this shows up in the initial form1, master form.
My problem arises in the EDIT form. I pass the AddressBook (master) list to the EDIT form. The EDIT form takes the master list and I am able to manipulate the records in that list. However when it comes to sending back the new list to the master page (form1), it does not pick it up. I am using the same code as I do in the ADD form which successfully sends back the new list. However this code does not work when sending back an edited list.
Here is my AddressBook property within form1
public List<Contact> addressBook;
public List<Contact> AddressBook
{
get { return addressBook;}
set {addressBook = value;}
}
Within EDIT:
public Edit()
{
InitializeComponent();
temp = Master.AddressBook; // temp is the temporary List I update within EDIT
}
** I then have my algorithm which successfully lets me EDIT the list temp. the list temp now has the edited list**
then when I hit the save button, I use the following code;
Master.AddressBook = temp;
All I need is for the list temp to be sent back to form1.
the code Master.AddressBook = temp; WORKS for when I add values to the list through the ADD form.
ADD FORM:
public Add()
{
InitializeComponent();
temp = Master.AddressBook;
}
**** code to add a new record into the list temp. the new record is called newRecord**********
private void btnAddClose_Click(object sender, EventArgs e)
{
stor.AddressBook = temp; // when I hit close on the form, it updates the master list AddressBook
this.Close();
}
This is all probably very poorly worded but in essence the only bit where my code fails is when I want to change my master Addressbook within form1 by replacing it with the list temp, which is the edited list from my EDIT form.
I think it's something to do with my AddressBook property. But this doesn't explain why I can replace AddressBook with a list containing new records but I can't replace it with a list containing edited records.
One way to accomplish this would be to make the list in Master static.
Master:
public static List<Contact> AddressBook { get; set; }
Note: You do not need the backing variable, and if you do want to use it, best practices would suggest that it be private. If you do decide to use it, it will also need to be static.
In the Add form, you would then gather the data to create a new Contact object and temp should, in fact, be just a Contact object.
Add Form:
private Contact newRecord = null;
public Add()
{
InitializeComponent();
newRecord = new Contact();
}
/**** code to add the user-input to the new Contact object ****/
private void btnAddClose_Click(object sender, EventArgs e)
{
Master.AddressBook.Add(newRecord);
this.Close();
}
Hope this helps.
This is where the Singleton pattern comes in handy: Implementing Singleton in C#
You will notice Application Settings uses this same patttern to allow you to globally access it without having to pass it around.
When I use a Singleton I typically make the class name like (TypeName)Manager (ex: AddressBookManager).
So the class might be something like this:
public static class AddressBookManager
{
#region Singleton
static readonly AddressBookManager instance = new AddressBookManager();
private AddressBookManager(); // prevent creating instances of this
public static AddressBookManager Current { get { return instance; } }
#endregion
AddressBook master = new AddressBook(); // the master address book
public AddressBook Master
{
get { return master; } // get the master address book
set { master = value; } // set the master address book
}
}
Then in each form you would access it like so:
var addressBook = AddressBookManager.Current.Master;
addressBook.Add(newRecord);
The problem you are experiencing with the Edit functionality probably has something to do with the way you are using temporary lists. By using a static, global list and merely adding/editing items inside of it, you don't run that problem. Since your Contact items are a class (not struct), their changes will be reflected in the list automatically since they are reference types.
The great part about Singleton classes is the ability to access them from anywhere in the project. The only caveat is that you need to be extra cautious when working with multi-threaded applications and Singleton classes.