两个按钮一个弹出窗口,有两个事件 [英] two buttons one popup with two events

查看:93
本文介绍了两个按钮一个弹出窗口,有两个事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个按钮

1.button1

2.button2



一个弹出式按钮带一个按钮



如果我点击任何按钮我想打开两个按钮常见的弹出窗口。



和如果我点击button1然后在那个弹出按钮中打开弹出按钮我需要按钮1方法

如果我点击button2并在弹出按钮中打开弹出按钮我需要按钮2方法



怎么办呢



感谢提前

解决方案

那里你的帖子中有一个缺陷:两个按钮常见的弹出窗口并不确定。你的意思是同一个窗口对象?如果是这样,这不是问题:你应该在外部内容中有一个窗口变量:第一个按钮(两个中的任何一个,以先到者为准)创建窗口并显示它,第二个按钮只是改变同一窗口的内容。



从您的帖子中,您不清楚在窗口关闭时您想要对窗口做什么以及窗口关闭的原因。如果您不必关心在弹出窗口中保留可由用户修改的某些内容,则没有问题。例如:

 <   html  >  

< head >
< script >

var myPopup

function Create(){
myPopup = window .open( width = 200,height = 100);
}
function CreateShow(text){
if (! myPopup)
Create();
else if (myPopup.closed)
Create();
myPopup。 document .write(text);
myPopup。 document .close();
}

function First(){
CreateShow( 第一种方法);
}

function Second(){
CreateShow( 第二种方法);
}

< / script >
< / head >

< 正文 >
< 输入 type = 按钮 = 首先显示 onclick = First() > < br / >
< input type = 按钮 value = 显示第二个 onclick = 秒() >
< / body >

< / html >





如果情况会更多复杂,请解释一下。







我也想警告你:在很多场景,使用弹出窗口可以被认为是非常糟糕的。用户可能会非常烦恼,通过基于浏览器的插件拦截器阻止他们,等等。这不是因为技术本身如此糟糕;这是因为弹出窗口被无耻的广告商和极端糟糕的设计师严重滥用;并且还因为弹出窗口使用窗口控制而不受用户的控制。小心弹出窗口,在很多情况下,最好避免使用。



-SA


< blockquote>只需调用两个按钮的事件即可。你有没有试过谷歌或在MSDN上找到它?



按钮代码是,



  //  创建新的按钮实例 
按钮button1 = new Button();
Button button2 = new Button();
// 添加内容
button1.Content = 按钮1;
button2.Content = 按钮1;
// 添加点击处理程序
button1.Click + = Button1_Click;
button2.Click + = Button2_Click;





如果您没有按钮,上面的代码会生成按钮。否则,如果您已经解决,请使用 controlName.Click + = Event_Name 将点击处理程序附加到它们。 + = 会为您完成工作。



现在,您可以使用您在按钮的C#代码中给出的名称来创建事件。



我只创建一个事件处理程序,你可以自己创建第二个事件处理程序。他们会工作的。但我仍然会推荐你,因为它们都是同一个事件,不要创建两种方法。使用一个功能并从两个按钮调用它,但检查您正在接听电话的按钮。两次写相同的代码违反了不要重复自己的规则。



 < span class =code-keyword> public   void  Button1_Click( object  sender,EventArgs e ){
// 编写单击按钮时执行的代码
< span class =code-comment> // 以下是弹出窗口!
MessageBox.Show( 点击了按钮!);
}





这足以解决这个问题。


I have two buttons
1.button1
2.button2

one popup with one button

if i click any button i want to open popup that is common for both buttons.

and if i click button1 and that opens popup in that popup button i want button 1 method
and if i click button2 and that opens popup in that popup button i want button 2 method

how can do this

Thanks for advance

解决方案

There is one flaw in your post: "popup which is common for both buttons" is not anything certain. Do you mean "the same window object"? If so, it's not a problem: you should have one window variable in outside content: first button (any of the two, whichever comes first) creates window and shows it, the second one just changes content of the same window.

From your post, it's unclear what do you want to do with the window when it is closed and what should cause the window closed. If you don't have to care about preserving some content in the popup window which could be modified by the user, there is no a problem. For example:

<html>

    <head>
        <script>

            var myPopup

            function Create() {
                myPopup = window.open("", "", "width=200, height=100");
            }
            function CreateShow(text) {
                if (!myPopup)
                    Create();
                else if (myPopup.closed)
                    Create();
                myPopup.document.write(text);
                myPopup.document.close();
            }

            function First() {
                CreateShow("first method");
	    } 

            function Second() {
                CreateShow("second method");
            } 

        </script>
    </head>

<body>
    <input type="button" value="Show First" onclick="First()"><br/>
    <input type="button" value="Show Second" onclick="Second()">
</body>

</html>



If the scenario is going to be more complex, please explain it.

[EDIT]

I also want to warn you: in many scenarios, using popups can be considered pretty bad. The users can be highly annoyed by them, block them by the browser-based plug-in blockers, and so on. This is not because the technique itself is so bad; this is because pop-ups have been heavily abused by shameless advertisers and extremely bad designers; and also because pop-ups take window control out of user's control. Be careful with pop-ups, in many situations, they are best avoided.

—SA


Simply call the events for both buttons. Did you even try to Google it or find it at MSDN?

Button code would be,

// create new instance of buttons
Button button1 = new Button();
Button button2 = new Button();
// add the content
button1.Content = "Button 1";
button2.Content = "Button 1";
// add the click handler
button1.Click += Button1_Click;
button2.Click += Button2_Click;



The above code would generate the buttons if you don't have then. Otherwise if you have then settled, append the click handler to them using the controlName.Click += Event_Name. += would do the work for you.

Now you can create the events using the very name that you gave inside the C# code for the buttons.

I would create only one event handler, you can create the second one on your own. And they'll work. But I would still recommend you, since they're both the very same events, don't create two methods. Use one function and call it from both buttons but check for which button you're getting the call from. Writing the same code twice is against the rule of "Don't Repeat Yourself".

public void Button1_Click (object sender, EventArgs e) {
   // write the code to execute when button is clicked
   // following is the popup!
   MessageBox.Show("Button was clicked!");
}



This would be enough to work around with this task.


这篇关于两个按钮一个弹出窗口,有两个事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆