|
One of the ways is:
From Form1 get Height and Width of Form2 by:
Form2 frm2 = new Form2();
int height = frm2.Height;
int width = frm2.Width;
I Love T-SQL
"Don't torture yourself,let the life to do it for you."
If my post helps you kindly save my time by voting my post.
www.aktualiteti.com
|
|
|
|
|
Assuming you have a reference to the other form then you can access its properties.
It's time for a new signature.
|
|
|
|
|
thanks first.
if i have such dialogs
InspectionImageDialog m_ImgDlg = new InspectionImageDialog();
InspectionResultDialog m_RltDlg = new InspectionResultDialog();
Intel_Amd_InfoDialog intelamdDialog = new Intel_Amd_InfoDialog();
i would like to only new once and i can use this instance in anywhere of my project. how to do that? so far, i have 3 forms above and 1 mainDlg, so i want to control 3 dialogs in my mainDlg, also sometimes i want to get some property in other dialog. so how to new a instance and then i can use this instance freely and get its property and asign value or get value there.
|
|
|
|
|
|
Keep the references to your dialogs or forms in your main form class or make them global so that you can refer to them in any module.
It's time for a new signature.
|
|
|
|
|
how to keep the reference for dialog or forms in my main form class? could u give me an example?thanks
|
|
|
|
|
I don't quite understand your problem with this. Just add a variable of the correct form type to your main form, create the new form and save the reference. You could do this in your constructor or create a special method that does it on demand; either way your other forms will remain until you destroy them. Then you can use that from anywhere in your main form code.
It's time for a new signature.
|
|
|
|
|
If you want to access an object instance from anywhere, the Singleton pattern[^] is the recommended method. Are you sure you want to do this for three forms though? Static/Singletons have their place and uses but can often be the result of poor design, poor undestanding of OOP, lazyness. I'm not saying "don't", just be sure that it is actually required!
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
Using White in .net
how shall i get the DatagridView object in White
thanks in advance
|
|
|
|
|
Your question is not very clear.
What is 'White'?
Do you mean the color White, or are you referring to a third party library called White?
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
In .net we've got several timers, but all of them specify an interval to elapse rather than a specific time to come. I'd like to implement a timer which specifies a specific time, say to be triggered at the time of 10:25:15am (hour:minute:second). Using polling tech to determine if it's the right time to trigger is a very bad way and inefficient, I'd like to put the timer to sleep, then when the time is coming it receives a signal from the system to be woken up and then do its work.
One way to do this is still based on the Threading.Timer, convert how long to wait for the specific time into interval. But is there any mechanism provided by .net directly supporting this scenario to signal the thread at specific time?
Many thanks.
|
|
|
|
|
I'm not aware of any explicit support for a due time.
For a 1-second resolution I would just pick some timer (Windows.Forms/Threading/Timers depending on circumstances) and use DateTime and TimeSpan classes to calculate and set the delay.
For a finer resolution, I'd start with the above, then probably execute some polling loop within the final second, including a Thread.Sleep(ms) with a number of milliseconds fitting the resolution (assuming >= 50)
Whatever you do, keep in mind things could go wrong in several ways:
- your PC might get very busy while calculating and setting the due time, resulting in a late call;
- your PC might get very busy at the due time, so the requested action may actually run somewhat later;
- your PC might be switched off or crash in the mean time.
Depending on your requirements a Windows scheduled task may or may not be a better option.
|
|
|
|
|
Yep, Windows scheduled task may be a way. But it needs to be reused/consumed by other programs, so using Windows scheduled task is out of scope.
|
|
|
|
|
The best way to do what you have requested is to use Threading.Timer as you have already noted.
Timers aren't meant to be used this way though, that is to wake up at a specific time only. Why don't you use the Scheduled task if what you want is to run your service/process at a specific time.
|
|
|
|
|
Yeah, a Scheduled Task might be the way to go.
You could use a Timer, but it might not be very good for that purpose -- have it wait for half (or 90% or something) of the remaining time, check, repeat as necessary.
|
|
|
|
|
|
|
|
Xeno's dichotomy paradox[^] says you get tortoise kebab in the real world...
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
quartz.net has a number of timers, a cron type timer being amongst them - may be worth a look
'g'
|
|
|
|
|
thanks, i'll take a look at quartz.net.
|
|
|
|
|
Don't use a timer for this - take a look, rather, at using something like a Monitor where you can specify an elapsed interval before the monitor "wakes up". It would look something like this:
private static readonly object _syncLock = new object();
private bool _cancel;
private void ScheduleProcess(DateTime scheduledDate)
{
if (scheduledDate >= DateTime.Now)
throw new ArgumentOutOfRangeException("The scheduled time is set for a time that has already passed.");
TimeSpan waittime = scheduledDate - DateTime.Now;
lock (_syncLock)
{
Monitor.Wait(_syncLock, waittime);
}
}
public void Cancel()
{
_cancel = true;
lock(_syncLock)
{
Monitor.PulseAll(_syncLock);
}
} Create a background thread, and use ScheduleProcess to block access to your functionality until the time has passed. For instance:
public void DoSomething(DateTime scheduledTime)
{
SmartThreadPool stp = new SmartThreadPool();
stp.QueueWorkItem(new WorkItemCallback(RunProcess), scheduledTime);
}
private object RunProcess(object scheduledTime)
{
ScheduleProcess((DateTime)scheduledTime);
if (!_cancel)
{
}
return null;
} This example uses the SmartThreadPool[^].
|
|
|
|
|
Using Monitor.Wait() is a great solution but comes with its own set of problems. Most notably, it has no guaranteed accuracy. If you tell it to wait one hour, it will wait AT LEAST one hour. A number of factors can delay the response. It would be much safer to use Monitor.Wait() to suspend the thread, then switch to a more accurate but processor-intensive method for precision.
Also, when calculating the delay, since a number of factors (already pointed out by someone else) can cause this delay to be off, if you really want precision, I would calculate it a few times, then perform the reverse calculation for each result. This will allow you to be assured that your delay value is accurate.
|
|
|
|
|
thanks for pointing out this.
|
|
|
|
|
Thanks very much for your example.
|
|
|
|