C#通过点击按钮调用文件?

时间:2016-06-20 14:57:40

标签: c#

我有FormClass: Form1& Timer

在Form1中,我有一个看起来像这样的按钮

public void browseSoundToolStripMenuItem_Click(object sender, EventArgs e)
{
    OpenFileDialog dialog1 = new OpenFileDialog();

    dialog1.Title = "Browse to find sound file to play";
    dialog1.InitialDirectory = @"c:\";
    dialog1.Filter = "Wav Files (*.wav)|*.wav";
    dialog1.FilterIndex = 2;
    dialog1.RestoreDirectory = true;

    //PlaySound(dialog1.FileName, new System.IntPtr(), PlaySoundFlags.SND_SYNC);             
}

在Timer.cs中我有Event,如下所示:

public void alert_sound(object source, ElapsedEventArgs e)
{
    MessageBox.Show("Alert Sound ding ding ding");

    //PlaySound(dialog1.FileName, new System.IntPtr(), Form1.PlaySoundFlags.SND_SYNC);
    alert_timer.Stop();
}

问题是在Timer.cs中它表示当前上下文中不存在dialog1。如何将Form1中的对话框1调用到我的Timer课程中?

3 个答案:

答案 0 :(得分:2)

您已在browseSoundToolStripMenuItem_Click事件方法的本地scope中创建了dialog1变量。要从不同的事件访问它,您需要在类级别创建它。像这样:

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0x00000000
Triggered by Thread:  0

Thread 0 name:
Thread 0 Crashed:
0   CoreFoundation                  0x22475196 CFStringHasPrefix + 10 (CFInternal.h:627)
1   CoreText                        0x247af94e ___ZL36GetSFTrimCharSetForChineseOrJapanesePPK9TFontInfo_block_invoke + 158 (SplicedFontSupport.cpp:8959)
2   libdispatch.dylib               0x220c180e _dispatch_client_callout + 22 (init.c:819)
3   libdispatch.dylib               0x220d3172 dispatch_once_f$VARIANT$mp + 62 (once.c:60)
4   CoreText                        0x24761360 MakeSpliceDescriptor(unsigned long) + 648 (once.h:68)
5   CoreText                        0x2474d84e TSplicedFontStashImp::CopySpliceFontAtIndex(long) const + 74 (TDescriptorSource.cpp:3694)
6   CoreText                        0x247a9ea0 TSplicedFontStashImp::CopySplicedDescriptorForName(__CFString const*, bool) const + 32 (TDescriptorSource.cpp:3705)
7   CoreText                        0x2474cbca TSplicedFontStashImp::CopySplicedDescriptorsForRequest(__CFDictionary const*, unsigned long) const + 134 (TDescriptorSource.cpp:3727)
8   CoreText                        0x247a84c4 TDescriptorSource::CopyDescriptorsForRequest(__CFDictionary const*, __CFSet const*, CFComparisonResult (*)(void const*, void const*, void*), void*, unsigned long, TCFRef<__CFArray const*>*) const + 172 (TDescriptorSource.cpp:3630)
9   CoreText                        0x2474c0ea TDescriptorSource::CopyDescriptorForRequest(__CFDictionary const*, __CFSet const*, CFComparisonResult (*)(void const*, void const*, void*), void*, unsigned long) const + 86 (TDescriptorSource.cpp:1663)
10  CoreText                        0x2474bf1c TDescriptor::CreateMatchingDescriptorInternal(__CFSet const*, bool) const + 424 (TDescriptor.cpp:538)
11  CoreText                        0x2474bca8 TDescriptor::InitBaseFont(bool) + 24 (TDescriptor.cpp:642)
12  CoreText                        0x2475eaa2 TDescriptor::CreateMatchingDescriptor(__CFSet const*, float, unsigned long) const + 278 (TDescriptor.h:62)
13  CoreText                        0x2475e942 CTFontCreateWithFontDescriptor + 34 (CTFont.cpp:93)
14  CoreText                        0x2475d85a CTFontCreateUIFontForLanguage + 22 (CTFont.cpp:152)
15  UIFoundation                    0x26a7531c +[UIFont systemFontOfSize:traits:] + 360 (UIFont.m:279)
16  UIKit                           0x26ad6216 -[UILabel _commonInit] + 86 (UILabel.m:504)
17  UIKit                           0x26ad60b0 -[UILabel initWithFrame:] + 76 (UILabel.m:531)
18  UIKit                           0x26aeeaf4 -[UIView init] + 44 (UIView.m:2343)
19  UIKit                           0x26b6ed28 -[UINavigationItemView initWithNavigationItem:] + 196 (UINavigationBar.m:9834)
20  UIKit                           0x26debe9e -[UINavigationItem _defaultTitleView] + 130 (UINavigationBar.m:1469)
21  UIKit                           0x26b6ec34 -[UINavigationItem _titleView] + 116 (UINavigationBar.m:1479)
22  UIKit                           0x26c094bc -[UINavigationBar _removeItemsFromSuperview:] + 140 (UINavigationBar.m:6931)
23  UIKit                           0x26deed7c -[UINavigationBar _setDecodedItems:] + 496 (UINavigationBar.m:2697)
24  UIKit                           0x26dee99c -[UINavigationBar initWithCoder:] + 484 (UINavigationBar.m:2662)
25  UIKit                           0x2709910e UINibDecoderDecodeObjectForValue + 782 (UINibDecoder.m:705)
26  UIKit                           0x27098df4 -[UINibDecoder decodeObjectForKey:] + 296 (UINibDecoder.m:792)
27  UIKit                           0x26deaf16 -[UINavigationItem initWithCoder:] + 734 (UINavigationBar.m:548)
28  UIKit                           0x2709910e UINibDecoderDecodeObjectForValue + 782 (UINibDecoder.m:705)
29  UIKit                           0x27098df4 -[UINibDecoder decodeObjectForKey:] + 296 (UINibDecoder.m:792)
30  UIKit                           0x26f814a4 -[UIRuntimeConnection initWithCoder:] + 160 (UIRuntimeConnection.m:23)
31  UIKit                           0x2709910e UINibDecoderDecodeObjectForValue + 782 (UINibDecoder.m:705)
32  UIKit                           0x270990a0 UINibDecoderDecodeObjectForValue + 672 (UINibDecoder.m:602)
33  UIKit                           0x27098df4 -[UINibDecoder decodeObjectForKey:] + 296 (UINibDecoder.m:792)
34  UIKit                           0x26f80a0a -[UINib instantiateWithOwner:options:] + 1110 (UINib.m:345)
35  UIKit                           0x26e33aa2 -[UIViewController _loadViewFromNibNamed:bundle:] + 322 (UIViewController.m:2309)
36  UIKit                           0x26c06ffa -[UIViewController loadView] + 142 (UIViewController.m:2438)
37  UIKit                           0x26acca1e -[UIViewController loadViewIfRequired] + 150 (UIViewController.m:2537)
38  UIKit                           0x26acc970 -[UIViewController view] + 24 (UIViewController.m:2598)
39  UIKit                           0x26ad3638 -[UIWindow addRootViewControllerViewIfPossible] + 68 (UIWindow.m:1445)
40  UIKit                           0x26ad0b3c -[UIWindow _setHidden:forced:] + 276 (UIWindow.m:1529)
41  UIKit                           0x26b45a5c -[UIWindow makeKeyAndVisible] + 48 (UIWindow.m:4929)
42  Electrical Calculations FREE    0x0008bb68 0x4d000 + 256872
43  UIKit                           0x26b41784 -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 392 (UIApplication.m:1865)
44  UIKit                           0x26d692d0 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 3080 (UIApplication.m:2112)
45  UIKit                           0x26d6d284 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1588 (UIApplication.m:3355)
46  UIKit                           0x26d8183c __84-[UIApplication _handleApplicationActivationWithScene:transitionContext:completion:]_block_invoke3286 + 36 (UIApplication.m:12263)
47  UIKit                           0x26d6a7b2 -[UIApplication workspaceDidEndTransaction:] + 134 (UIApplication.m:2748)
48  FrontBoardServices              0x23bf6c06 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 18 (FBSSerialQueue.m:158)
49  FrontBoardServices              0x23bf6ab8 -[FBSSerialQueue _performNext] + 224 (FBSSerialQueue.m:177)
50  FrontBoardServices              0x23bf6db8 -[FBSSerialQueue _performNextFromRunLoopSource] + 48 (FBSSerialQueue.m:206)
51  CoreFoundation                  0x22515dfe __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 14 (CFRunLoop.c:1761)
52  CoreFoundation                  0x225159ec __CFRunLoopDoSources0 + 452 (CFRunLoop.c:1807)
53  CoreFoundation                  0x22513d5a __CFRunLoopRun + 794 (CFRunLoop.c:2536)
54  CoreFoundation                  0x22463228 CFRunLoopRunSpecific + 520 (CFRunLoop.c:2814)
55  CoreFoundation                  0x22463014 CFRunLoopRunInMode + 108 (CFRunLoop.c:2844)
56  UIKit                           0x26b3ac3c -[UIApplication _run] + 524 (UIApplication.m:2578)
57  UIKit                           0x26b35188 UIApplicationMain + 144 (UIApplication.m:3772)
58  Electrical Calculations FREE    0x00082920 0x4d000 + 219424
59  libdyld.dylib                   0x2210b872 start + 2 (start_glue.s:64)

答案 1 :(得分:2)

在您的示例中,需要在模块级别声明dialog1。您在browseSoundToolStripMenuItem_Click()方法中声明了它,因此dialog1只有该方法的范围。

移动此行:

OpenFileDialog dialog1 = new OpenFileDialog();

到表单的顶部。

答案 2 :(得分:1)

您必须将文件名存储在可由其他方法调用的变量中。

string filename = dialog1.Filename;

可以将filename变量用作类级变量,但这是一个建议,因为我对你的类及其布局一无所知。