|
Hello,
I have written a 2 page program. The first page connects to a remote database so that queries can be run from the second page. Once the connection is established, that second page is navigated to using a button that calls the ShowDialog command. On the second page, I construct a query from an assortment of listview boxes and send the results to a DataGridView. So far so good....everything works to this point. Now, I have a rich text box that shows the final SQL statement that was used to create the GridView. I want to be able to modify the command manually and run it again to get new results without leaving this page...seems simple enough...however, when I try to run the command the second time using a different run button, the first page reappears when the code is completed. I stepped through it and found that the program is sending me back to the first page and stopping in the button code that called the ShowDialog command; and then I am stuck there. I do not know why I am being forced to leave the second page and why the code doesn't simply run and put the new information into the DataGrid. Please tell me where I am going wrong.....Thank you in advance....Pat
|
|
|
|
|
Well, at a rough guess I would say there is either a code bug or a logic error in your program. However, without seeing some of your code it's difficult to be more precise.
The best things in life are not things.
|
|
|
|
|
Richard,
Thank you for your reply....Yes, I agree with your input. There was so much code on this that I did not think it was OK to show it all, so I did not. I think that Bob (below) has an idea regarding the modality so I will pursue that line right now. In the meantime, I created a work around (which did not fix the issue but allowed me to get the desired result a different way). I wanted to reply though and thank you for taking the time to assist. I know you time is valuable and I appreciate it.
Best Regards, Pat
|
|
|
|
|
No worries, hope you resolve your problem.
The best things in life are not things.
|
|
|
|
|
ShowDialog returns when the form it calls is closed. So either the event handler on the second button is closing the modal form (directly or indirectly), or you have set a DialogResult on that button (which causes it to close the form).
|
|
|
|
|
Bob,
Thank you for your reply. I will further examine the code as per your suggestion and post the solution if I am able to correct it. In the meantime, I have created a work around to solve the immediate problem, but I will not be happy until I understand the "why" of it so I can do it correctly next time.
Best Regards, Pat
|
|
|
|
|
this message apper to me
invalid object name 'ControlMessage'
|
|
|
|
|
Did this happen in a dream or something??
You might want to explain a bit more about what you're doing and post the code snippet where this message is popping up.
|
|
|
|
|
|
hey
exemple
<br />
int i = 0;<br />
while (i<20000000)<br />
{<br />
<br />
if (i=5000) {<br />
PictureBox.image = "x.gif" <br />
}<br />
i++;<br />
}<br />
<br />
the problem is that the picture box does not change the image if i=5000 ;
it changed after loop "while "
thx
|
|
|
|
|
It would seem you're blocking the UI thread - you're making the UI thread do so much work it can't keep up with all the other requests like to repaint. I assume you're executing this code in response to some control event or initialization code?
While I'm sure this is just an example, assuming there's other work you should do that work in a worker thread and invoke the property setting on the UI thread since many times executing code on a control in a different thread than in which it was created can cause issues.
First you'll need to define a method to set your image property.
private void SetImageLocation(string uri)
{
if (pictureBox1.InvokeRequired)
{
Action<string> setImageLocation = SetImageLocation;
pictureBox1.Invoke(setImageLocation, new object[] { uri });
}
else
{
pictureBox1.ImageLocation = uri;
}
}
To create a worker thread, consider using the ThreadPool like so:
private void DoWork()
{
for (int i = 0; i < 20000; ++i)
{
if (i == 5000)
{
SetImageLocation("x.gif");
}
}
}
private void button1_Click(object sender, EventArgs e)
{
ThreadPool.QueueUserWorkItem(DoWork);
}
This posting is provided "AS IS" with no warranties, and confers no rights.
Program Manager II
Visual Studio Professional Deployment Experience
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
The way Heath describes is the correct way to handle this type of situation.
The really bad practice, which you should not do (but is handy when you just want to throw something together as a quick test or throwaway util, is;
int i = 0;
while (i<2000000000)
{
if (i==5000) {
pictureBox1.Load("x.gif");
Application.DoEvents();
}
i++;
}
This forces the message pump to catch up.
But as i said already, this is bad practice, and should not be used in production code and will probably lead to you being shot by the code police
o see why you should not be using it in production code, look here; http://blog.somecreativity.com/2007/11/19/a-look-at-doevents/[^], or search Google for DoEvents and it will become obvious its baaaaaaaad.
So why tell you about then? Well, rather than you find out on your own and not know its bad and start to use it, its better to be told about it and learn why its bad and should be avoided, if that makes sense!
|
|
|
|
|
abdellah ab wrote: if (i=5000) {
I'm surprised that even compiles!
However, you may need to force a refresh of the control, not that I recommend you do that.
CQ de W5ALT
Walt Fair, Jr., P. E.
Comport Computing
Specializing in Technical Engineering Software
|
|
|
|
|
|
I think he was writing pseudo code, as there are a few ommissions
|
|
|
|
|
Indeed, and we knew what he meant. I'm very happy that doesn't compile, actually, because I do it accidentally quite a lot and if I were writing C I would get my fingers burnt every time.
|
|
|
|
|
Hi wold of CodeProject!
How can i Create a dialup Connection via C# Windows Application.
and also i wanna to dialup to internet directly via the connection i created.
Thanks!
|
|
|
|
|
|
I fear this question has been asked a thousand times, but the keywords I can think of are too common and give millions of answers and I can't find the correct one.
In a Windows forms application (C# 2008) I have a base form with a datagridview on it, and I want to catch the cell click events on the descendants. If the contents of the datagridview is clicked I have to create a custom event, but if columnhead is clicked I allways want the same event.
I hoped I could achieve this by defining the common event in the base class, followed by the custom event in the descendant. I defined a break in the code of the base class when column head is clicked, the break is correctly handled, but the code resumes in the descendant's form class.
How can I stop the code execution in the descendant form if I have a break in the base class ?
Thanks
Rob
|
|
|
|
|
That's how subtyping works. When you crete an instance of the child class, all code is executing against that instance. If the base class defines handlers for events like in your case, it still executes against that instance of the child class you created.
But what is it that's not working? Both events can be defined on the base class, like so:
internal abstract class MyFormBase : Form
{
DataGridView dataGridView1;
protected MyFormBase()
{
dataGridView1 = new DataGridView();
dataGridView1.CellClick += OnDataGridViewCellClick;
}
protected virtual string Message
{
get { return "Message from the base class."; }
}
protected virtual void OnDataGridViewCellClick(object sender, DataGridViewCellEventArgs e)
{
}
protected virtual void OnDataGridViewColumnHeaderMouseClick(object sender, DataGridViewBindingCompleteEventArgs e)
{
MessageBox.Show(Message, "Test");
}
}
internal class MyChildForm : MyFormBase
{
protected override string Message
{
get { return "Message from the child class."; }
}
protected override void OnDataGridViewCellClick(object sender, DataGridViewCellEventArgs e)
{
MessageBox.Show("You clicked a cell.", "Test");
base.OnDataGridViewCellClick(sender, e);
}
}
Now when you create an instance of MyChildForm and click a column header, you'll see "Message from the child class" even though the event handler is defined in the base class. It calls the virtual (overridable) property which IS the property on the MyChildForm instance. If you want to call the base class's method, use base .
You also overrided the CellClick event handler which will show "You clicked a cell." I also show an example of calling the base class's method though in this case it doesn't do anyway. If it would never do anything, define it as abstract instead of virtual and remove the body like so:
private abstract void OnDataGridViewCellClick(object sender, DataGridViewCellEventArgs e);
Hopefully this shows you an example of how polymorphism is working such that you can define your handlers in your base class but have it access the data - expectedly - in the child class.
This posting is provided "AS IS" with no warranties, and confers no rights.
Program Manager II
Visual Studio Professional Deployment Experience
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Thanks for your answer, although it is not what I wanted to hear....
Perhaps I was unclear, I'm creating an application with over a dozen forms with a datagridview on it. The datagridview contains buttons, and their action is dependent on the child form.
In the cellclickevent I get the rownumber, and when it's -1 the columnheader is clicked. I want all child forms to ignore the click of the columnheader and I wanted to achieve it like:
child form:
protected override void dgvSelect_CellClick(object sender, DataGridViewCellEventArgs e)
{
base.dgvSelect(sender, e);
custom cell click events;
}
I will solve it like this:
protected override void dgvSelect_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (base.dgvSelectHeaderClicked(sender, e) == true)
{
return;
}
custom cell click events;
}
Thanks for your time.
Rob
|
|
|
|
|
Holy sh*t! A rare sighting of the elusive Heath Stewert!!
I am honored to be in the presence of the greatness of one of CP's legends.
|
|
|
|
|
Thanks!
I might have to make that first one part of my sig.
This posting is provided "AS IS" with no warranties, and confers no rights.
Program Manager II
Visual Studio Professional Deployment Experience
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
how to draw a circle using double buffering.
please give coding
|
|
|
|
|
Please give effort and show what you have tried. Without that you will not get anything good from us.
I wasn't, now I am, then I won't be anymore.
|
|
|
|