|
Yes. There are 5 MyClass instances and you can use one method for all events. This is better :
private List<MyButton> myButtons = new List<MyButton>();
private void CreateButtons()
{
for(int i = 0; i < 5; i++)
{
MyButton myButton = new MyButton();
myButton.MyInt = i;
myButton.Click += new EventHandler(myButton_Click);
myButtons.Add(myButton);
}
}
private void myButton_Click(object sender, EventArgs e)
{
MyButton myButton = sender as MyButton;
if(myButton != null)
{
MessageBox.Show(myButton.MyInt.ToString());
}
}
public class MyButton : Button
{
private int myInt;
public int MyInt
{
get { return myInt; }
set { myInt = value; }
}
}
|
|
|
|
|
Thanks for the reply!
I think that was exactly what I was looking for. I'll try to implement this in my real case and I get back if I don't succeed.
|
|
|
|
|
You don't even need an extra class, you could store the MyClass reference inside the Button using myClass.MyBtn.Tag=myClass; so later an event handler could convert its sender to Button to MyClass instance.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
xkrja wrote: //Is it possible to get the value of 'myClass.MyInt'
//in this event handler since 'myClass.MyBtn' belongs
//to the same object?
In your example code: yes.
Just use myClass.MyInt . The lambda expression will capture the myClass variable, so every event handler will know to which MyClass instance it belongs.
You should not capture the i variable - that would always be 5 because the loop has terminated when the event executes.
But capturing variables declared inside the loop will give you the value from the corresponding iteration.
|
|
|
|
|
how do i insert a new line character.
i want to display a sentence in next line. i selected the multiline property. i want to display the second line in next line of text box like wise \n in C lang
can any one help pls
This code was posted by me...
|
|
|
|
|
|
Just add "\r\n" to the end of your line and the next text will be on the next line.
textbox1.text = string.format("{0}\r\n{1}","this is on line 1","this is on line 2");
Bob
Ashfield Consultants Ltd
Proud to be a 2009 Code Project MVP
|
|
|
|
|
Environment.NewLine will do that.
It's not necessary to be so stupid, either, but people manage it. - Christian Graus, 2009 AD
|
|
|
|
|
Hi all.
I made an application for communicating with serial port.
But there is a trouble.
In the DataReceived event when I wanna fill textbox with data that given from serial port an Exception has occurred.
http://www.picfront.org/d/bV1xDokvV/u.png[^]
Could you please guide me how I can resolve it ?
|
|
|
|
|
void serialPort1_DataReceived(object sender, ...)
{
Invoke(new MethodInvoker(AddText));
}
void AddText()
{
textBox1.Text = serialPort1.ReadExisting();
}
Search in google for cross-thread-exception. You cannot change something in textbox from another thread.
|
|
|
|
|
|
This article[^] explains and solves your problem.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Hi All,
I'd like to make a separate thread in inserting of data into the Custom Collection I have and fires an event when its done so it would be bound to a grid.
Is it possible?
here is my example code.
public class Details : List<Details.Detail>
{
public delegate void _WorkComplete();
public event _WorkComplete WorkComplete;
public void AddDataSource(object value)
{
Thread worker = new Thread(new ParameterizedThreadStart(dowork));
worker.Start(value);
}
private void dowork(object value)
{
ArrayList list = (ArrayList)value;
foreach (Item item in list)
{
this.Add(new Detail { FIELD1 = item.Code1, FIELD2 = item.Code2 });
}
if (WorkComplete != null) WorkComplete();
}
public class Detail
{
private string field1;
private string field2;
public Detail()
{
field1 = string.Empty;
field2 = string.Empty;
}
public string FIELD1
{
get { return field1; }
set { field1 = value; }
}
public string FIELD2
{
get { return field2; }
set { field2 = value; }
}
}
}
so in the UI side i could do
private Details details = new Details();
protected void Page_Load(object sender, EventArgs e)
{
details.OnWorkComplete += new Details._WorkComplete(details_AddComplete);
details.AddDataSource(source1);
details.AddDataSource(source2);
}
void details_AddComplete()
{
grid1.DataSource = details;
grid1.DataBind();
}
|
|
|
|
|
Search in google for cross-thread-exception. You cannot change something in control from another thread.
"grid1.DataSource = details;" -> Cross Thread Exception
void details_AddComplete()
{
Invoke(new MethodInvoker(AddComplete));
}
void AddComplete()
{
grid1.DataSource = details;
grid1.DataBind();
}
|
|
|
|
|
that does not fire an error the "details" is created on the same thread. as the "grid1".
not the comment I expected but thanks for the reply.
modified on Wednesday, August 12, 2009 5:07 AM
|
|
|
|
|
Assuming you add all your sources in Page_Load, you could do this:
In AddDataSource, just add the source to a collection inside Details.
Add a method Details.StartProcessing() which starts the background work.
So Page_Load would look like:
details.AddDataSource( source1 );
details.AddDataSource( source2 );
details.StartProcessing();
}
As Stan said, your event will be fired on the background thread. Either use a BackgroundWorker instead of a Thread, or use Invoke before you update the grid.
Nick
----------------------------------
Be excellent to each other
|
|
|
|
|
Hi,
I want to perform an action before the beginning of an application installation.
I tried to override the event OnBeforeInstall (in the AddInCustomAction class) but I see that when that event is raised my files were already copied to the target directory.
The reason for what I want to do is:
I have a previous version of the application (without setup; just an exe) written in MFC.
Now I rewrite the application in C# .Net VS 2005, and the installation is done via a setup.
I don't want to change the name of the exe and nor the name of the Target directory.
so what I want to do is:
before the installation begin:
check that the previous verion doesn't exist.
if exists, remove the old exe
continue the installation
Any Ideas How can I do this?
Thanks,
|
|
|
|
|
Hello.
more detail:
I have a datagridview and i written save code for it ,but when i change the fied of datagridview Rows for test my save code,after i chenge field(immediately)and i press Eter key ,that row i chenged it be removed from datagridview,before i press save button .So i cann't save the changes.
But if i change the last row of datagridview this row not remove ,and i can save it exactly.
I do not know why other rows after change to remove.?
this is my save code:
private void myBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
SqlConnection objconnection = new SqlConnection(myconnection_string);
int i, idj, tedad, ghimat;
for (i = 0; i < myDataGridView.RowCount; i++)
{
idj = int.Parse((myDataGridView.Rows[i].Cells[0].Value).ToString());
tedad = int.Parse((myDataGridView.Rows[i].Cells[1].Value).ToString());
SqlCommand mycommand = new SqlCommand("UPDATE table1 SET tedad = @tedad WHERE (fid=@fid AND idj = @idj AND tedad ='0')", objconnection);
mycommand.Parameters.AddWithValue("@fid", fid);
mycommand.Parameters.AddWithValue("@idj", idj);
mycommand.Parameters.AddWithValue("@tedad", tedad);
objconnection.Open();
mycommand.ExecuteNonQuery();
objconnection.Close();
}
}
|
|
|
|
|
I have these two applications, one of which sends a screenshot image to the other. This works smoothly on most PCs, however some (older) PCs start receiving a lot of zeros after reading some of the data. How can I ensure that all the data being sent, is read? I don't know if this is a timeout issue, or what?
(I have found some solutions, such as WriteByte / ReadByte, but I need it to perform quite fast too, if possible)
The methods are as follows:
Sending:
if (nsStream.CanWrite)
nsStream.Write(bUp, 0, bUp.Length);
nsStream.Flush();
int x = 0;
while (nsStream.DataAvailable && x < dataReadWait)
{
x++;
}
return true;
Receiving:
if (nsStream.CanRead)
nsStream.Read(bDown, 0, bDown.Length);
nsStream.Flush();
return bDown;
At this point, it is worth saying that the Sender prior to sending the actual image, sends the length of the image (which is assigned to bDown), that is why data is read until the length of bDown...
Any help is so greatly appreciated!
|
|
|
|
|
I have sql (sql server 2005) table which is having StartDate and EndDate datetime columns.
<code>StartDate value inserting as 2009-08-11 00:00:00.000,
EndDate value inserting as 2009-08-11 23:59:00.000 (End of the day).</code>
This DB Server is located at london.
in my winforms(2.0) appliction i have typed dataset, is having same table schema.
Typeddatasset StartDate and EndDate DateTimeMode property set to UnspecifiedLocal.
i am populating this dataset with SP. SP is functioning properly.
returned data i am binding to DataGridView. when its biinding to datagridview,
<code>StartDate is showing as '8/11/2009 4:30 AM' instead of '8/11/2009 12:00 AM' and
EndDate is showing as '8/12/2009 4:29 AM' instead of '8/11/2009 11:59 PM'</code>
it seems problem is with databinding to dataset.
any sounds?
Ramana
modified on Wednesday, August 12, 2009 4:15 AM
|
|
|
|
|
Ramana. G.V wrote: This DB Server is located at london.
Where are you? I would guess it's just a time zone issue, that it's adjusting for your local time zone. Some very smart people worked very hard to make it work that way and now you say you want it the other way?
|
|
|
|
|
yes, if you have any other way to do. please help me
Ramana
|
|
|
|
|
I thought I had my Regular Expression working just fine, but then I found a weakness...
First off, I'm trying to parse a command line, so a Regex may not be the best tool, but it's so close! (And it's fine for testing purposes anyway.)
Given a command line like:
command param1 /options=( option1 , "option 2" , option3 ) "param 2"
I have no trouble getting most of this. Generally the worst part is that I have to get the contents of the options all together,
e.g. " option1 , "option 2" , option3 " , but that's not a big deal, I can parse it separately.
What I'm stumped by now, is if an option is in quotes and contains a right-parenthesis:
/options=( "(trouble)" )
The related piece of the Regex I'm using is:
"(?:/\\s*(?'Switch'\\w+)(?:\\s*=\\s*(?:(?:\"(?'Value'[^\"]*)\")|(?:\\((?'Value'[^\\)]*)\\)))|(?'Value'\\w+))?)"
I want "(trouble)" , I get "(trouble
Is there a way to get the Regex to recognize that the right-parenthesis is in quotes? I assume I need a balancing-group, but I can't wrap my head around it.
Edit:
I replaced ((?'Value'[^\\)]*) with (?'Value'(?:(?:\"[^\"]*\")|[^\\)]*?)*) and it seems to work.
modified on Wednesday, August 12, 2009 1:51 AM
|
|
|
|
|
hi,
I've some table of content or data. I've to show the content as table and charts in PDF using C#. Is there any possibility to display charts in PDF using some third party Dlls.
Pls let me know the solution.
Thanks in advance..........
-Ravi Vellanki
|
|
|
|
|