问题描述:

In the following code Im having list box for 2 text box ,currently its working but I have two question

1.suppose I need to drag for more than 2 text box ,instead of copy the code can I write it

in dynamic way to support 5 text box for instance ?

this is the code for the two text boxes

private void textbox1_PreviewDrop(object sender, DragEventArgs e)

{

string name = e.Data.GetData(DataFormats.StringFormat).ToString();

textbox1.Text += name;

textbox1.Focus();

textbox1.CaretIndex = textbox1.Text.Length;

e.Handled = true;

_UsersList.Remove((User) listbox1.SelectedItem);

}

private void textbox2_PreviewDrop(object sender, DragEventArgs e)

{

string name = e.Data.GetData(DataFormats.StringFormat).ToString();

textbox2.Text += name;

textbox2.Focus();

textbox2.CaretIndex = textbox2.Text.Length;

e.Handled = true;

bool remove = _UsersList.Remove((User)listbox1.SelectedItem);

}

The second question in the following code I check if the text box already have value,if so its not allowed to drag (the Or statement for text box 1 & 2 )

another value to it ,there is better way to write it if i will have more than 2 text boxes?

private void listbox1_SelectionChanged(object sender, SelectionChangedEventArgs e)

{

if (e.AddedItems.Count > 0 && (string.IsNullOrEmpty(textbox1.Text) || string.IsNullOrEmpty(textbox2.Text)))

{

if (listbox1.SelectedItems.Count > 0)

{

var mySelectedItem = listbox1.SelectedItem as User;

...

}

}

}

in the xaml

 <TextBox x:Name="textbox1"

AcceptsReturn="True"

AllowDrop="True"

DragOver="textbox1_DragOver"

PreviewDrop="textbox1_PreviewDrop"

<TextBox x:Name="textbox2"

AcceptsReturn="True"

AllowDrop="True"

DragOver="textbox2_DragOver"

PreviewDrop="textbox2_PreviewDrop"

网友答案:

Question 1: Yes you can create a common method and pass in the parameters. Something like this:

    private void textbox_PreviewDrop(object sender, DragEventArgs e)
    {
       string name = e.Data.GetData(DataFormats.StringFormat).ToString();

        TextBox textbox = (TextBox)sender;
        textbox.Text += name;
        textbox.Focus();
        textbox.CaretIndex = textbox.Text.Length;
        e.Handled = true;
        bool remove = _UsersList.Remove((User)listbox1.SelectedItem);
    }

   <TextBox x:Name="textbox1"  
              AcceptsReturn="True"
              AllowDrop="True" 
              DragOver="textbox1_DragOver"
              PreviewDrop="textbox_PreviewDrop" 

  <TextBox x:Name="textbox2"  
              AcceptsReturn="True"
              AllowDrop="True" 
              DragOver="textbox2_DragOver"
              PreviewDrop="textbox_PreviewDrop" 

Question 2: Your code looks right. You will have to check for Null/Empty for each textbox. I don't see a better way.

网友答案:

Have one common handler for all textBoxes. You can get textBox from the sender:

private void textbox_PreviewDrop(object sender, DragEventArgs e)
{
    TextBox textBox = (TextBox)sender;
    string name = e.Data.GetData(DataFormats.StringFormat).ToString();
    textBox.Text += name;
    textBox.Focus();
    textBox.CaretIndex = textBox.Text.Length;
    e.Handled = true;
    bool remove = _UsersList.Remove((User)listbox1.SelectedItem);
}
相关阅读:
Top