在c#中加载新表单 [英] to load new form in c#

查看:83
本文介绍了在c#中加载新表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好b $ b

我用这个装了新表格



Form3 newform = new Form3();

this.Hide();

newform.ShowDialog();

this.Show();





但是如果只选择了组合框的值,我的意图是加载这个表格....现在这只适用于点击搜索按钮....组合框在form1



有人可以帮我编码吗?



谢谢

A-code

Hi
I have loaded new form using this

Form3 newform = new Form3();
this.Hide();
newform.ShowDialog();
this.Show();


but my intention is to load this form if only value of combobox selected.... now this will work for just click search button.... that combobox is in form1

Can some one help me with coding?

Thanks
A-code

推荐答案

你的代码,就像我一样想你知道,不会按原样编译,因为'SelectedValue拼错了。



'SelectedValue只会在你做的时候返回有意义的东西数据绑定并具有链接到控件的有效'ValueMember [ ^ ]。



'SelectedItem.ToString()可用于测试用户的在ComboBox中的选择,可以'SelectedIndex。



在'对话模式下显示另一个表单时隐藏主表单不是一个好主意;这不是用户期望Windows应用程序的行为方式。



我建议:
Your code, as I think you know, will not compile as it is, since 'SelectedValue is mis-spelled.

'SelectedValue is going to return something "meaningful" only if you are doing data-binding and have a valid 'ValueMember linked to the Control [^].

'SelectedItem.ToString() can be used to test the user's choice in the ComboBox, as can 'SelectedIndex.

Hiding a main form while you show another Form in 'Dialog mode is not a good idea; it's not the way users expect Windows Applications to behave.

I suggest:
// declare and create your instance of Form3 once
private Form3 f3 = new Form3();

//somewhere in your code
if (comboBox1.SelectedItem == null) return;

switch (comboBox1.SelectedItem.ToString())
{
    case "choice 1":
        f3.ShowDialog();
        break;
    case "choice 2":
        // whatever ...
        break;
}

或者,(效率更高):

switch (comboBox1.SelectedIndex)
{
    case -1:
        // no choice made
        break;
    case 0:
        // item 1 chosen
        f3.ShowDialog();
        break;
    case 1:
        // item 2 chosen
        // whatever ...
        break;
}


尝试这样的事情



try something like this

if(combobox.selectedvalue == "your desired value")
{
Form3 newform = new Form3();
this.Hide();
newform.ShowDialog();
this.Show();
}


这篇关于在c#中加载新表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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