|
|
Hi Jinal i have this solution with me but this is implemented on 1 string and not on a collection right? For the above to work I would need to loop through the collection and then check. I need something which can be applied directly on the collection without looping.
Regards,
Samar
|
|
|
|
|
Sorry dear.
For collection you have to use looping.
Jinal Desai - LIVE
|
|
|
|
|
As Jinal suggest, use a regular expression.
But I would use a regex to match the whole string you want:
Regex regex = new Regex("(Record.ID)|(RecordID)",
RegexOptions.IgnoreCase
| RegexOptions.CultureInvariant
| RegexOptions.IgnorePatternWhitespace
| RegexOptions.Compiled
);
string result = regex.Replace(InputText,""); This would remove any text containing "record" followed by "id" or any character then "id" in any combination of upper or lower case.
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace
C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
|
|
|
|
|
I'm getting a "Cross-thread operation not valid" error when I call Refresh() on a custom .NET Marquee control. This control used to be working in prior .NET projects and I'm trying to get it going again, eventually with hopes of posting as an article. It was really a great Marquee control in that it could display both text and graphics and could run in any direction, etc. I've included the full source to the control and to the demo form I'm trying to test it with. To help resolve the problem and for anyone else that might benefit from the control. I just can't figure out why I'm calling the
controls.Refresh() method from a cross thread. If anyone can shed some light on this, it would be greatly appreciated.
Aaron
Here's the code for the marquee control:
using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Text;
using System.Drawing.Design;
using System.Timers;
namespace RADMarquee
{
[DefaultProperty("DrawingMode"),
DefaultEvent("DrawingModeChanged")]
public class Marquee : System.Windows.Forms.UserControl
{
#region Privat Members
Graphics _gfx;
int x, y; // coords of top left corner of graphic
float StringWidth = 0;
float StringHeight = 0;
int BoxPaddingTop = 2;
bool bNewBrush = true;
SolidBrush BrushBG;
SolidBrush BrushBG2;
SolidBrush BrushFG;
System.Timers.Timer _tmrMain;
bool bNewText = true;
public enum Direction
{
Up, Down, Right, Left
};
public enum TextRefreshTiming
{
Async, TextOffScreen
};
const string _sMessageDelimiter = "!^#-=_";
const string sBR = "\r\n";
float StringPadding = 2;
#endregion
#region Properties
TextRefreshTiming _refreshTiming = TextRefreshTiming.Async;
[Browsable(true),
ReadOnly(false),
Category("Marquee"),
Description("Not currently working. Async: Display new Text
right after Text is changed. "
+ "TextOffScreen: Wait until the Text scrolls off the screen
before restart.")]
public TextRefreshTiming TextRefreshTimimg
{
get{ return _refreshTiming; }
set
{
_refreshTiming = (TextRefreshTiming)value;
}
}
// Direction
Direction _direction = Direction.Left;
[Browsable(true),
Category("Marquee"),
Description("Marquee Scrolling Direction"),
ReadOnly(false)]
public Direction ScrollingDirection
{
get { return _direction; }
set
{
_direction = value;
}
}
// Refresh Rate
int iRefreshRate = 100;
[Browsable(true),
ReadOnly(false),
Category("Marquee"),
DefaultValue(typeof(int), "100"),
Description("How long to wait before redrawing the Marquee in
milliseconds (set this to a large value to improve perormance)")]
public int RefreshRate
{
get { return iRefreshRate; }
set
{
iRefreshRate = value;
}
}
// Speed
int iSpeed = 8;
[Browsable(true),
ReadOnly(false),
DefaultValue(typeof(int), "8"),
Category("Marquee"),
Description("How far to advance the Marquee during each
refresh")]
public int Speed
{
get { return iSpeed; }
set
{
iSpeed = value;
}
}
public int PauseCycles = 5;
// RegionColor
private Color _bgColor = Color.Black;
[Browsable(true),
ReadOnly(false),
DefaultValue(typeof(Color), "Color.Black"),
Category("Marquee"),
Description("Background Color of Marquee")]
public Color RegionColor
{
get { return _bgColor; }
set
{
_bgColor = value;
bNewBrush = true;
}
}
// BG2Color
private Color _bg2Color = Color.Black;
[Browsable(true),
ReadOnly(false),
DefaultValue(typeof(Color), "Color.Black"),
Category("Marquee"),
Description("Background Color of Marquee")]
public Color BGColor
{
get { return _bg2Color; }
set
{
_bg2Color = value;
bNewBrush = true;
}
}
// ForeColor
Color _foreColor = Color.Red;
[Browsable(true),
ReadOnly(false),
Category("Marquee"),
Description("Marquee Text Color"),
DefaultValue(typeof(Color), "Color.Red")]
public override Color ForeColor
{
get { return _foreColor; }
set
{
_foreColor = value;
bNewBrush = true;
}
}
// Font
private Font _font;
[Browsable(true),
ReadOnly(false),
Category("Marquee"),
Description("Marquee Font Properties ")]
public new Font Font
{
get { return _font; }
set
{
_font = value;
bNewText = true;
}
}
// DisplayText
StringBuilder _sbDisplayCurrent;
[Browsable(true),
Category("Marquee"),
Description("Marquee Display Text"),
ReadOnly(false)]
public string DisplayText
{
get
{
if(!Object.Equals(_sbDisplayCurrent, null))
{
return _sbDisplayCurrent.ToString();
}
else
{
return "";
}
}
set
{
if(Object.Equals(_sbDisplayCurrent, null)) // null
_sbDisplayCurrent
{
_sbDisplayCurrent = new StringBuilder(value);
}
LoadStringCollection(value);
// refresh String on next cycle
if(/*this.TextRefreshTimimg == TextRefreshTiming.Async &&
*/value != "")
{
bNewText = true;
StartDraw(0);
}
}
}
private void LoadStringCollection(string sVal)
{
_sbDisplayCurrent.Remove(0, _sbDisplayCurrent.Length);
_sbDisplayCurrent.Append(sVal);
}
// Enabled
bool bEnabled = true;
[Browsable(true),
ReadOnly(false),
DefaultValue(typeof(bool),"True"),
Category("Marquee"),
Description("Turn the Marquee off or on. Can be used to stop
scrolling")]
public new bool Enabled
{
get { return bEnabled; }
set
{
bEnabled = value;
}
}
#endregion
#region Events
// TextOffScreen
[Browsable(true),
ReadOnly(false),
Category("Marquee"),
Description("Event to raise when text has cleared visible area,
occurs just before TextEnd")]
public event EventHandler TextOffScreen;
// TextEnd
[Browsable(true),
ReadOnly(false),
Category("Marquee"),
Description("Event to raise when Marquee text has cycled once
and is about to start over, occurs just afterTextOffScreen")]
public event EventHandler TextEnd;
private System.ComponentModel.Container components = null;
#endregion
#region Constructor
public Marquee()
{
// this call is required by the Windows.Forms Form Designer.
_sbDisplayCurrent = new StringBuilder();
InitializeComponent();
this.Size = new Size(400,200);
this.Text = "Marquee";
SetStyle(ControlStyles.UserPaint, true); // enable dbl
bufferg
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.DoubleBuffer, true);
Init();
_tmrMain = new System.Timers.Timer();
_tmrMain.Elapsed += new
ElapsedEventHandler(this.DoDrawEvent);
_tmrMain.Interval = this.RefreshRate;
_tmrMain.Start();
}
#endregion
public void Quit()
{
this.Dispose();
}
public void Stop()
{
}
public void Init()
{
if (Object.Equals(_font, null))
_font = new Font("Arial", 14, FontStyle.Bold);
SetDefaults();
}
public void SetDefaults()
{
this.ForeColor = Color.Red;
this.BGColor = Color.Black;
this.RegionColor = Color.Black;
}
public void LoadBrushes()
{
bool bTmp = bEnabled; // save enabled state
bEnabled = false; // disable
// load colors
BrushBG = new SolidBrush(RegionColor);
BrushBG2 = new SolidBrush(BGColor);
BrushFG = new SolidBrush(ForeColor);
bEnabled = bTmp; //EnabledState.Disabled;
bNewBrush = false;
DrawBackground();
}
protected override void OnPaint(PaintEventArgs e)
{
_gfx = e.Graphics;
if(bNewBrush)
LoadBrushes();
if(bNewText)
BuildStringGraphic(e.Graphics);
_gfx.FillRectangle(BrushBG, 0, 0, Width, Height);
// add support here for using a different brush to draw the
strings/portions of the string
_gfx.DrawString(_sbDisplayCurrent.ToString(), _font, BrushFG,
x, y);
}
protected override void OnPaintBackground(PaintEventArgs
pevent)
{
// don't allow the background to paint
}
protected void BuildStringGraphic(Graphics g)
{
// examine the string for settings mark up
// <setting[\s\w\d\"]+/>
// message characters: [\w\s\d\.\"!-\+=&\*\(\)\$%@]+
// <setting bgcolor="" color="" font="" />
//ParseMessages(); // look for settings and apply here
if (Object.Equals(_sbDisplayCurrent, null) ||
Object.Equals(_font, null) || _sbDisplayCurrent.ToString() == "")
return;
StringFormat sfTrue =
(StringFormat)StringFormat.GenericTypographic.Clone();
sfTrue.Alignment = StringAlignment.Near;
sfTrue.LineAlignment = StringAlignment.Near;
SizeF dims = g.MeasureString(_sbDisplayCurrent.ToString(),
_font);
StringWidth = dims.Width;
StringHeight = dims.Height;
// set initial coords by direction
if(_direction == Direction.Right)
{
x = -(int)(StringPadding + StringWidth); // start at left
y = (int)((Height - StringHeight)/ 2 ) + BoxPaddingTop;
}
else if(_direction == Direction.Left)
{
x = (Width + (int)(StringPadding + StringWidth));
y = (int)((Height - StringHeight) / 2 ) + BoxPaddingTop;
}
else if(_direction == Direction.Up)
{
x = (int)StringPadding;
y = (int)(Height / 2);
}
else if(_direction == Direction.Down)
{
x = (int)StringPadding;
y = (int)-(Height / 2);
}
bNewText = false;
}
protected void DrawBackground()
{
Rectangle _rec = new Rectangle(0, BoxPaddingTop, Width,
Height - BoxPaddingTop * 2);
_gfx.FillRectangle(BrushBG2, _rec); // fill the second
background
}
public void DoDrawEvent(object sender, ElapsedEventArgs e)
{
DoDrawingCycle();
}
public void StartDraw(int iPauseCycles)
{
PauseCycles = iPauseCycles;
// don't do anything if this control is disabled
if(bEnabled == false || DisplayText == "")
return;
DoDrawingCycle();
}
protected void DoDrawingCycle()
{
if(_direction == Direction.Right)
{
this.MoveRight();
}
else if(_direction == Direction.Left)
{
this.MoveLeft();
}
else if(_direction == Direction.Down)
{
this.MoveDown();
}
else if(_direction == Direction.Up)
{
this.MoveUp();
}
}
public void MoveLeft()
{
int i = 0;
bool bTextCleared = false;
if( x < -(StringPadding + StringWidth) ) // restart from
right
{
x = Width + (int)StringPadding;
i = 0;
bTextCleared = false;
OnTextEnd(null);
}
else if (x < -(StringWidth) && !bTextCleared)
{
bTextCleared = true;
OnTextClear(null);
}
if(i < PauseCycles)
++i;
else
x = x - Speed;
// folling call is generating this error:
// Cross-thread operation not valid: Control 'mq' accessed
from a thread other than the thread it was created on.
Refresh();
}
public void MoveRight()
{
int i = 0;
bool bTextCleared = false;
if( x > (Width) ) // restart from left
{
x = -Width;
i = 0;
bTextCleared = false;
OnTextEnd(null);
}
else if (x == Width && !bTextCleared)
{
bTextCleared = true;
OnTextClear(null);
}
if(i < PauseCycles)
++i;
else
x = x + Speed;
Refresh();
}
public void MoveUp()
{
int i = 0;
bool bTextCleared = false;
if( y < -(StringPadding + StringHeight + Height) )
{
y = Height + (int)StringPadding;
x = 0;
i = 0;
bTextCleared = false;
OnTextEnd(null);
}
else if (y == -StringHeight && !bTextCleared)
{
bTextCleared = true;
OnTextClear(null);
}
if(i < PauseCycles)
++i;
else
y = y - Speed;
Refresh();
}
public void MoveDown()
{
int i = 0;
bool bTextCleared = false;
if( y > (Height + StringPadding) ) // restart from left
{
x = 0;
y = 0 - (int)StringHeight ;
i = 0;
bTextCleared = false;
OnTextEnd(null);
}
else if (y == Height && !bTextCleared)
{
bTextCleared = true;
OnTextClear(null); // no EventArgs
}
if(i < PauseCycles)
++i;
else
y = y + Speed;
Refresh();
}
#region Event Handling
// invoke delegates when the Marquee text has cleared the
display area
protected virtual void OnTextClear(MarqueeEventArgs e)
{
if (TextOffScreen != null && DisplayText != "")
{
// invokes the delegates.
TextOffScreen(this, e);
}
}
// invoke delegates when the marquee text has reached the
absolute end of its cycle
protected virtual void OnTextEnd(MarqueeEventArgs e)
{
if (TextEnd != null && DisplayText != "")
{
TextEnd(this, e);
}
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void Marquee_Resize(object sender, System.EventArgs e)
{
this.Invalidate();
}
#endregion
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new
System.Resources.ResourceManager(typeof(Marquee));
//
// Marquee
//
this.BackColor = System.Drawing.Color.WhiteSmoke;
this.BackgroundImage = ((System.Drawing.Image)
(resources.GetObject("$this.BackgroundImage")));
this.Name = "Marquee";
this.Resize += new System.EventHandler(this.Marquee_Resize);
}
#endregion
}
public class MarqueeEventArgs : EventArgs
{
int iFlags = 0;
public MarqueeEventArgs()
{
}
}
}
Here's the code for the demo form:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace RadMarqueeDemo
{
public partial class Form1 : Form
{
private RADMarquee.Marquee mq;
private void MyInitializeComponent()
{
mq = new RADMarquee.Marquee();
this.mq.ForeColor = System.Drawing.Color.Red;
this.mq.BGColor = Color.Black;
this.mq.Location = new System.Drawing.Point(10, 10);
this.mq.Name = "mq";
this.mq.ScrollingDirection =
RADMarquee.Marquee.Direction.Left;
this.mq.Size = new System.Drawing.Size(300, 50);
mq.RefreshRate = 70;
this.mq.Speed = 7;
this.mq.TabIndex = 0;
this.Controls.Add(this.mq);
mq.Enabled = true;
mq.DisplayText = "Hello World!";
}
public Form1()
{
MyInitializeComponent();
InitializeComponent();
}
private void Form1_Load_1(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
this.mq.Enabled = true;
}
}
}
|
|
|
|
|
|
To add to Abhinavs reply, a couple of things:
1) When you post code, don't post the whole project - just the relevant portion. A code fragment makes it a lot more likely that your code will be read, (and responded to with relevant details) than posting the whole file. That just makes it tiresome to get to the "Reply" button if anyone wants to.
2) When you post a code fragment, use the "code block" widget, or suuround it with <pre>...</pre> tags to preserve the formatting - it does a much neater job than the dogs dinner you served up!
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace
C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
|
|
|
|
|
Aaron Radich wrote:
_tmrMain = new System.Timers.Timer();
_tmrMain.Elapsed += new ElapsedEventHandler(this.DoDrawEvent);
_tmrMain.Interval = this.RefreshRate;
_tmrMain.Start();
That's your problem. The System.Timers.Timer raises events on a ThreadPool thread unless you set it's Synchronizing Object property.
Alan.
|
|
|
|
|
Hi,
I did not read all that code, however I'm pretty sure what you need is explained here[^].
|
|
|
|
|
With a folderbrowserdialog and a textbox that shows the folder to save to, i need to put \ after.
have been trying to put it like this:
textBoxSaveToFolder + (\) or ("\") or "\"
but it doesnt work. How can that been done???
wc.DownloadFileAsync(new Uri(sFiles.Trim()), String.Format(textBoxSaveToFolder.Text + "{0}.xml", ("omgang=") + i, Path.GetExtension(sFiles)));
private void btnFolder_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
textBoxSaveToFolder.Text = folderBrowserDialog1.SelectedPath;
}
}
|
|
|
|
|
in languages such as C# a backslash is an escape character, it isn't taken literally; instead it gets combined with the character following it to create specials, such as a tab, a newline, etc.
if you want a real backslash, you need to:
- either have two of them
- or indicate you don't want the escape mechanism by prefixing a @
so try "a\\b" or @"a\b"
That is elementary knowledge about a programming language, something you need to pick up from reading a book on C#.
To apply this correctly in your code, I suggest you use your debugger, single-step through your code and watch at the intermediate values. And yes, Visual Studio may try and confuse you by adding some backslashes that aren't really there, so my recommendation is to really output strings to the output panel, using
Console.WriteLine(str);
That is bound to truthfully show the string content no matter what.
|
|
|
|
|
Thank you very much!
I have seen that before but didnt understood what is was good for (@).
Seems like i have missed a lot from books, i got C# beginners book but havent seen it. Or i learn very slow!
|
|
|
|
|
|
You may want to consider using Path.Combine() [^] instead of manually constructing the filename.
/ravi
|
|
|
|
|
thanks, that will be useful
|
|
|
|
|
You need to escape the character...
textBoxSaveToFolder + "\\"
Sorry, I didn't see the above answers for some reason before I submitted mine.
|
|
|
|
|
ok...I've looked around, and haven't been able to get many places with this.
(Necessary Background Information)
Essentially, the situation we are in is that users need to log into our application that is going to be sitting on a production server. The user's corporate credentials will be used to authenticate their access to our app and one of the primary functions that they are to have is a "Push Button File Push" to a Celera server that has ancillary processes running on it that are out of scope for the purposes of this post. The user will NOT be allowed to be authenticated against the Celera share, our application will.
Problem:
I need to be able to access the file server through my code via a UNC path...and in production, this server will be locked down.
I'm trying to use a NetworkCredentials instance to authenticate myself, but I am unsure exactly what System.Net object I need to use to connect to our server. WebRequest?? WebClient??
File.CreateText isn't working, because I can't find a way to provide credentials to the path.
Any help?
I am absolutely stumped!!
My code thus far looks like this (with WebRequest):
private static void WriteArchive(string document, MessageType outboundInfo, string filePath)
{
try
{
WebRequest request = UNCResource("~data/ApplicationSettings.xml");
TextWriter writer = null;
string fileName = outboundInfo.FileName;
writer = new StreamWriter(filePath + fileName);
request.
writer.Write(document);
writer.Close();
}
catch (Exception ex)
{
throw ex;
}
}
private static WebRequest UNCResource(string configurationLocation)
{
XmlDocument configuration = (new XmlDocument().Load(configurationLocation));
NetworkCredential credentials = GetNetworkCredentials(configuration);
return UNCResource(ref credentials, configuration);
}
private static WebRequest UNCResource(ref NetworkCredential credentials, XmlDocument configuration, Destination destination)
{
CredentialCache cache = new CredentialCache();
string destinationUri = String.Empty;
switch (destination)
{
case Destination.Archive:
{
destinationUri = configuration.DocumentElement.SelectSingleNode("fileArchiveLocation").InnerText;
break;
}
case Destination.Outbound:
{
destinationUri = configuration.DocumentElement.SelectSingleNode("fileSaveLocation").InnerText;
break;
}
}
cache.Add(new Uri(destinationUri), "Basic", credentials);
WebRequest server = WebRequest.Create(destinationUri);
server.Credentials = cache;
return server;
}
private static NetworkCredential GetNetworkCredentials(XmlDocument configuration)
{
XmlNode userNameNode = configuration.DocumentElement.SelectSingleNode("username");
XmlNode passwordNode = configuration.DocumentElement.SelectSingleNode("password");
XmlNode domainNode = configuration.DocumentElement.SelectSingleNode("domain");
return new NetworkCredential(userNameNode.InnerText, passwordNode.InnerText);
}
"I need build Skynet. Plz send code"
|
|
|
|
|
By the way, Configuring the app for Impersonation is a last option. I am hoping to find a solution that simply allows us a means to provide credentials to the server at run-time and drop a file into a folder that is accessible via those credentials.
Anybody have an idea how to get this done?
"I need build Skynet. Plz send code"
|
|
|
|
|
Hi All,
I'm trying to hide/show controls which allow a user to add a new record to a DataList when the Edit button is clicked. So when the btnEdit link is clicked (please see code below) then the ddlDescAdd dropDownList show be hidden (visible = false).
Here is a snipet of my DataList:
<asp:DataList id="ItemsList"
runat="server"
...>
<ItemTemplate>
<asp:LinkButton id="btnEdit"
Text="Edit"
CommandName="Edit"
runat="server"/></td&g
</ItemTemplate>
<EditItemTemplate>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlDescAdd"
Runat="server" Width="280px" />
<asp:LinkButton id="btnAdd"
Text="Add"
CommandName="Insert"
runat="server" /> </td>
</FooterTemplate>
</asp:DataList>
Does anyone know how I can do this?
Thanks,
Mel
|
|
|
|
|
|
Thanks for your reply Jinal,
I was able to hide the footerTemplate when the edit button is clicked.
I have another problem I've came into though. When The dataList is empty I want to show only the header and footer templates, I thought I could do this with the following code:
if (dTable.Rows.Count == 0)
{
dlICD9Codes.ShowHeader = true;
dlICD9Codes.ShowFooter = true;
lblSearchStatus.Text = "count == 0";
}
else
{
dView = new DataView(dTable);
dView.Sort = "ICD9CODE";
lblSearchStatus.Text = "count != 0";
}
Do you know why the header and footer aren't showing up when the row count is zero? Do you know of a way I can get this working?
Thanks in advance,
Mel
|
|
|
|
|
|
hi,
i have developed win apps with c# and this apps basically read csv file through a map drive.
when i am running this apps just clicking on exe file name then it can read file through the map drive but when the exe of my apps is invoked by win task scheduler then application is running but fail to read the file through map drive. i am testing this apps on windows server 2008. so please anyone help me with idea that what i need to do as a result network resource will be available when my apps will be invoked by windows task scheduler. please help in detail....its urgent.
thanks in advance.
tbhattacharjee
|
|
|
|
|
Hi,
I suspect your app running through Task Scheduler isn't using your logon session and desktop, hence is unaware of your drive mappings. You probably could incorporate code to do its own drive mapping, see this CP article: Map Network Drive (API)[^]. Hope it works well for you, I haven't done this myself yet.
|
|
|
|
|
When you add a scheduled task, make sure the user you enter has sufficient privileges to access the drives to be mapped. Then you might also need to create a batch file which maps the drives with the "net use" command before starting the actual task from that batch file.
|
|
|
|
|