使用DropDownList中选择的数据填充TextBox1 [英] Populating TextBox1 with data selected in DropDownList

查看:67
本文介绍了使用DropDownList中选择的数据填充TextBox1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


如何填充
TextBox1
with the value在
中选择
DropDownList1好吗?



  1. 我双击DropDownList1并发现SelectedIndexChanged是附加到该对象的方法。






  1. 但是,当我运行网站并更改值时 - 没有任何反应





  1. 当我添加一个按钮并在按钮中使用相同的代码时,它的工作原理,问题是我需要点击按钮,
    然后页面刷新,这就是什么我想避免。我只想在DropDownList1中更改值时获得相同的效果








解决方案

您好btsDeveloper,


你需要使用"SelectedIndex"的"SelectedItem"。请查看下面的代码。希望这对你有帮助。

使用System; 
使用System.Windows.Forms;

namespace ComboBox_To_TextBox
{
public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
}

private void comboBox1_SelectedIndexChanged(object sender,EventArgs e)
{
textBox1.Text = comboBox1.SelectedItem .ToString();
}
}
}

输出:




How do I populate a TextBox1 with the value selected in DropDownList1 please ?

  1. I double clicked on the DropDownList1 and noticed that SelectedIndexChanged is the method attached to that object.

  1. But, when I run the website and change the value - nothing happens

  1. When I add a button and use the same code in the button, then it works, the problem is I need to click the button, then the page is refreshing and this is what I want to avoid. I would just like to get the same effect when I change the value in the DropDownList1


解决方案

Hi btsDeveloper,

You need to use "SelectedItem" insted of "SelectedIndex". Please look into the below code. Hope this helps you.

using System;
using System.Windows.Forms;

namespace ComboBox_To_TextBox
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox1.Text = comboBox1.SelectedItem.ToString();
        }
    }
}

Output:


这篇关于使用DropDownList中选择的数据填充TextBox1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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