|
public void FileReplaceString(string path, string find, string replace, bool matchcase)
{
System.IO.TextReader r = new System.IO.StreamReader(path);
string text = r.ReadToEnd();
r.Close();
r.Dispose();
text = System.Text.RegularExpressions.Regex.Replace(text, System.Text.RegularExpressions.Regex.Escape(find), replace, (matchcase)?System.Text.RegularExpressions.RegexOptions.None:System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.IO.TextWriter w = new System.IO.StreamWriter(path);
w.Write(text);
w.Flush();
w.Close();
w.Dispose();
} To call:
<br />
FileReplaceString(@"C:\Document.txt", "FLAT", "ZAG", false);<br />
|
|
|
|
|
Has anyone used C# (or even VB.NET) to display video from an ethernet camera? I am trying to do this in the Compact Framework. Does anyone know of any examples, web sites, tools or the best classes to help with this?
Thanks!!!
|
|
|
|
|
May be this can help you... I used a library from DirectShow.Net[^].
This guy has a sample using a webcam (dxwebcam) look for that one.
daniero
|
|
|
|
|
Thanks - I'll check it out and see if it works with the Compact Framework.
|
|
|
|
|
Here's a good problem.
I'm trying to set up a system where text fields in a database define dynamically-generated controls on the form. In other words, when a user makes a selection on the form, the system will pull all the records that match that selection from a database that contains text information that defines all the controls from one table, and text information that defines each of those controls' properties from a related table.
Now, I could set up switch statements that would satisfy many of the possible permutations. But that would require hundreds of lines of code, and it wouldn't be feasible to predict all the possibilities.
My idea is to use generics (which I'm just learning about) to dynamically define the controls and their properties. In essense, I need to somehow get text from the database into the utilization of the generic.
Conceptually, something along these lines:
****
public class GenericControl<T>
{
**Code to construct the control**
}
class TestGeneric
{
private void BuildControl()
{
//In reality, this string would be retrieved from the database.
string controltype = "System.Web.UI.WebControls.TextBox";
//Then the part that won't work:
GenericControl<controltype> MyControl = new GenericControl<controltype>();
}
}
****
Now, if I understand correctly, generic utilization requires that you use a defined object where I have "controltype" - which is why the above example won't work. Is there any way to take the string "System.Web.UI.WebControls.TextBox" and use it to get a TextBox object type (or the appropriate object type) - without using a switch statement?
|
|
|
|
|
I think that you will have much better luck using reflection instead.
Use the GetConstructors method of the Type class to get an array of ConstructorInfo objects. Use the Invoke method to create the object.
---
b { font-weight: normal; }
|
|
|
|
|
In Generics, types must be known compile-time, not run-time, and do not instance parameters (controltype is an instance of the System.String class). The suggestion from Guffa is very good, but you could still avoid having tons of switches in your code.
Start from the fact that all your controls inherit from the System.Windows.Forms.Control class, which, I would say, contains about 95% of the properties of the built-in controls (Size, Font, Text, Position, Name, Visible, etc.).
So, using reflection, you could, as suggested by Guffa, instanciate your controls in a way similar to the one below:
List<Control> controls = new List<Control>();
foreach (....)
{
Type myControlType = Type.ReflectionOnlyGetType(controltype, true, false);
ConstructorInfo myControlContructor = myControlType.GetConstructor(Type.EmtpyTypes);
Control myControl = myControlContructor.Invoke(null);
controls.Add(myControl);
}
Then, set all the properties common to all controls, I think this would cover much of your cases.
You will need some mechanism (maybe a flag from your database) to indicate properties that are not defined in the System.Windows.Forms.Control class.
Hope this helps,
Michel
--------
"I say no to drugs, but they don't listen."
- Marilyn Manson
-- modified at 7:39 Friday 27th January, 2006
|
|
|
|
|
I have a program that uses a custom combobox I wrote. On SOME versions of Windows 2K, when the combobox dropdown closes, the area underneath the dropdown is not redrawn/refreshed. If I add a "Refresh()" call to the code to refresh the display when it closes, nothing happens.
I also notice that, on the same display, if I run a program that has a crystal reports control, when the report is finished, the report is drawn offset to the right (on other displays, it is scaled to fit within the window and centered). If you scroll all the way to the right of the report, then back to the left edge, what went under the left edge of the control display is not redrawn (it is missing), even after forcing a refresh. The only way to get it to redraw is to change the scale/size of the displayed report.
So far, I have only seen this happen on SOME versions of Windows 2K. I have not seen it happen on all Win 2K or even Win XP systems. If I do a windows update on the effected systems, it does not fix the problem.
Is there a corrective action, either in Windows settings or programatically?
-- modified at 15:45 Thursday 26th January, 2006
|
|
|
|
|
Try attatching Invalidate() to the drop down / changing events of the listbox, or on the Form.Enter event
|
|
|
|
|
Since I am calling the parent window's "Refresh()" method, shouldn't that cause it to completely redraw, regardless of whether any part if it is invalidated or not. I know that calling the "Update()" method should only cause a redraw of those parts of the interface that are invalidated, but I thought "Refresh()" said redraw entire... no matter what.
|
|
|
|
|
I think refresh() changes the date, all painting should be in paint / invalidate()
Invalidate() is what tells the system to re-draw the client area - call that whenever focus enters the form or leaves the combobox
|
|
|
|
|
Hi.
How can send my programm to system tray.
I write a clock alarm program and want when running go to system tray.
Best wishes
|
|
|
|
|
You can use the NotifyIcon[^]-Component.
'A programmer is just a tool which converts caffeine into code'
|
|
|
|
|
|
Can someone please tell me how to create the appropriate buffer to hold characters in a LPCTSTR buffer in C#. I have tried using Strings but the results are chaotic to say the least.
For instance, SendMessage, often will use a LPCTSTR in the LPARAM to return a collection of characters. How do I create the appropriate array in C#.
Eric
|
|
|
|
|
I'm sorry to be more clear, How do I get a string safely into an IntPtr since I have defined my SendMessage with LPARAM as an IntPtr?
Thanks,
Eric
|
|
|
|
|
Try System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi(Auto/Uni) . This will do the trick.
Andy
Human beings were not meant to sit in little cubicles staring at computer screens all day, filling out useless forms and listening to eight different bosses drone on about about mission statements. -- Peter Gibbons
|
|
|
|
|
Thanks Andy, this seems to work!
Eric
|
|
|
|
|
Try casting it to a C# string from a lpcstr
|
|
|
|
|
Use StringBuilder from the System.Text namespace.
[DllImport("kernel32.dll")]
static extern bool GetVolumeNameForVolumeMountPoint(string lpszVolumeMountPoint, [Out] StringBuilder lpszVolumeName, uint cchBufferLength);
public static string GetVolumeName(string MountPoint)
{
StringBuilder sb = new StringBuilder();
GetVolumeNameForVolumeMountPoint(MountPoint, sb, 100);
return sb.ToString();
}
|
|
|
|
|
I tried a StringBuilder but I got empty strings back. I think it has to do with the LPARAM parameter needing to be LPCSTR type pointer and StringBuilder is in appropriate.
What I am trying to do is use the SendMessage API to get the textline in an edit control with EM_GETLINE. The LPARAM parameter in this case is an LPCSTR pointer to a buffer of characters and I just don't know how to allocate the memory for this to happen correctly.
Thanks,
Eric
|
|
|
|
|
I recently recieved a snipit of code from a friend that contained a character I was unfamiliar with and in fact that my conversion tool was unfamiliar with as well.
The code contained a charcter of pipes (||) used to represent an "OR" argument. The original code was written in VB.
Can someone tell me the equivilent of || for the "OR" argument in C#?
Here is the line of code:
<br />
sub void Customers1_ItemDataBound(Object sender, RepeaterItemEvenArgs e) <br />
{ <br />
<br />
if (e.Item.ItemType = ListItemType.Item) || (e.Item.ItemType = ListItemType.AlternatingItem) <br />
<br />
{ <br />
Repeater oRpt = (Repeater) e.Item.FindControl("Customers2") <br />
DataRowView oDrv = e.Item.DataItem <br />
<br />
'Bind item repeater <br />
//This line is to retrieve only the records in the CTPT table that have the same ItemID as the HSCTI record in the current row <br />
oRpt.DataSource = oDrv.CreateChildView("ItemID") <br />
oRpt.DataBind <br />
end if <br />
} <br />
<br />
//Similarly, to show all the related comments on the Customers2 repeater, you include code in the Customers2_ItemDataBound() event <br />
sub void Customers2_ItemDataBound(Object sender, RepeaterItemEvenArgs e) <br />
{ <br />
<br />
//NOTICE: and this line of code as well <br />
if (e.Item.ItemType = ListItemType.Item) || (e.Item.ItemType = ListItemType.AlternatingItem) <br />
<br />
<br />
{ <br />
//This is to look for the Customers3 repeater that is nested inside the Customers2 repeater <br />
Repeater oRpt = (Repeater) e.Item.FindControl("Customers3") <br />
DataRowView oDrv = e.Item.DataItem <br />
<br />
'Bind item repeater <br />
oRpt.DataSource = oDrv.CreateChildView("ct_number") <br />
oRpt.DataBind <br />
end if <br />
} <br />
Thank you for your help.
Sincerely,
Tim
|
|
|
|
|
The equivalent for || in C# is ||.
The problem you have is that the syntax of the if statement is wrong. The syntax should look like this:
if (expression) { ... }
What you have is:
if expression { ... }
Put a parenthesis around the expression, and you will get further.
---
b { font-weight: normal; }
|
|
|
|
|
Guffa,
Thanks so much for the pointer. I can't believe I didn't see that. What I did was to immediately put the code into the converter to get a first pass at it in C# when it failed on the pipes[||].
Thank you again.
Tim
|
|
|
|
|
TMF320 wrote:
Thanks so much for the pointer. I can't believe I didn't see that.
Don't feel bad, I did the same thing repeatedly while porting an algorithm from vba to c#.
|
|
|
|