|
it works!!! Thanks very much
|
|
|
|
|
string test = "1";
test.Replace("1","2");
i tried doing this in something more complex like cleaning out characters in a big string, but even after this code i'm seeing .net ignore me...test should equal "2" after this, and it doesn't.
|
|
|
|
|
Strings are immutable in .NET. This is for thread safety and other reasons. Do this instead
test = test.Replace("1", "2");
-
Drew
|
|
|
|
|
Wow little ackward but owell...Thx for clearing that up for me
|
|
|
|
|
How can I get a dataset of selected rows in a DataGrid? It should get me the correct selected rows even if the grid is sorted on any one of the columns.
|
|
|
|
|
snnn wrote:
How can I get a dataset of selected rows in a DataGrid? It should get me the correct selected rows even if the grid is sorted on any one of the columns.
I am still looking forward for an answer. Would anyonr please help?
|
|
|
|
|
Hi,
We have this SelectedIndexChanged event that is raised by the .NET listview control.
I wonder why there is no SelectedIndexChanging event in the listview control. I have a need whereby i need to store the current index somewhere before the selectedindexchanged event is about to be raised. I can't see how i can achieve this. I certainly need an even that will tell me that the index is about to be changed. SelectedIndexChanged event is too late for me to store the index of the previously selected index.
Any ideas?
|
|
|
|
|
It might seem logical that you could derive from the control and dispatch a pre notification event, but that might not work because the control just wraps the Win32 API. So your best bet is to keep a previousSelectedIndex around. When your SelectedIndexChanged delegate is invoked you can do what you need to do, then store the previousSelectedIndex at that time. The next time your delegate is invoked the previousSelectedIndex will be the previous index.
-
Drew
|
|
|
|
|
Hey guys... first I just want to praise The Code Project and everyone on here for basically teaching me the basics of C#. I have a web background and this is the first language I have learned coming off my javascript and vbscript web knowledge.
I am sure this question has a very simple answer, but that is an answer I cannot figure out!
Here it is...
I have dummied down my problem into the following code examples. I have a very basic program that takes one richtext field and copies the content into another. I am doing so by calling a method in a separate class that accomplishes the copying of text.
Form1.cs:
<br />
using System;<br />
using System.Drawing;<br />
using System.Collections;<br />
using System.ComponentModel;<br />
using System.Windows.Forms;<br />
using System.Data;<br />
<br />
namespace TestApp<br />
{<br />
public class Form1 : System.Windows.Forms.Form<br />
{<br />
private System.Windows.Forms.Label label1;<br />
public System.Windows.Forms.RichTextBox txtFromSecondClass;<br />
public System.Windows.Forms.RichTextBox txtToSecondClass;<br />
private System.Windows.Forms.Label label2;<br />
private System.Windows.Forms.Button button1;<br />
private System.ComponentModel.Container components = null;<br />
<br />
public Form1()<br />
{<br />
InitializeComponent();<br />
<br />
}<br />
<br />
protected override void Dispose( bool disposing )<br />
{<br />
if( disposing )<br />
{<br />
if (components != null) <br />
{<br />
components.Dispose();<br />
}<br />
}<br />
base.Dispose( disposing );<br />
}<br />
<br />
#region Windows Form Designer generated code<br />
private void InitializeComponent()<br />
{<br />
this.txtFromSecondClass = new System.Windows.Forms.RichTextBox();<br />
this.label1 = new System.Windows.Forms.Label();<br />
this.txtToSecondClass = new System.Windows.Forms.RichTextBox();<br />
this.label2 = new System.Windows.Forms.Label();<br />
this.button1 = new System.Windows.Forms.Button();<br />
this.SuspendLayout();<br />
this.txtFromSecondClass.Location = new System.Drawing.Point(8, 200);<br />
this.txtFromSecondClass.Name = "txtFromSecondClass";<br />
this.txtFromSecondClass.Size = new System.Drawing.Size(280, 64);<br />
this.txtFromSecondClass.TabIndex = 0;<br />
this.txtFromSecondClass.Text = "";<br />
this.label1.Location = new System.Drawing.Point(8, 176);<br />
this.label1.Name = "label1";<br />
this.label1.Size = new System.Drawing.Size(272, 16);<br />
this.label1.TabIndex = 1;<br />
this.label1.Text = "From SecondClass:";<br />
this.txtToSecondClass.Location = new System.Drawing.Point(8, 32);<br />
this.txtToSecondClass.Name = "txtToSecondClass";<br />
this.txtToSecondClass.Size = new System.Drawing.Size(280, 64);<br />
this.txtToSecondClass.TabIndex = 2;<br />
this.txtToSecondClass.Text = "";<br />
this.label2.Location = new System.Drawing.Point(8, 8);<br />
this.label2.Name = "label2";<br />
this.label2.Size = new System.Drawing.Size(272, 16);<br />
this.label2.TabIndex = 3;<br />
this.label2.Text = "Send To SecondClass:";<br />
this.label2.Click += new System.EventHandler(this.label2_Click);<br />
this.button1.Location = new System.Drawing.Point(24, 120);<br />
this.button1.Name = "button1";<br />
this.button1.Size = new System.Drawing.Size(240, 32);<br />
this.button1.TabIndex = 4;<br />
this.button1.Text = "Send To Second Class";<br />
this.button1.Click += new System.EventHandler(this.button1_Click);<br />
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);<br />
this.ClientSize = new System.Drawing.Size(292, 273);<br />
this.Controls.Add(this.button1);<br />
this.Controls.Add(this.label2);<br />
this.Controls.Add(this.txtToSecondClass);<br />
this.Controls.Add(this.label1);<br />
this.Controls.Add(this.txtFromSecondClass);<br />
this.Name = "Form1";<br />
this.Text = "Form1";<br />
this.ResumeLayout(false);<br />
<br />
}<br />
#endregion<br />
<br />
[STAThread]<br />
static void Main() <br />
{<br />
Application.Run(new Form1());<br />
}<br />
private void button1_Click(object sender, System.EventArgs e)<br />
{<br />
SecondClass.transferText(txtToSecondClass.Text);<br />
}<br />
<br />
private void label2_Click(object sender, System.EventArgs e)<br />
{<br />
<br />
}<br />
}<br />
}<br />
SecondClass.cs:
<br />
using System;<br />
<br />
namespace TestApp<br />
{<br />
public class SecondClass<br />
{<br />
public static void transferText(string x)<br />
{<br />
Form1 formObj = new Form1();<br />
formObj.txtFromSecondClass.Text = x;<br />
}<br />
}<br />
}<br />
Any input would be awesome. Thanks again for such a great site =)
P.S. This isn't an example of the real code I am working on, but does illustrate the problem I have. I know the above project can be very simply accomplished in one class.
|
|
|
|
|
your problem is that you are creating a new instance of form1.
You need to pass in the existing instance of form1
transfertext to take the existing instance
public static string transfertext(System.Windows.Forms.Form x)
{
return x.txtFromSecondClass.Text;
}
usage
string value = SecondClass.transfertext(this);
best of luck
Forever Developing
|
|
|
|
|
Hey thanks for the reply!
... and here is the but hehehe ...
Can you treat me like a moron, and show me the rewritten methods for each class? I just can't wrap my mind around this...
|
|
|
|
|
..\..\..\SecondClass.cs(12): 'System.Windows.Forms.Form' does not contain a definition for 'textFromSecondClass'
That is what I get when I try to use your code. I guess I have some reading to do.
Thanks anyway.
|
|
|
|
|
Hi there,
I'm kind of new to C# (that might explain this question).
I stuck with my code when I have to:
1. implement a list of pairs (string, int)
2. sort it by int
Could you give me some hints on points 1 & 2?
Thanks
Domo_
|
|
|
|
|
The first step is to define a structure to hold your data. I used the property accessors in this case because it is part of my habit. These aren't required but are there for good measure.
The second step is to define a class that will determine how you will sort your data. This class derives from the IComparer interface and implements the Compare method. For more robust code you could check the instance type of the objects to be sure they are of the Pair type.
The third step is to sort your array of data. This can be done by using the static Sort utility method on the Array class. This method will accept any standard Array and sort it using your comparer.
using System;
using System.Collections;
namespace ConsoleApplication1
{
class Pair
{
public String MyString
{
get
{
return _string;
}
set
{
_string = value;
}
}
public int MyInt
{
get
{
return _int;
}
set
{
_int = value;
}
}
private int _int;
private String _string;
}
class PairComparer : IComparer
{
#region IComparer Members
public int Compare(object x, object y)
{
Pair left = (Pair)x;
Pair right = (Pair)y;
return left.MyInt.CompareTo(right.MyInt);
}
#endregion
}
class Class1
{
[STAThread]
static void Main(string[] args)
{
Pair[] pairs = new Pair[3];
pairs[0] = new Pair();
pairs[0].MyInt = 10;
pairs[0].MyString = "Ten";
pairs[1] = new Pair();
pairs[1].MyInt = 3;
pairs[0].MyString = "Three";
pairs[2] = new Pair();
pairs[2].MyInt = 1;
pairs[2].MyString = "One";
Array.Sort(pairs, new PairComparer());
foreach (Pair pair in pairs)
{
Console.WriteLine(
"Pair: {0} {1}" ,
pair.MyInt,
pair.MyString);
}
}
}
}
-
Drew
|
|
|
|
|
//Wrap this in a command button click event
System.Collections.ArrayList arylist = new ArrayList();
for (int i = 10; i >= 0; i--)
{
pairs values;
values.stringvalue = i.ToString();
values.intvalue = i;
arylist.Add(values);
}
IComparer comp = new MyCompare();
arylist.Sort(comp);
for(int i = 0; i<=10; i++)
{
pairs values = (pairs)arylist[i];
MessageBox.Show(values.intvalue.ToString() + " " + values.stringvalue);
}
//end wrap
private class MyCompare: System.Collections.IComparer
{
public int Compare(object x, object y)
{
pairs valuex = (pairs)x;
pairs valuey = (pairs)y;
if (valuex.intvalue < valuey.intvalue)
return -1;
if (valuex.intvalue > valuey.intvalue)
return 1;
return 0;
}
}
private struct pairs
{
public string stringvalue;
public int intvalue;
}
Forever Developing
|
|
|
|
|
|
Has anyone used embedded user controls (http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=187&printer=t) in a real application. I didn't even know this was possible until today.
I am building an app where the client wants drag-and-drop upload of multiple files. There is a Java tool out there, but I was considering creating an embeded user control.
Any suggestions? (No need to mention basic platform compatability issues).
|
|
|
|
|
I would see no real issues except that you will need to install .NET Framwork on the all of the machines that need this functionality.
Best of Luck
Forever Developing
|
|
|
|
|
hi all,
i created a windows service and build a controller to that service, just as the SQL Server Service Manager if any saw it. Anyway the problem is i want to control the startup mode of the windows service (Automatic, Manual, Disabled), in .Net you can specify this in the Service Installer but after that i didn't find a way to change it.
Does anyone know a way to change the startup mode of the Windows Service in .Net? i think that i foud a way to do it in win32, but i need it in .Net...
thanks for help
|
|
|
|
|
The Service startup mode can be changed in C# using System.Management namespace. a sample method is given below.
public bool ServiceStartModeUpdate(string serviceName, string startMode, out string errorMsg)
{
uint success = 1;
errorMsg = string.Empty;
string filter =
String.Format("SELECT * FROM Win32_Service WHERE Name = '{0}'", serviceName);
System.Management.ManagementObjectSearcher query = new ManagementObjectSearcher(filter);
if (query == null) return false;
try
{
ManagementObjectCollection services = query.Get();
foreach (ManagementObject service in services)
{
ManagementBaseObject inParams =
service.GetMethodParameters("ChangeStartMode");
inParams["startmode"] = startMode;
ManagementBaseObject outParams =
service.InvokeMethod("ChangeStartMode", inParams, null);
success = Convert.ToUInt16(outParams.Properties["ReturnValue"].Value);
errorMsg = GetErrorMsg(success) + "\n Try \"Run as Administrator\" ";
}
}
catch (Exception ex)
{
errorMsg = ex.Message;
throw;
}
return (success == 0);
}
|
|
|
|
|
hellow to all
i had download some projects from this site but when i try to run them a get error and it is
the applecation for project (C:\.....THE PATH)
make sure the applecation for project type (.csproj) is installed
thank u for the help \!
|
|
|
|
|
|
Hello Gurus,
Happy Friday to Gurus. I appreciate for your looking into this and any tips or tricks.
Thanks & Happy Coding
Roughly what I am trying to do is:
1. Get a video frame from a webcam ExternalSource.
2. Some modifications to the bitmap of the current frame.
2. Save this frame to a Image.bmp file.
3. Display the image from Image.bmp on an ImageBox
4. Repeat from Step 1, to achieve a video-like show.
The Problem: can't delete or overide Image.bmp file with new images.
string sFileName = "C:\Temp\Image.bmp";
Image img;
// ExternalSOurce = an image frame extracted from a video stream from a webcam
ExternalSource.SaveFile(sFileName);
img = Image.FromFile(sFileName);
picControl.Image = img;
img.Dispose(); // won't help
img = null; // won't help
While (!DONE)
{
// Can't delete because file is locked until img is destroyed!
File.Delete(sFileName);
ExternalSource.SaveFile(sFileName);
img = Image.FromFile(sFileName);
picControl.Image = img;
img.Dispose(); // won't help
img = null; // won't help
}
|
|
|
|
|
I think you're looking for this[^] little workaround for that problem.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Dave, thanks so much for the great tip, Guru! You're always great and know so much.
Have a great weekend
Khang
|
|
|
|