|
Hi,
I'm trying to get all the files from directory that ends with *.txt only.
The problem is, when i'm doing:
string[] filePaths = Directory.GetFiles(@"C:\MyDir\", "*.txt");
i get also the path.
How can i get only the filename?
(at the end i have to get a string[] because i need to use it as DataSource)
|
|
|
|
|
Example:
string fileName = @"C:\mydir\myfile.txt";<br />
string result;<br />
result = Path.GetFileName(fileName);
Regards.
I Love T-SQL
"Don't torture yourself,let the life to do it for you."
If my post helps you kindly save my time by voting my post.
www.aktualiteti.com
|
|
|
|
|
tamir901 wrote: i need to use it as DataSource
For a Control? What type?
Have you tried this:
System.IO.FileInfo[] fi = (new System.IO.DirectoryInfo ( "C:\\" )).GetFiles ( "*.txt" ) ;
listBox1.DataSource = fi ;
comboBox1.DataSource = fi ;
checkedListBox1.Items.AddRange ( fi ) ;
domainUpDown1.Items.AddRange ( fi ) ;
I prefer using FileInfo rather than string.
|
|
|
|
|
Thanks...
|
|
|
|
|
I'm a long time VB addict, and I am finally making the switch, and have a quick question. When you're in the code view in VB and you open the drop-down box on the right, you get all the options for that control, so lets say you were in form1_load and clicked the drop down, you would see every available option for form1_whatever. Where the heck is that in C#? I'm going to have to keep VB open just to see the options... hahaha.
Thanks in advance!
|
|
|
|
|
You mean Propety Windows?
You go to view and Select Property Window or Right-Click on any control in Design mode and select Properties. After that you can dock with control
|
|
|
|
|
Nah, like form1_leave form1_click form1.ondragdrop etc, all the events that can be handled in your code...
|
|
|
|
|
ahhh i found it, sorta. Thanks
Theres an events "tab" in the properties window... wierd
|
|
|
|
|
|
You'll find the same thing in VB.NET as well.
What you talking about is specific to the VB.NET code editor. It doesn't show up in the C# version.
|
|
|
|
|
Acording to tutorial (A book: GDI+ Custom Controls With Visual CSharp 2005)I am reading, i have created Custom Control that is divered from Control.
I have overriden OnPaint event, and everytime i put base.OnPaint(e) into event, my IDE and app crashes. IDE crashed due to tempting to render a custom control. I am using VS 2008 SP1 and .NET framework 3.5 SP1.
Why does this happen? Is it required to put inside when diverting from Control class)
Edit: Found the problem. The problem was that I used base.OnPaint() inside event function. I needed to ovridde OnPaint
modified on Sunday, December 6, 2009 2:09 PM
|
|
|
|
|
OnPaint() method actually raises the Paint event. By calling OnPaint() from a method bound to the Paint event, you actually set up an infinite loop causing a stack overflow that crashed the IDE.
|
|
|
|
|
Hello,
I'm developing an app that localize WPF applications but When I try to serialize a class that contains a BAMLLOCALIZATIONDICTIONARY serialization failed because this class do not allow serialization.
Is there any way to serialize this class?
Thanks.
Dad
|
|
|
|
|
You can not serialize a dictionary, if this is your dictionary, use KeyedCollection
|
|
|
|
|
In our application , we are used datagridview drag and drop itself , i was did properly , But i want visualization of the drag . Now visual only a default cursor point only .. How can i do visual of these drag and drop. I am used C# Windows application
Thanks....
|
|
|
|
|
Use the dragenter event to set the effect.
e.Effect = DragDropEffects.Move;
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Hi everyone!
I have an app: draw a resizeable and draggable rectangle on form.
Now i want to modify it to draw a resizeable and draggable rectangle on picturebox not on Form to select area and crop image.
I tried a few ways but i can't. I'm newbie in C#. Please help me! Thank you so much!
Here's my code:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace DragResizeRectangle
{
public partial class MainForm : Form
{
private RectangleF DragRect=new RectangleF(20, 20, 100, 80);
private System.Drawing.RectangleF[] Adornments = new RectangleF[0];
private bool MouseInRect = false;
private bool RectDragging = false;
private bool RectResizing = false;
private int ActiveAdornment = -1;
private Point MousePos;
private SolidBrush BackBrush;
private System.Drawing.Pen[] LinePen = new Pen[0];
public MainForm()
{
InitializeComponent();
}
void MainFormLoad(object sender, EventArgs e)
{
Adornments=new RectangleF[8];
LinePen=new Pen[2];
BackBrush = new SolidBrush(BackColor);
LinePen[0] = new Pen(ForeColor);
LinePen[1] = new Pen(ForeColor);
LinePen[1].DashPattern = new float[] { 5, 3 };
CalcAdornments();
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.UserPaint, true);
}
private void CalcAdornments()
{
Adornments[0] = new RectangleF(DragRect.X - 4, DragRect.Y - 4, 8, 8);
Adornments[1] = new RectangleF(DragRect.X + DragRect.Width / 2 - 4, DragRect.Y - 4, 8, 8);
Adornments[2] = new RectangleF(DragRect.X + DragRect.Width - 4, DragRect.Y - 4, 8, 8);
Adornments[3] = new RectangleF(DragRect.X + DragRect.Width - 4, DragRect.Y + DragRect.Height / 2 - 4, 8, 8);
Adornments[4] = new RectangleF(DragRect.X + DragRect.Width - 4, DragRect.Y + DragRect.Height - 4, 8, 8);
Adornments[5] = new RectangleF(DragRect.X + DragRect.Width / 2 - 4, DragRect.Y + DragRect.Height - 4, 8, 8);
Adornments[6] = new RectangleF(DragRect.X - 4, DragRect.Y + DragRect.Height - 4, 8, 8);
Adornments[7] = new RectangleF(DragRect.X - 4, DragRect.Y + DragRect.Height / 2 - 4, 8, 8);
}
private void SetAdornmentCursor()
{
if (ActiveAdornment != -1) {
switch (ActiveAdornment) {
case 0:
Cursor = Cursors.SizeNWSE;
break;
case 1:
Cursor = Cursors.SizeNS;
break;
case 2:
Cursor = Cursors.SizeNESW;
break;
case 3:
Cursor = Cursors.SizeWE;
break;
case 4:
Cursor = Cursors.SizeNWSE;
break;
case 5:
Cursor = Cursors.SizeNS;
break;
case 6:
Cursor = Cursors.SizeNESW;
break;
case 7:
Cursor = Cursors.SizeWE;
break;
}
}
else {
Cursor = Cursors.Default;
}
}
void MainFormMouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left) {
if (MouseInRect & ActiveAdornment == -1) {
RectDragging = true;
MousePos = new Point(e.X, e.Y);
MousePos = PointToScreen(MousePos);
}
else if (ActiveAdornment != -1) {
RectResizing = true;
MousePos = new Point(e.X, e.Y);
MousePos = PointToScreen(MousePos);
}
}
}
void MainFormMouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left) {
Point CurrMouse = new Point(e.X, e.Y);
CurrMouse.X = Math.Max(0, Math.Min(CurrMouse.X, ClientSize.Width));
CurrMouse.Y = Math.Max(0, Math.Min(CurrMouse.Y, ClientSize.Height));
CurrMouse = PointToScreen(CurrMouse);
if (RectDragging) {
if (Math.Abs(CurrMouse.X - MousePos.X) > 2 | Math.Abs(CurrMouse.Y - MousePos.Y) > 2) {
DragRect.Offset(CurrMouse.X - MousePos.X, CurrMouse.Y - MousePos.Y);
DragRect.X = Math.Max(0, Math.Min(DragRect.X, ClientSize.Width - DragRect.Width));
DragRect.Y = Math.Max(0, Math.Min(DragRect.Y, ClientSize.Height - DragRect.Height));
MousePos = new Point(e.X, e.Y);
MousePos.X = Math.Max(0, Math.Min(MousePos.X, ClientSize.Width));
MousePos.Y = Math.Max(0, Math.Min(MousePos.Y, ClientSize.Height));
MousePos = PointToScreen(MousePos);
CalcAdornments();
Invalidate();
}
}
else if (RectResizing) {
if (ActiveAdornment != -1) {
if (Math.Abs(CurrMouse.X - MousePos.X) > 2 | Math.Abs(CurrMouse.Y - MousePos.Y) > 2) {
switch (ActiveAdornment) {
case 0:
DragRect.Height -= CurrMouse.Y - MousePos.Y;
if (!(DragRect.Height < 20)) {
DragRect.Y -= -CurrMouse.Y + MousePos.Y;
}
else {
DragRect.Height = 20;
}
DragRect.Width -= CurrMouse.X - MousePos.X;
if (!(DragRect.Width < 20)) {
DragRect.X -= -CurrMouse.X + MousePos.X;
}
else {
DragRect.Width = 20;
}
break;
case 1:
DragRect.Height -= CurrMouse.Y - MousePos.Y;
if (!(DragRect.Height < 20)) {
DragRect.Y -= -CurrMouse.Y + MousePos.Y;
}
else {
DragRect.Height = 20;
}
break;
case 2:
DragRect.Height -= CurrMouse.Y - MousePos.Y;
if (!(DragRect.Height < 20)) {
DragRect.Y -= -CurrMouse.Y + MousePos.Y;
}
else {
DragRect.Height = 20;
}
DragRect.Width += CurrMouse.X - MousePos.X;
DragRect.Width = Math.Max(20, DragRect.Width);
break;
case 3:
DragRect.Width += CurrMouse.X - MousePos.X;
DragRect.Width = Math.Max(20, DragRect.Width);
break;
case 4:
DragRect.Width += CurrMouse.X - MousePos.X;
DragRect.Width = Math.Max(20, DragRect.Width);
DragRect.Height += CurrMouse.Y - MousePos.Y;
DragRect.Height = Math.Max(20, DragRect.Height);
break;
case 5:
DragRect.Height += CurrMouse.Y - MousePos.Y;
DragRect.Height = Math.Max(20, DragRect.Height);
break;
case 6:
DragRect.Height += CurrMouse.Y - MousePos.Y;
DragRect.Height = Math.Max(20, DragRect.Height);
DragRect.Width -= CurrMouse.X - MousePos.X;
if (!(DragRect.Width < 20)) {
DragRect.X -= -CurrMouse.X + MousePos.X;
}
else {
DragRect.Width = 20;
}
break;
case 7:
DragRect.Width -= CurrMouse.X - MousePos.X;
if (!(DragRect.Width < 20)) {
DragRect.X -= -CurrMouse.X + MousePos.X;
}
else {
DragRect.Width = 20;
}
break;
}
MousePos = new Point(e.X, e.Y);
MousePos.X = Math.Max(0, Math.Min(MousePos.X, ClientSize.Width));
MousePos.Y = Math.Max(0, Math.Min(MousePos.Y, ClientSize.Height));
MousePos = PointToScreen(MousePos);
CalcAdornments();
Invalidate();
}
}
}
}
else {
MouseInRect = false;
ActiveAdornment = -1;
RectangleF tmprect = default(RectangleF);
int i = 0;
foreach (RectangleF tmprect_loopVariable in Adornments) {
tmprect = tmprect_loopVariable;
if (tmprect.Contains(e.X, e.Y)) {
MouseInRect = true;
ActiveAdornment = i;
SetAdornmentCursor();
Invalidate();
return;
}
i += 1;
}
if (DragRect.Contains(e.X, e.Y)) {
MouseInRect = true;
Cursor = Cursors.SizeAll;
Invalidate();
return;
}
Cursor = Cursors.Default;
Invalidate();
}
}
void MainFormMouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left) {
RectDragging = false;
RectResizing = false;
Invalidate();
}
}
void MainFormPaint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.FillRectangle(BackBrush, g.ClipBounds);
if (!MouseInRect) {
g.DrawRectangle(LinePen[0], DragRect.X, DragRect.Y, DragRect.Width, DragRect.Height);
}
else {
g.DrawRectangle(LinePen[1], DragRect.X, DragRect.Y, DragRect.Width, DragRect.Height);
g.FillRectangles(Brushes.White, Adornments);
g.DrawRectangles(Pens.Black, Adornments);
}
}
}
}
|
|
|
|
|
Hi,
your code seems pretty clean, although probably too wieldy and not correct.
Unfortunately your text isn't clear, "I tried a few ways but i can't" doesn't even start to describe what is going on.
Here are some thoughts:
1. the code shown describes your Form, so calling Invalidate() will cause the entire Form to be repainted; when you draw on a Control (e.g. a PictureBox), you should invalidate that Control, and make sure it has a paint handler to paint its stuff itself.
What may be happening right now is one of these:
- you Invalidate the Form, it repaints with a rectangle on top of your PB, then the PB itself repaints;
- you Invalidate the Form, it repaints with the exception of the areas taken by its controls, then it does not repaint the controls, as you did not explicitly ask for that to happen (see Invalidate(boolean)).
2.
A PictureBox is a tricky (I would add "and stupid") Control; it knows how to resize the image it holds (SizeMode), however such resizing will not apply to what you draw on top of the PB. For applications like yours I tend to use a Panel, and paint the image myself, in the same paint handler that draws the rectangle, and with the same transformation settings.
3.
You should use mouse handlers that belong to the Control of interest, that would yield coordinates relative to that Control; IMO there probably is no need to call PointToScreen and the like.
|
|
|
|
|
Hey all,
I came across one unusual behavior of Directory.Exists() method when I was testing user path input, I was trying with different inputs just to test the existence of the directory on my local drive. For negative test cases I provided the existing directory path and put two ".." at the end of directory path
e.g.
lets say I have "C:\CurrentDir" on my local drive
when I check the existence of directory using
bool isDirExist = Directory.Exists(@"C:\CurrentDir..");//".." are appended
surprisingly isDirExist is true
when I try to access file contained in this directory it throws error......
it does not throw exception if I append only one "." [for @"C:\CurrentDir." +AnyFileName]
Is this is bug in Directory.Exists method or anything else is happening behind the screen at OS level??
|
|
|
|
|
SaveTigers wrote: surprisingly isDirExist is true
Directory.Exists is working as expected. You can't create a directory that ends with . (DOT) in windows. Windows will remove the dots automatically. So the function is not caring about the dots.
You can do something like.
string directoryToCheck = Path.GetFullPath(@"C:\CurrentDir..");
bool isDirExist = Directory.Exists(directoryToCheck);
Best wishes,
Navaneeth
|
|
|
|
|
Hi all.
I would like to write a program that will function as a client on a remote computer. The idea is that at a given time i will be able to request specific actions from the client application. Remotely, of course.
Up to now i have considered two ways of doing this:
1. Use a DB on the server. The client will poll the DB for tasks at a standard rate and act as insrtucted there.
2. Use a web-service. The client will call a getTasks() method at a standard rate.
Both of these methods include polling the server at a fixed interval. This is necessary, since i need the client application to respond almost immediateley to server requests.
The thing is, i don't want these applications to interfere with the user's use-experience. I don't want him to feel that he has my application running in the background and polling every now and then.
So:
Are my concerns justified?
If so, what would you suggest as a substitute?
If not, which of the above would you suggest?
Thanks for reading all the way down here. I will really appreciate your responses.
SummerBulb.
|
|
|
|
|
I'd suggest you to got with choice 2. Use a web service or WCF to communicate with DB. This avoid tight coupling of client application with database.
SummerBulb wrote: I don't want him to feel that he has my application running in the background and polling every now and then.
So users won't be using your application directly? If not, you can start it and keep it in "System tray". Have a thread which does the polling.
Best wishes,
Navaneeth
|
|
|
|
|
N a v a n e e t h wrote: Have a thread which does the polling
I don't mind having the application in the background. My dillemma is concerning the CPU, memory and internet bandwidth.
|
|
|
|
|
SummerBulb wrote: My dillemma is concerning the CPU, memory and internet bandwidth.
It'd be a premature optimization. I'd suggest to make the application working first. Profile it and see whether it requires optimizations. BTW, you can consider few things when you develop.
1 - Ensure calls to Dispose() for all disposable objects - CLR manages the memory quite well and you don't have to worry much about it. Few classes in the .NET framework implements IDisposable and make sure you call Dispose() method on it to avoid resource leaks.
2 - Don't send unnecessary informations over the wire - I have found WCF services perform better than SOAP based web services. But you need to understand how WCF does the communication. When you send large objects/collections, it will be slow. So make sure you send only the necessary information.
Once you done with the application, profile it and find out the areas that can be optimized further.
Best wishes,
Navaneeth
|
|
|
|
|
Thank you very much for your replies.
Does the WCF differ much from the SOAP? (this must sound like a very silly question to someone whoe knows them both well...)
modified on Sunday, December 6, 2009 4:51 AM
|
|
|
|
|