|
awesome thank you for your help!
|
|
|
|
|
Hi,
I have created a user control with txtFirstName and txtLastName fields on it.
On my frmPassengers, I am populating the user control more than one time based on the number user decided to add like this:
private void frmPassengerDetails_Load(object sender, EventArgs e)
{
this.Cursor = Cursors.WaitCursor;
BaseLayoutItem prevItem = layoutControlPassenger.Root;
for (int i = 1; i < total_adults; i++)
{
ctlPassengers uc = new ctlPassengers() { PassText = "Passenger " + i };
LayoutControlItem item = layoutControlPassenger.AddItem("", uc, prevItem, DevExpress.XtraLayout.Utils.InsertType.Bottom);
item.TextVisible = false;
item.SizeConstraintsType = SizeConstraintsType.Custom;
item.MinSize = new Size(830, 75);
item.MaxSize = new Size(830, 75);
prevItem = item;
}
}
so if user choosed 5 for the total_adult value then the user control will be populated five times.
Now I want to know how can I read the value of every txtFirstName and txtLastName on the form (the five of it for example)..
Technology News @ www.JassimRahma.com
|
|
|
|
|
Add a list to your form (e.g. List<ctlpassengers>) to which you add your user controls as you create them. This avoids having to traverse the visual tree later to get at them.
Once you're ready to read back the name values, iterate over the controls in the previously created list.
|
|
|
|
|
sorry I didn't get you...
Technology News @ www.JassimRahma.com
|
|
|
|
|
txtFirstName and txtLastName are "text boxes" in your "user control"; right?
To access the text box contents (.Text) you need a reference to the users controls and the text boxes; no?
Aren't you creating these "user controls" on the fly and adding them to your (devExpress) form?
If not, what was the purpose of the code you posted?
|
|
|
|
|
You are right in all but in this case I am creating multiple controls from the same user control. The user control has just one txtFirstName but when users sets total_adults=5 in Form1 then five txtFirstName controls will be created on the Form2
Thats why I am asking how can I change the property class to return the value and from Form2 I need when clicking Save to save read the value of all fives txtFirstName controls.
Thanks
Jassim
Technology News @ www.JassimRahma.com
|
|
|
|
|
Add a list to your form:
list<uc> mylist = new list<uc>.
As you create and add uc's to the form, add them to the list also:
mylist.add(myuc)
When you click "save", iterate the list to access controls and their contents:
foreach ( uc myuc in mylist){
.... myuc.txtFirstName.Text ...
etc.
}
One set of controls; multiple "references" to those controls: form; list.
|
|
|
|
|
I think you have to create a method form doing this.
If you have limit for total adults its easy else you have go for Max value.
If you say 10 is the maximum adult capacity.
List<string> lstFirstName=new List<string>();
List<string> lstSecondName=new List<string>();
void RetrieveValues()
{
if(adultCount>0)
{
for(int i=1;i<=adultCount;i++)
{
string first=txtFirstName.Name+i.ToString();
string second=txtSecondName.Name+i.ToString();
lstFirstName.Add((TextBox)first.Text);
lstFirstName.Add((TextBox)second.Text);
}
}
}
This may work. If its not working please reply with the error you are facing.
|
|
|
|
|
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<myControls> lst;
private void button1_Click(object sender, EventArgs e)
{
int max = int.Parse(txtInput.Text);
lst = new List<myControls>();
for (int i = 0; i < max; i++)
{
new myControls();
lst.Add(new myControls());
}
}
private void button2_Click(object sender, EventArgs e)
{
foreach (var item in lst)
{
MessageBox.Show(item.Name);
}
}
}
class myControls
{
public string Name;
public TextBox t;
public Label l;
}
//by default control's modifiers property is set to private. Make it public to access from other classes.
modified 3-Sep-19 21:02pm.
|
|
|
|
|
Hi, I have made a program to compare the pages of 2 books using the overloading concept. But i m not sure that is correct or not. Please see my code and tell me whether my program is logically correct or not based on the concept..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace comp
{
class book1
{
public int page1;
}
class book2:book1
{
public int page2;
public book2(int p1 ,int p2)
{
page1 = p1;
page2 = p2;
}
public static bool operator <(book2 b1, book2 b2) // overloading
{
if (b1.page1 < b2.page2)
return (true);
else
return (false);
}
public static bool operator >(book2 b1, book2 b2)
{
if (b1.page1 > b2.page2)
return (true);
else
return (false);
}
}
class Program
{
static void Main(string[] args)
{
book2 b2 = new book2(5,10); // passing parameters
if (b1.page1 < b2.page2)
{
Console.WriteLine("book 1 has less pages than book 2");
}
else if (b1.page1 > b2.page2)
{
Console.WriteLine("book1 has more pages than book 2");
}
Console.ReadLine();
}
}
}
|
|
|
|
|
Whoa, there's quite a lot which is not right there.
By the look you have overloaded operators to compare books, but you are actually comparing pages!
Regards,
Rob Philpott.
|
|
|
|
|
Hey rob, i thought better to compare the no. of pages of the books .. But if i compile this it wont print the message.( I m new to this overloading concept). What do you suggest how it should be then ??
|
|
|
|
|
OK. Why do you have two book classes, one derived from the other?
In OOD, when you derive something it specializes the thing it derives from. In this case, it adds a second integer member called Page2. What's going on here?
If you can, avoid this as this is just a distraction from what you're trying to do - operator overloading.
Regards,
Rob Philpott.
|
|
|
|
|
As Rob has said, deriving from a base class specialises something - a book does not derive from a previous book except when it is an expanded version: the Directors Cut if you like.
Think about it: a "Book by Charlotte Bronte" derives from "Book", and so does a "Book by Terry Pratchett" - but that is pretty much the only thing they have in common: the elements which are common to all books (Pages, a cover, a title, an author) and so forth.
You wouldn't overload a method to get a number of pages from two different books, because the number of pages is a property of the individual instance on the book. But... you might overload it to get different information which is also instance specific:
public class Book
{
private int pages;
private List<int> chapters = new List<int>();
public int GetPages()
{
return pages;
}
public int GetPages(int chapter)
{
int lastPage = pages;
if (chapter >= chapters.Count || chapter < 0) return 0;
if (chapter != chapters.Count - 1) lastPage = chapters[chapter + 1];
return lastPage - chapters[chapter];
}
}
This has two overloaded GetPages methods.
If you don't supply any parameters it returns the number of pages in the book.
If you pass it an integer, it returns the pages in that chapter.
The type of the parameter determines which version of the method gets called, and thus what the method does.
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
You are right but you need make some modifications to simplify it.
|
|
|
|
|
//PRODUCT CODE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
[Serializable]
public class Product
{
private string ProductID;
private string ProductName;
private float Price;
private int Quantity;
public Product()
{
}
public void Read()
{
Console.WriteLine("Enter Product ID : ");
ProductID = Console.ReadLine();
Console.WriteLine("Enter Product Name : ");
ProductName = Console.ReadLine();
Console.WriteLine("Enter Price : ");
Price = float.Parse(Console.ReadLine());
Console.WriteLine("Enter Quantity : ");
Quantity = int.Parse(Console.ReadLine());
}
public void Show()
{
Console.WriteLine("{0}\t\t{1}\t\t{2}\t\t{3}", ProductID, ProductName, Price, Quantity);
}
}
}
//MAIN PROGRAM CODE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
List list = new List();
int op;
try
{
Stream stream = File.Open("F:\\Product.obs", FileMode.Open);
BinaryFormatter formatter = new BinaryFormatter();
list = (List)formatter.Deserialize(stream);
}
catch (Exception ex)
{
}
do
{
Console.Clear();
Console.WriteLine("1.New product");
Console.WriteLine("2.List All");
Console.WriteLine("0.Exit");
Console.WriteLine("------------------");
Console.WriteLine("Enter Choice : ");
op = int.Parse(Console.ReadLine());
switch (op)
{
case 1:
Product p = new Product();
p.Read();
list.Add(p);
break;
case 2:
Console.WriteLine("Product Details");
Console.WriteLine("--------------------");
Console.WriteLine("ProductID\t ProductName\t Price\t Quantity");
Console.WriteLine("------------------------------------------");
for (int i = 0; i < list.Count; i++)
{
p = list[i];
p.Show();
}
break;
case 0:
Stream stream = File.Open("F:\\Product.obs", FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, list);
break;
}
Console.ReadKey();
}
while (op != 0);
}
}
}
|
|
|
|
|
First, I'm not reading it like that - use the code block (<pre> tags) to preserve the formatting so the code is readable.
Then, explain you problem in reasonable detail in the question, instead of in the title - remembering that we can't see your screen, access your HDD, or read your mind.
Explain what you are having problems with - what the code does and doesn't do, and why. At the moment, you've just dumped some code on us and said "fix this".
Help us to help you!
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
I inherited an app that was created using EF6 - Model First. How do I update the database after making modifications to the tables? If it was code first I could use migrations to handle it, but I'm hesitant to try that with this model.
|
|
|
|
|
I think you need to clarify your terminology.
The "tables" ARE the "database".
Did you modify the "database" and now want to update the "model"? In which case, you open the ".edmx", right-click the design surface, and then click "Update model from database...".
... Backing up your project first, of course.
modified 25-Jul-14 19:49pm.
|
|
|
|
|
Doesn't that drop all the tables and their data?
|
|
|
|
|
Why would you think what I suggested "drops the tables ...".
The "model" is the "code".
"Update model from database..." READS the database schema and updates the "classes" that define the "entities" in the "model".
|
|
|
|
|
I am consolidating about 10 databases into one. I have ran across multiple cases in the older databases where they have a combo-box linked to a database table with under 10 items. What would be the best way to handle the small amount of data ? Do I create a strongly typed array or do I keep the 20 different tables that will never have any data added ?
|
|
|
|
|
Sounds like the tables might be holding enumeration types values. If so, they're probably anchored by foreign keys on other tables. Do they have just two columns, a key and a value?
It's a common problem, because in code you might use an enum to represent these values and you end up in the situation of how to keep the database and code in sync.
You could remove the tables, but that might render the database a bit meaningless without the application tier in place, limiting what could be achieved via a stored proc for instance. You can't really create enum types at runtime, although you could create the same effect using static members on a class.
Probably the best way would be to have them as a collection of key/value pairs.
I can't really offer a definitive answer I'm afraid, but for completeness it might be advisable to keep them.
Regards,
Rob Philpott.
|
|
|
|
|
I have jumped both way in the past, leave multiple emun tables or stick them into a single key/value table. I find the multiple table solution the best mainly for the FK constraint capability.
I also use my code generator to create the enum values in the application in a class called... Enums - go figure!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Mycroft Holmes wrote: I also use my code generator to create the enum values in the application
That has been my solution in the past too!
Avoids all that nasty ORM stuff. Also getting it to do a runtime check that things are aligned is useful, in case someone in their wisdom decides to reassign the enums..
Regards,
Rob Philpott.
|
|
|
|