|
Well, you want to make some sort of a Froms factory!! This is possible, but as Mav said... Why?? Each form has its own special properties to be initialized. Why don't you initialize them at design time once??
Anyway, I'm not in the position to judge or modify your coding style. All I wanted was to guide you to what I think is better than what you want. Trust me, this isn't the reason to make such a factory method.
Anyway, you can do it -like Mav said again- using Reflection namespace.
1- Declare your method as
MyMethod(Type TheForm)
2- Call it as
MyMethod(typeof(FormClassName));
3- In the Form you can call Assembly.CreateInstance() to create your form. Then using this object you can initialize it.
Yet, again... Does it really worth all this??
Regards
|
|
|
|
|
I see. Thanks mav and nader
I really wanted to implement this as part of my UI. I have created an mdi application that mimic the MS MONEY 2006 tab bar
tab ---> HOME BILLING INVENTORY
So if i click on the HOME TAB, it will only load a single form. this is all done in my FrmMain. this is why i have this sub.
private void SelectTabToView(string sWhatTab)
{
switch (sWhatTab.ToUpper()) {
case "HOME":
FrmHome F1 = new FrmHome()
F1.MdiParent = this;
F1.Dock = DockStyle.Fill;
F1.Show();
this.pnlCompositeCon.Controls.Clear();
this.pnlCompositeCon.Controls.Add(F1);
this.txtCurrentLoadedForm.Text = F1.Name.ToUpper();
break;
case "BILLING":
FrmHome F2 = new FrmHome()
F2.MdiParent = this;
F2.Dock = DockStyle.Fill;
F2.Show();
this.pnlCompositeCon.Controls.Clear();
this.pnlCompositeCon.Controls.Add(F2);
this.txtCurrentLoadedForm.Text = F2.Name.ToUpper();
break;
case "INVENTORY":
FrmHome F3 = new FrmHome()
F3.MdiParent = this;
F3.Dock = DockStyle.Fill;
F3.Show();
this.pnlCompositeCon.Controls.Clear();
this.pnlCompositeCon.Controls.Add(F3);
this.txtCurrentLoadedForm.Text = F3.Name.ToUpper();
break;
}
}
So i could have used the function
private void SelectTabToView(string sWhatTab)
{
switch (sWhatTab.ToUpper()) {
case "HOME":
WhatFormToLoad(FrmHome);
break;
case "BILLING":
WhatFormToLoad(FrmBilling);
break;
case "INVENTORY":
WhatFormToLoad(FrmInventory);
break;
}
}
This is much easier than typing all those redundant settings
Thats why i have decide to create a generic routine or a form factory to catter the loading of forms by just passing the form name.
If im not on the right path in doing this please advice on what to do
Maybe i was just looking how to hammer a nail with a jackhammer
|
|
|
|
|
Well, you can use generics;P
Or much simpler, this:
private void SelectTabToView(string sWhatTab)
{
switch (sWhatTab.ToUpper()) {
case "HOME":
{
FrmHome frm = new FrmHome();
WhatFormToLoad(Frm);
break;
}
case "BILLING":
{
FrmBilling frm = new FrmBilling();
WhatFormToLoad(Frm);
break;
}
case "INVENTORY":
{
FrmInventory frm = new FrmInventory();
WhatFormToLoad(Frm);
break;
}
}
}
private void WhatFormToLoad(Form MyForm)
{
MyForm.MdiParent = this;
MyForm.Dock = DockStyle.Fill;
MyForm.Show();
this.pnlCompositeCon.Controls.Clear();
this.pnlCompositeCon.Controls.Add(MyForm);
this.txtCurrentLoadedForm.Text = MyForm.Name.ToUpper();
}
Regards
|
|
|
|
|
Thanks for the Prompt reply, nader
I was about to go on this direction. As i was playing with the Refactoring menu
anyway thanks for the input its highly appreciated
|
|
|
|
|
hey whats up? i need help! i query a database off a web site and i onnly want a few fields, this is in c#..
this db has thousands of records which is why i need to code this. this is just to make a contact list.
what i want is:
Pastor/Leader: John Cowan
Contact Info: phone: 916-387-9750
Mailing Address: 7322 Folsom Blvd, Sacramento, CA 95826 USA
Website: http://www.calvarysac.org/
html is:
body {margin ; font-family:"lucida grande", tahoma, arial, verdana, sans-serif; font-size:10px}
label {display: block; font: normal 10px/16px "lucida grande", tahoma, arial, verdana, sans-serif; margin: 0; padding: 0px 0; color: #666;}
.textfield {font: normal 11px/13px "lucida grande", tahoma, arial, verdana, sans-serif; padding:2px; margin: 0; border: 1px solid #ccc; background-color: #eee; width: 100%; color: #666;}
.textfield2 {font: normal 11px/13px "lucida grande", tahoma, arial, verdana, sans-serif; padding:2px; margin: 0; border: 1px solid #ccc; background-color: #eee; width: 207px; color: #666;}
#contact td {padding:5px;}
#contact table {width: 100%;}
h1 {margin ; padding ; font-size:20px; margin-bottom:7px}
div.hr {height: 2px; background: #ddd; margin:10px 0 7px 0 }
div.hr hr {display: none;}
a {color:#374872}
a:visited {color:#374872}
a:hover {background:#ddd}
#verified p span {padding:3px}
/* Images */
img {border: none;}
p.img {margin: 0; padding: 0;}
p.img img {float: right; margin: 0 0 1em 1em;}
p.img a img {padding: 3px; border: 1px solid #c4c4c2;}
p.img a:hover img {border: 1px solid #949492;}
Calvary Chapel Sacramento
Pastor/Leader: John Cowan
Contact Info:
phone: 916-387-9750
facsimile: 916-387-9752
Mailing Address:
7322 Folsom Blvd.
Sacramento, CA 95826 USA
Meeting Address:
7322 Folsom Blvd.
Sacramento, CA 95826 USA
★ View Map
Website: http://www.calvarysac.org/
//<![CDATA[
if(typeof sIFR == "function"){
sIFR.replaceElement(named({sSelector:"h1", sFlashSrc:"/assets/swf/avenir.swf", sColor:"#374872", sLinkColor:"#000000", sBgColor:"#FFFFFF", sHoverColor:"#CCCCCC", nPaddingTop , nPaddingBottom , sFlashVars:"textalign=left&offsetTop=0"}));
};
//]]>
|
|
|
|
|
Hello. Is it possible to create controls from a textfile?
For example, my text file contains:
Panel pNew = new Panel();
pNew.BackColor = System.Drawing.SystemColors.ControlLightLight;
pNew.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
pNew.Location = new System.Drawing.Point(3, 5);
pNew.Name = "panel1";
pNew.Size = new System.Drawing.Size(368, 408);
pNew.AutoScroll = true;
How do I go about it?
|
|
|
|
|
Hello
Well, to some extent Yes! It's possible, but depends on:
1- What are you storing in your text file? Are you storing other parameters like the location, size, etc...??
2- Do you know exactly what you are storing in the text file? ie. All the controls hve the same format, or do some controls have more properties stored than others?
3- Are all controls of the same type, or different types?? Do you know all the types you are declaring? Will there be UserControls, or controls unknown at design time involved?
Regards
|
|
|
|
|
The text file contains all the controls and it is of different types.. Some controls have more proerties stored and, yes I'm storing the location too..
Is there a command that executes this (the creation of controls?)?
Thank you!!
|
|
|
|
|
Well, one easy clumsy way -if you know all the types of controls you will declare- is to parse the text file and Create the controls in a switch. eg.
switch (MyInputType)
{
case "TextBox":
{
TextBox temp = new TextBox();
MyForm.Controls.Add(temp);
}
}
This way the temp variable will be destroyed, but the reference in the MyForms.Controls will keep the control alive. This way you can declare as many TextBoxes as you want.
If you expect UserControls or controls that you don't know its type, you'd have to use Reflection namespace which is by far bigger than to be covered in a single post.
PS.
Why use TextFile?? Why not XmlDocument?
Regards
|
|
|
|
|
I just started using C# in VS2003 (after learning vb.net)
When I type a variable name in VS2003 for a vb.net project - it corrects any capitalization for me.
Is there a way to do that for C# projects in VS2003 ?
Thanks.
|
|
|
|
|
Unlike VB, in C# variable names are case sensitive, so no. Otherwise how could you define a myVariable and a MyVariable? (not saying that that is a good idea mind).
|
|
|
|
|
Well thats my point - I dont want to so why not help me out a little on the capitalization ?
|
|
|
|
|
As it's case-sensitive it can't help you out in the way you want. However, in VS 2005 the IntelliSense is a bit more intelligent in the following way.
Suppose you write
int customer = 5;
Then you start typing CUs
Then IntelliSense will pop up and automatically select the customer declaration. If you tab then it will substitute customer for CUs.
But if you were to fully type CUstomer then it won't auto-correct it afterwards. In theory, though it could make an inference based on the fact that the only "CUstomer" that's been declared in scope is "customer."
Anyway, if you're in a position to try using C# 2005. It's a much nicer editing experience. As it happens, at work I'm using neither C# 2003 nor 2005 but VB 2003.
C# 2005's IntelliSense and code snippet behaviour is better than VB 2005's BTW.
Kevin
|
|
|
|
|
The development of an application that handles encryption (AES) of data stored in a database (SQL server 2005) at the application level through C#. So in practice I would in C# do some simple data handling and then encrypt these data and then insert these data in the corresponding DB tables. This is what I am trying to develop, but I am just in a researching face for the moment, hence my question.
Can anybody reply me on the how difficult this should be to accomplish and where to concentrate my researching and if any body as any form of input to this I am very pleased to receive this too.
Thank you
Kenn Kikkenborg
|
|
|
|
|
i use a class writen by Peter Huber, for pop3 client but i have a problem with it
I cannot establish a connection throw a proxy but when i use direct connection "not firewalled or proxy environment" its work good , are there any changes must done on this method ( connect() )???? or any help please:
public void Connect() {
if (pop3ConnectionState!=Pop3ConnectionStateEnum.Disconnected &&
pop3ConnectionState!=Pop3ConnectionStateEnum.Closed &&
!isTimeoutReconnect) {
CallWarning("connect", "", "Connect command received, but connection state is: " + pop3ConnectionState.ToString());
} else {
//establish TCP connection
try {
CallTrace(" Connect at port {0}", port);
serverTcpConnection = new TcpClient(popServer, port);
} catch (Exception ex) {
throw new Pop3Exception("Connection to server "+ popServer + ", port " + port + " failed.\nRuntime Error: "+ex.ToString());
}
if (useSSL) {
//get SSL stream
try {
CallTrace(" Get SSL connection");
pop3Stream = new SslStream(serverTcpConnection.GetStream(), false);
pop3Stream.ReadTimeout = readTimeout;
} catch (Exception ex) {
throw new Pop3Exception("Server " + popServer + " found, but cannot get SSL data stream.\nRuntime Error: "+ex.ToString());
}
//perform SSL authentication
try {
CallTrace(" Get SSL authentication");
((SslStream)pop3Stream).AuthenticateAsClient(popServer);
} catch (Exception ex) {
throw new Pop3Exception("Server " + popServer + " found, but problem with SSL Authentication.\nRuntime Error: " + ex.ToString());
}
} else {
//create a stream to POP3 server without using SSL
try {
CallTrace(" Get connection without SSL");
pop3Stream = serverTcpConnection.GetStream();
pop3Stream.ReadTimeout = readTimeout;
} catch (Exception ex) {
throw new Pop3Exception("Server " + popServer + " found, but cannot get data stream (without SSL).\nRuntime Error: "+ex.ToString());
}
}
//get stream for reading from pop server
//POP3 allows only US-ASCII. The message will be translated in the proper encoding in a later step
try {
pop3StreamReader= new StreamReader(pop3Stream, Encoding.ASCII);
} catch (Exception ex) {
if (useSSL) {
throw new Pop3Exception("Server " + popServer + " found, but cannot read from SSL stream.\nRuntime Error: " + ex.ToString());
} else {
throw new Pop3Exception("Server " + popServer + " found, but cannot read from stream (without SSL).\nRuntime Error: " + ex.ToString());
}
}
//ready for authorisation
string response;
if (!readSingleLine(out response)) {
throw new Pop3Exception("Server " + popServer + " not ready to start AUTHORIZATION.\nMessage: " + response);
}
setPop3ConnectionState(Pop3ConnectionStateEnum.Authorization);
//send user name
if (!executeCommand("USER "+ username, out response)) {
throw new Pop3Exception("Server " + popServer + " doesn't accept username '" + username + "'.\nMessage: " + response);
}
//send password
if (!executeCommand("PASS " + password, out response)) {
throw new Pop3Exception("Server " + popServer + " doesn't accept password '" + password + "' for user '" + username + "'.\nMessage: " + response);
}
setPop3ConnectionState(Pop3ConnectionStateEnum.Connected);
}
}
|
|
|
|
|
I want to display doc(MS Word) file. I have used rich text box control but it is not working. I want to show graphics (circles....) in my appliction. Is rich txt box is right choice or.... . Tell me about it. Please give me code example if possible.
Thanks
Syed Shahid Hussain
|
|
|
|
|
Hello Shahid.
You can't display Word Documents on your forms using RichTextBox, because it won't understand the formtting of the document.
First of all, check this article[^].
If it's not what you want, you'd have to use somthing called Word Interop -shorthand for Inter operability-
The namespace is Microsoft.Office.Interop.Word , but first you have to reference to it:
In the solution explorer =>Right click on your project's name => Add reference => COM tab => Microsoft Word object library
Important note:
This will require that MS Word is installed on your machine and the user's machine
and the office programming section[^] in the articles may be useful.
Best Regards
|
|
|
|
|
Very Thanks Brother.
God Bless You
Syed Shahid Hussain
|
|
|
|
|
HI,
I learned how to make events using delegates. And, to my understanding, all the events must be made using the control's existing prime events. i.e, If I want to trigger an event for a textbox whenever an user type 'A', i will have to use text_Changed event and check if the text == 'A'. But my question is, How can I create a prime event for a control. Is it the only way to use Timer with a very small timespan and check if the control's property changed and trigger event ?
I have an user control (HTML Editor Control) which doesn't have documentHTML_Changed event. I want to create this event and I did not find any supporting prime eevent to know when exactly the documentHTML property has been changed for that HTML editor. Thats why I was thinking what is the solution for creating this kind of event other than using TIMERs ? To me, TIMERs are the worst solution as they are always running and eating CPU resources for nothing.
Regards,
Emran
|
|
|
|
|
bashiwala wrote: I have an user control
If you create a user control you can add whatever events you want and raise them whenever you want. What's the problem?
|
|
|
|
|
Hello, Thanks for reply.
Say for example, web browser control has a property named DocumentText but it doesnot have any event DocumentText_Changed. Now, is there any way to know when the DocumentText has been changed other than using Timer and check every milli second if the DocumentText is not equal to the last value ..etc.. . If I have some supporting events, then I can make custom event based on the supporting event, right ? But I was wondering how to make the core event like TextChanged, EnableChanged, etc for a control which doesn't have these events defined !
Thanks again
Emran
|
|
|
|
|
Hello Emran
Sure there is a way of making your own events. In your custom class declare your public event like this:
public event HtmlDocumentChange OnHtmlDocumentChange;
HtmlDocumentChange is your handler's delegate.
Then you have to fire/raise the event by calling that delegate:
OnHtmlDocumentChange();
This is the very basic principle of events. The entire issue is when to know that the document has changed thus firing the event is specific to your contorl. If you are inheriting from WebBrowser control, you'd have to handle all events that might change the document -eg. Navigated, DocumentCompleted, DocumentTitleChanged, etc..-, then check the current document against the old one somehow. Depending on the changes you want to track you can store a backup copy of the current document in a private member to compare against, or just store the part you are tracking. When you sense a change in the document call the OnHtmlDocumentChange() delegate.
PS.
Using Timers is not bad.. It's disastrous way of firing event.
Regards
|
|
|
|
|
Is there a way to know where a browser is headed from a C# based application? i would like to be able to know what pages and files have been downloaded from a C# application, if there is a better way to do it using just the internet conection and not a browser that would be better. Please, help.
Victor Bello
you are what you research. VB
|
|
|
|
|
i've been trying some stuff, can anybody tell me how to use the SocketInformation.ProtocolInformation on C#? i can't get the socket to work.
VB
|
|
|
|
|
method #1: create a windows hooker to intercept the events of the browser
can be quite messy.
method #2: create a http sniffer (plenty of samples on this website)
|
|
|
|
|