|
Why on earth are you interacting with a console application from your WPF application? It sounds like you're really making problems for yourself.
|
|
|
|
|
Pete O'Hanlon wrote: Why on earth are you interacting with a console application from your WPF application? It sounds like you're really making problems for yourself.
I started with console, i'll move it to class library, and eventually i'll add a dll as a reference.
|
|
|
|
|
Do it right the first time and it will save you considerable work and problems.
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Fair enough. That clarifies things a lot - for a moment, I was worried that you were asking questions in the console app. Anyway, Luc's suggestion is as reasonable as any other you're likely to get and the syntax is nice and straightforward.
|
|
|
|
|
igalep132 wrote: is it possible to fire events between threads ?
No.
Event handlers are executed on the thread that raise the event.
Although... what about avoiding cross-thread WinForms calls in events?...
|
|
|
|
|
i can`t make it in textbox
but in masked textbox i make the mask "00\0000" but user can enter month greater than 12 so i want to restrict this
or make valdation type the mask follow so user can`t enter > 12 or <1 or > this year
how ?
or can i make new type ("mm\yyyy")
|
|
|
|
|
Rather than a MaskedTextBox, why not use a DateTimePicker? That way the date is always valid, and it is obvious regardless of locale.
If you can't, then you would be best to use a ValidatingType property[^]
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
Hello Mohamed,
I don't know if it would fit in your scenario, but I'd advise you to try to use a datetime picker where Format property is set to custom.
Cheers
Manfred
|
|
|
|
|
thnx to all
ok .... how to create new type in C#
|
|
|
|
|
The maskedtextbox is described here[^], and it's usage is explained with a nice example at the bottom of that page. You'd set the mask to "00/0000" according the example, but I bet that it takes somewhat more customizing than merely changing the mask.
Mohamed Nabawy wrote: can i make new type ("mm\yyyy")
You could create a new class that encapsulates both a month and a year. The current edit-controls would not know how to interact with your class, unless you also create a TypeConverter .
I suggest you keep it simple and put two textboxes on that form; limit the keys that can be entered to numbers. Done
I are Troll
|
|
|
|
|
I'm trying to make a simple web browser with tab support and am having difficulty with getting pages to load in the right tabs.
I can make new windows and tabs and switch between them, however the web page typed in the address bar always loads in tab 0. I've never really used tab controls before and I'm kind of learning as I go.
I think I need somehow to almost dynamically name each new instance of webBrowser so they can be accessed individually but I don't know how to go about something like that.
Here's what I've got so far
private void toolStripTextBox1_KeyPress(object sender, KeyPressEventArgs e)<br />
{<br />
if (e.KeyChar == 13)<br />
{<br />
int currentTab = tabControl1.SelectedIndex;<br />
MessageBox.Show(currentTab.ToString());<br />
webBrowser1.Navigate(toolStripTextBox1.Text);<br />
}<br />
}<br />
<br />
private void Form1_Load(object sender, EventArgs e)<br />
{<br />
webBrowser1.Navigate("http://www.google.co.uk");<br />
}<br />
<br />
private void newWindowToolStripMenuItem_Click(object sender, EventArgs e)<br />
{<br />
Form nextWindow = new Form1();<br />
nextWindow.Show();<br />
}<br />
<br />
private void newTabToolStripMenuItem_Click(object sender, EventArgs e)<br />
{<br />
TabPage nextTab = new TabPage("Google");<br />
tabControl1.TabPages.Add(nextTab);<br />
tabControl1.SelectedTab = nextTab;<br />
WebBrowser webBrowser1 = new WebBrowser();<br />
nextTab.Controls.Add(webBrowser1);<br />
webBrowser1.Dock = DockStyle.Fill;<br />
webBrowser1.Navigate("http://www.google.co.uk");<br />
<br />
<br />
}
Thank you for any help
|
|
|
|
|
Hi,
here are some thoughts:
1.
seems you have a TabControl with one TabPage+WebBrowser added in Visual Designer; and inside newTabToolStripMenuItem_Click() you have code to add a TabPage+WebBrowser at run-time. This makes your first TabPage technically different from the future tab pages, not a very good idea:
Right now the first tab page has its WebBrowser kept in a variable webBrowser1 which is a class member of your Form; it is therefore the one that gets used in toolStripTextBox1_KeyPress(), no matter which TabPage is currently visible. The extra tab pages have a local variable, also called webBrowser1, but that only exists in the scope of newTabToolStripMenuItem_Click().
2.
almost all web browsers (e.g. FireFox) support tabs, and show a single TextBox for entering a URL; however the content depends on which TabPage is current. There basically are two ways to achieve this:
(a) really have a single TextBox and switch its content whenever the tab pages get switched;
(b) have a TextBox for each TabPage, all at the same Location, however all but one invisible. This might be easier.
3.
This is what I would do (assuming a Form called MyBrowser):
- with Visual Designer add a TabControl without Tab Pages to MyBrowser; and an invisible TextBox for URLs ("tbDummyURL").
- create a "BrowserPage" class that holds everything about a single tab page, including a TextBox "tbURL" and a WebBrowser "browser";
- create a variable MyBrowser.currentBrowserPage that holds the current browser page (initially null);
- create a method BrowserPage MyBrowser.AddTab ; it would instantiate and return a BrowserPage, add its TabPage to the TabControl, set its TabPage.Tag to itself, and set its TextBox.Location to tbDummyURL.Location without making it visible.
- create a method MyBrowser.ShowTab(browserPage) ; it would set currentBrowserPage.tbURL.Visible false (unless null) and browserPage.tbURL.Visible true.
Then at run-time probably immediately create and show the first tab; whenever some button gets clicked create and show an additional tab; etc.
refinement: if you have another button to close the current tab, you need to remove the TabPage, and show again the previous tab. The proper way would use a most-recently-used approach, which you can implement with a List<BrowserPage> in MyBrowser. Then ShowTab(browserPage) should remove browserPage from the list, and add it again, effectlively moving it (if it was already in the list) to the last position in the list. So, closing a tab would remove it from the list, and the now last entry, if any, should be shown.
This may be a bit much, I suggest you try it step by step. Ask again if you get stuck (but please use PRE tags, not CODE tags, for code snippets).
|
|
|
|
|
Thank you, that makes a lot more sense now, should be easy enough to change what's affected by the address bar with a tab index changed event or similar.
I'll let you know how I get on,
Thank you
|
|
|
|
|
you're welcome.
|
|
|
|
|
Well I am sad to say that I have usually avoided inheritance because I don't have a full understanding of it and/or I haven't yet found a good use for it.
So my newest learning project is a client / server (have been posting about it you may notice alot). A brief overview of what I have been doing is passing my custom object over TCP. My custom object is a seperate DLL project.
Erik didn't like it but to be honest I haven't came up with a better way or fully understood. AFter reviewing hie article that he pointed me to I'm just wondering if I should be using a interface and/or inheritance.
Here is my Commons object:
[Serializable]
public class Commons
{
public Commons()
{
TimeStamp = DateTime.Now;
}
public enum Tasks
{
CHECKIN,
DISKDRIVE,
DRIVEINFO,
FAN,
MESSAGE,
PHYSICALMEMORY,
PRINTERS,
PROCESSES,
REGISTER,
SERVICES,
SOFTWARE,
ADD_CMD,
CLOSECD,
GET_ALL,
REBOOT,
OPENCD,
SHUTDOWN
}
public Tasks Task { get; set; }
public Tasks AddTask { get; set; }
public DateTime TimeStamp { get; set; }
public string AgentId { get; set; }
public string Netbios { get; set; }
public string IpAddresses { get; set; }
public string PhysicalAddresses { get; set; }
public string Message { get; set; }
public int LocationID { get; set; }
public Software[] Software { get; set; }
public Services[] Services { get; set; }
public DiskDrive[] DiskDrive { get; set; }
public DrivesInfo[] DriveInfo { get; set; }
public Memory[] Memory { get; set; }
public Printers[] Printers { get; set; }
public Processes[] Processes { get; set; }
public Fan[] Fan { get; set; }
}
Now those other classes look like this:
[Serializable]
public class Printers
{
public string PrinterName { get; set; }
public string PaperSizes { get; set; }
public string PrinterResolutions { get; set; }
public bool SupportsColor { get; set; }
public bool IsDefaultPrinter { get; set; }
public bool CanDuplex { get; set; }
public bool IsPlotter { get; set; }
}
So getting an idea of what I'm trying to do, I am wondering if a situation like this will best serve using a interface and/or inheriting a class? I get the idea, you implement a interface, you inherit a class.. but like I said I haven't put together a good use for something I'm trying to do.
Now what I am doing with this data once it reaches the server is inserting it into SQL. If a client gets the TASK == PRINTERS, then it will perform a lookup using WMI of all local printers, poopulate the Printers[] array and send back to the server. So server gets the TASK == PRINTERS and see the Printers[] array is populated and proceeds to insert that data into SQL.
Sorry if this question is kind of broad.
|
|
|
|
|
Jacob D Dixon wrote: I get the idea, you implement a interface, you inherit a class..
So you understand more than many here.
In most cases, I define an interface and an abstract class to act as a base for other classes. However, maybe you don't need a hierarchy of classes at all. I was playing with WCF last spring and didn't need a hierarchy of classes. With a Web Service, your client usually makes calls to different methods of the server, rather than sending different commands to one method.
I guess I'd need a higher-level overview of what you're trying to accomplish to give a better answer. The big question is... Is this an actual thing you need? Or merely an exercise to learn new things?
|
|
|
|
|
PIEBAL,
Everything I do is just an exercise to learn new things. I'm not currently a programmer or in school for doing this stuff. (I am in school for programming but they teach you very basic crap, and nothing like this... at least the school I'm in).
I'm currently a Network Engineer and setup this software called LabTech. It is a remote monitoring / control application. You have your server out on the internet, and install agents to different locations (different networks). They report stuff like hardware, processes, services, and more. I know that it is made with VB.net and possibly some C#.
Anyways I just wanted to try to make my own applicaton that was similiar. So far I have my server and agent. They communicate and transfer data (my Commons object) over TCP (Socket Class).
The server holds in memory "pending commands". With each Commons object is arrays, Agent ID, and other things (you can see this class in the first post). So basically we are just transferring that back and forth. So when the agent checks in, the server looks in memory (a List<commons>) to see if there are any Commons object that pertains to that agent (by AgentID.. GUID).
So lets say I want a certain agent to get a list of processes and return to server. I manually add (for right now) a commons object to the List on the server.
The agent checks in.
Server queries the List<commons> object for objects that match that Agent's ID.
Server returns array of Commons[] object (because it might have more than one command) to client
Client performs the commands
Client returns array of Commons[] object to server
Server loops through and inserts the data into SQL.
So if we are wanting the agent to get its Services and send to data we will set the Commons Object TASK to Services and the client will populate the Services[] array (Which is just another class that holds stuff like ServiceName, Status, etc).
For this exercise I rather not use a web service. I wanted to learn more about TCP protocol. The server is using the Socket class asyncronous and the client is not. Here is a post where Luc helped me out with my server's Socket implementation:
http://www.codeproject.com/Forums/1649/Csharp.aspx?fid=1649&fr=151#xx0xx[^] (this is working fine).
Hope that helps you understand more of what I'm doing
Note:
And of course I can always send you my project if you wanted lol.
modified on Saturday, December 4, 2010 11:22 PM
|
|
|
|
|
Oh, yeah, OK. Personally I'd use WCF, but learning sockets can be good too (I have done very little of it). A more advanced technique would be to have the communication layer abstract, perhaps with a plug-in*, so the client and server don't actually know or care how the communication is performed (as long as they are both configured the same).
I'll re-read the other post and see if I can think of anything else.
* Which also usually uses an interface and maybe an abstract base class.
|
|
|
|
|
I think using a web service would make the task easier. I just want to get a handle on Sockets and a good "design" of a client / server model. Erik also suggested the same thing as you about the communication layer. Saying that the server should handle anything thrown at it pretty much. I just haven't been able to wrap my head around his suggestions on how to make it work for me for this particular project. I mean technically right now it does handle everything. It detects if it is a Commons[] object, and if it is then does something with it. If it isn't a Commons[] object then it just does nothing with it.
But the way I have it designed.. any future updates would require the server to be updated (which would mean the agents couldn't check in at that time), and the agents to be updated depending on what is being added. Like if I wanted to make v2 be able to reboot the agents to safe mode, then most likely it would require a server & agent upgrade. I just don't see how to get around doing this task without an upgrade later on.
I hope that makes sense. I guess I'm just looking for pointers on making my classes more "professional" and easier to manage. I mean I think they are easy to manage because I built them lol.
|
|
|
|
|
The socket-oriented communication to a third-party system that I had to do on my last job was very simplistic (circa 1980s?). It was strictly ASCII text -- the first two characters were the number (type) of message, the next four characters were the length of the message, followed by the data of the message. I used an enumeration (as you did) to hold the message types, but I had no need of defining a hierarchy of message classes.
I just took a quick look at my code and I see that I defined one class, which derives from EventArgs; it is used for sending and receiving the messages. Received messages are passed up the chain by events. Sent messages are passed along to the log via events. Otherwise I probably wouldn't defnie a class at all. The Windows Service that performs this communication merely reads new messages from a table, sends them, and marks them as sent; and reads messages from the socket and inserts them into the database (marked as received). Other Services handle entering messages to send and dealing with responses.
|
|
|
|
|
I've been thinking a lot about this and looking at some of the other post. In my situation I am leaning towards leaving it the way it is. (As in not using inheritance). I mean if you really think about it, any update would require the server to be updated along with the agent on the systems. I don't see a way around this.
I do like the idea of sending the four characters to specify the type of message that was being sent.
|
|
|
|
|
Have your client code use reflection and generics to dynamically invoke your object's methods:
<br />
#region Libraries<br />
using System;<br />
using System.Collections.Generic;<br />
using System.Reflection;<br />
#endregion<br />
<br />
namespace Utilities<br />
{<br />
public class DynamicMethodInvocation<T, R><br />
{<br />
public R Invoke(string MethodName, IDictionary<string, object> methodParameterValues)<br />
{<br />
return Invoke(MethodName, methodParameterValues, null);<br />
}<br />
<br />
public R Invoke(string MethodName, IDictionary<string, object> methodParameterValues, object[] constructorParameters)<br />
{<br />
T instance = (T)Activator.CreateInstance(typeof(T), constructorParameters);<br />
MethodInfo mi = instance.GetType().GetMethod(MethodName);<br />
if (methodParameterValues != null)<br />
{<br />
ParameterInfo[] parameters = mi.GetParameters();<br />
object[] values = (object[])Array.CreateInstance(typeof(object), parameters.Length);<br />
foreach (ParameterInfo parameter in parameters)<br />
{<br />
values[parameter.Position] = methodParameterValues[parameter.Name];<br />
}<br />
return (R)mi.Invoke(instance, values);<br />
}<br />
else<br />
{<br />
return (R)mi.Invoke(instance, null);<br />
}<br />
}<br />
}<br />
}<br />
Kevin Rucker, Application Programmer
QSS Group, Inc.
United States Coast Guard OSC
Kevin.D.Rucker@uscg.mil
"Programming is an art form that fights back." -- Chad Hower
|
|
|
|
|
Hello ,
While I use a different coding platform, I've worked a bit with inheritance and client/server applications. The examples described below is pseudocode (okay, I lie, it's a hack of mostly Delphi mixed with pseudocode, so sue me :P), so you should be able to make a decision as to whether you would want to use inheritance or not.
Your particular scenario involves transporting data between a client and a server. What data is being transported (for argument's sake) isn't actually what's important, how you *interpret* the data, on either side of the protocol, is. The core functionality of your base class involves just putting the data into a stream and reading the data from a stream. Nothing else. You don't want your objects to do more than it needs to. The specialisation of each (derived) class will contain the specific enhancements that you would want from it. So for my example, you have your class defined like so:
TYourBase = class(TObject)
protected
procedure WriteToStream(AStream : TMemoryStream); virtual;
procedure ReadFromStream(AStream : TMemoryStream); virtual;
public
function ToStream : TMemoryStream;
procedure FromStream(AStream : TMemoryStream);
end;
With your implementation for this class looking something like this:
procedure TYourBase.WriteToStream(AStream : TMemoryStream);
begin
end;
procedure TYourBase.ReadFromStream(AStream : TMemoryStream);
begin
end;
function TYourBase.ToStream : TMemoryStream;
begin
Result := TMemoryStream.Create;
WriteToStream(Result);
end;
procedure TYourBase.FromStream(AStream : TMemoryStream);
begin
ReadFromStream(AStream);
end;
Now for the inheritance part. Let's say that your derived class needs to contain a few accessors (a string, an integer and a float). You would define it something like this:
TSomeDescendant = class(TYourBase)
protected
procedure WriteToStream(AStream : TMemoryStream); override;
procedure ReadFromStream(AStream : TMemoryStream); override;
public
SomeString : String;
SomeInteger : Integer;
SomeFloat : Real;
end;
With the implementation looking something like this:
procedure TSomeDescendant.WriteToStream(AStream : TMemoryStream);
begin
inherited WriteToStream(AStream);
AStream.WriteString(SomeString);
AStream.WriteInteger(SomeInteger);
AStream.WriteFloat(SomeFloat);
end;
procedure TSomeDescendant.ReadFromStream(AStream : TMemoryStream);
begin
inherited ReadFromStream(AStream);
SomeString := AStream.ReadString;
SomeInteger := AStream.ReadInteger;
SomeFloat := AStream.ReadFloat;
end;
Now, when you want to read or write from a stream, all you will need to do is something like this:
var
LSomeObject : TSomeDescendant;
LStream : TMemoryStream;
begin
LSomeObject := TSomeDescendant.Create;
LSomeObject.SomeString := 'test';
LSomeObject.SomeInteger := 1;
LSomeObject.SomeFloat := 5.5;
LStream := LSomeObject.ToStream;
FreeAndNil(LSomeObject);
LSomeObject := TSomeDescendant.Create;
LSomeObject.FromStream(LStream);
end;
This is just one way in which you can use inheritance to simplify your streaming of data. To put this into an even broader context. You can define virtual methods in your ancestor class(es), to do things like validate the class or anything you want. Bearing in mind your example, you could define a virtual method to persist the data. That way you can define descendants that don't necessarily persist your data to SQL server, it could persist to anything you like: XML, Excel, [insert your favourite persistance flavour here].
I hope this helps you understand a bit more about how inheritance can help you build your class(es) to do as little as it possibly needs to do, while still allowing it to function correctly. By defining virtual and abstract functions and methods, you allow your derived classes to do what it needs to do, specific to itself. Where you define your printer class, you can specifically stream your data for that class. If you define several other classes, you can define specific reading and writing to the stream. This is how you provide polymorphic behaviour to your objects.
My example of writing and reading from a memory stream may not pertain to your example specifically, so I'll leave you with this final story: One of the more classic 'real life' examples of polymorphic behaviour is the question "What makes a saw a saw?". Programmaticaly, If you define a class called TSaw with a (virtual) method called Cut. The TSaw object itself does not know what to do when it needs to Cut. But you can define TRipSaw and THackSaw descendants which overrides the Cut method. They are still both TSaw descendants (and for all intents and purposes, they are still both saws), but they Cut differently. You wouldn't use a THackSaw to Cut wood, nor would you use a TRipSaw to Cut metal, but they both Cut and they are both saws... ...
Okay, I'd better stop this monologue and let you decide from there ...
Cheers,
Glen Vlotman
|
|
|
|
|
Inheritance should be used sparingly in business applications. In reality, in terms of these applications, inheritance has limited value compared to the internals, military, and scientific sectors.
Developers who use inheritance for the most part use it unnecessarily add a lot of ambiguity to the systems they are working on. Nonetheless, there are reasonable situations where inheritance can be of value; the most important being where you have numerous different types of constructs that basically belong within a same set of data (ie: security types; employee types).
That being said here are a few axioms that you should always follow when using inheritance as described by Tom Patton a number of years ago in his second edition of his COM+ Component programming manual:
1) Never use more than three levels of inheritance. Once this rule is broken inheritance hierarchies tend to become too complicated for easy maintenance.
2) Never use a "Protected" access modifier for a variable or method implementation. This attribute has been the bane of many inheritance hierarchies causing
them to fail. The reason being is that they tend to break such hierarchies when updated during maintenance.
3) Test your inheritance hierarchy thoroughly to make sure that it works in all scenarios as expected.
4) Test your inheritance hierarchy for its level of "black-box reuse". The more an inheritance hierarchy can be used without any understanding of anything
but the public API, the more concrete your structure will be. Most inheritance hierarchies tend to fall in the "white-box reuse" range meaning that there
are higher levels of maintenance required, which is not what you want.
5) If you find that your hierarchy is causing more issues than warranted either redesign it or drop the implementation all-together. It will only get worse as
it enters a production mode.
Steve Naidamast
Black Falcon Software, Inc.
blackfalconsoftware@ix.netcom.com
|
|
|
|
|
Err ... I could go a lot about the pros and cons of inheritance and interfaces. Good and bad design is all a matter of taste anyway. Or to put it another way, no two programmers will ever agree. What I will say is it is important to be consistent, at the very least, and if you find you are posed with these types of questions then in all likelyhood you haven't done enough preparation in your design.
I'll point you in the direction of a book called "Head First Design" patterns. It is easy to read and gives you a good understanding of the issues involved in OOP&D. At least, it helped me bridge the gap between structured/procedural programming and OOP.
|
|
|
|
|