none
TextBox change and ComboBox change textBox text RRS feed

  • Question

  • I try to do something like below image Image text has some wrong texts like PPPPPP GAAAA Edit to Ga Edit A05A0006 to A05A00063 

    My comboxBox is look like

    <ComboBox Name="ChooseComboBox" SelectedIndex="0" HorizontalAlignment="Left" Margin="70,30,0,0" VerticalAlignment="Top" Width="140" SelectionChanged="ChooseComboBox_SelectionChanged">
                    <ComboBoxItem Content="A05A00063"></ComboBoxItem>
                    <ComboBoxItem Content="A05A00071"></ComboBoxItem>
                    <ComboBoxItem Content="A05A00075"></ComboBoxItem>
                    <ComboBoxItem Content="A05A00079"></ComboBoxItem>
    </ComboBox>

    My Name: call projectNameTextBox

    My Path: call pathTextBox

    My function will work like 

    1.I change Name:Default to Name:test then Path:C:\\PPPPPP GAAAA Edit\A05A00063\Default will change to Path:C:\\PPPPPP GAAAA Edit\A05A00063\test will change

    2.If I change Path:C:\\PPPPPP GAAAA Edit\A05A0006\Default to Path:C:\\Ga Edit\A05A0006\test  then Name:Default will change to Name:test

    3.If I choose chooseCombobox from A05A00063 to A05A00071 then Path:C:\\PPPPPP GAAAA Edit\A05A00063\Default to Path:C:\\PPPPPP GAAAA Edit\A05A00071\Default

    Here is my code

    private void SetPathButton_Click(object sender, RoutedEventArgs e)
            {
                FolderBrowserDialog folderDialog = new FolderBrowserDialog();
                folderDialog.SelectedPath = "C:\\";
                DialogResult result = folderDialog.ShowDialog();
                if (result.ToString() == "OK")
                {
                    path = folderDialog.SelectedPath+ "\\PPPPPP GAAAA Edit\\A05A00063";
                    if (!(projectName == null))
                    {
                        path += projectName;
                       
                    }//end if
                    else
                    {

                    }//end else
                   
                   
                }//end if
            }

    private void projectNameTextBox_TextChanged(object sender, TextChangedEventArgs e) { try { projectName = projectNameTextBox.Text; if (projectName != null) { }//end if if (!IsLoaded) return; if (IsProjectNameChanging) return; var m = Regex.Match(pathTextBox.Text, @"(\\PPPPPP GAAAA Edit\)(?i)(^.*?\\)(.*?)(\\s*)$"); if (m.Success) { IsPathChanging = true; try { pathTextBox.Text = m.Groups[1].Value + m.Groups[2].Value+projectNameTextBox.Text; } finally { IsPathChanging = false; path = pathTextBox.Text; } } } catch { } } private void PathTextBox_TextChanged(object sender, TextChangedEventArgs e) { if (!IsLoaded) return; if (IsPathChanging) return; var m = Regex.Match(pathTextBox.Text, @"(?i)(^.*?\\)(.*?)(\\PPPPPP GAAAA Edit\\s*)$"); if (m.Success) { IsProjectNameChanging = true; try { projectNameTextBox.Text = m.Groups[2].Value; projectNameTextBox.Text= projectNameTextBox.Text.Split('\\')[1] ; } finally { IsProjectNameChanging = false; path = pathTextBox.Text; } }//end if } private void ChooseComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { try { switch (ChooseComboBox.SelectedIndex) { case 0: break; case 1: break; case 2: break; case 3: break; } } catch { } }


    How to those things thanks



    • Edited by CY_Chen Friday, October 18, 2019 2:36 AM Fix error
    Friday, October 18, 2019 2:23 AM

Answers

  • Hi Kyle

    ChooseComboBox_SelectionChanged code will wrong like below

    How to solve this?

    Thanks

    In WPF, try the following code in "SelectionChanged".

        string str = pathTextBox.Text.Split('\\')[pathTextBox.Text.Split('\\').Length - 2];
        ComboBoxItem cbi = (ComboBoxItem)(sender as System.Windows.Controls.ComboBox).SelectedItem;
        string selectedText = cbi.Content.ToString();
        pathTextBox.Text = pathTextBox.Text.Replace(str, selectedText);

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by CY_Chen Tuesday, October 22, 2019 12:26 AM
    Monday, October 21, 2019 9:47 AM
    Moderator

All replies

  • Hi CY_Chen,

    Hope the following code may help you.

        private void SetPathButton_Click(object sender, EventArgs e)
        {
            // Simulate the operation that set path
            tbName.Text = "Default";
            tbPath.Text = @"C:\\PPPPPP GAAAA Edit\A05A00063\Default";
        }
    
        // 1.change the text in tbPath
        private void tbName_TextChanged(object sender, EventArgs e)
        {
            string[] strs = tbPath.Text.Split('\\');
            string path = "";
            for (int i = 0; i < strs.Length - 1; i++)
            {
                path += strs[i] + "\\";
            }
            tbPath.Text = path + tbName.Text;
        }
    
        // 2.change the text in tbName
        private void tbPath_TextChanged(object sender, EventArgs e)
        {
            tbName.Text = tbPath.Text.Split('\\')[tbPath.Text.Split('\\').Count() - 1];
        }
    
        // 3.replace "A05A00063" in the path
        private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            string str = tbPath.Text.Split('\\')[tbPath.Text.Split('\\').Count() - 2];
            tbPath.Text = tbPath.Text.Replace(str, comboBox.Text);
        }

    The test result,

    But I am not quite understand the "Ga Edit" in "function 2" is. Can you elaborate on this?

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, October 18, 2019 6:01 AM
    Moderator
  • Hi Kyle thanks for answering

    I try your code 

    I got some errors

    1. I choose location c:\temp it will has problem 

    2. comboBox choose is wrong I chose xxxx71 but I  got xxxx63

    Friday, October 18, 2019 7:30 AM
  • Hi Kyle thanks for answering

    I try your code 

    I got some errors

    1. I choose location c:\temp it will has problem 

    2. comboBox choose is wrong I chose xxxx71 but I  got xxxx63

    Could you provide any detailed info about the errors?

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, October 18, 2019 7:35 AM
    Moderator
  • Hi Kyle

    I face the situatuion at

    I try to edit name Default I try to delete 't' let it become Defaul

    I expect path will like C:\\PPPPPP GAAAA Edit\A05A0006\Defaul

    But I get same as name Defaul not C:\\PPPPPP GAAAA Edit\A05A0006\Defaul

    Monday, October 21, 2019 3:47 AM
  • Hi Kyle

    I face the situatuion at

    I try to edit name Default I try to delete 't' let it become Defaul

    I expect path will like C:\\PPPPPP GAAAA Edit\A05A0006\Defaul

    But I get same as name Defaul not C:\\PPPPPP GAAAA Edit\A05A0006\Defaul

    It works fine in my test.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 21, 2019 5:35 AM
    Moderator
  • Hi Kyle 

    My result was like

    projectNameTextBox code

    string[] strs = projectNameTextBox.Text.Split('\\');
                    string p = "";
                    for (int i = 0; i < strs.Length - 1; i++)
                    {
                        p += strs[i] + "\\";
                    }
                    pathTextBox.Text = p + projectNameTextBox.Text;



    • Edited by CY_Chen Monday, October 21, 2019 6:44 AM Update code
    Monday, October 21, 2019 6:31 AM
  • Hi Kyle 

    My result was like

    Could you show me the "tbName_textChanged" code?

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 21, 2019 6:37 AM
    Moderator
  • projectNameTextBox code

    string[] strs = projectNameTextBox.Text.Split('\\');
                    string p = "";
                    for (int i = 0; i < strs.Length - 1; i++)
                    {
                        p += strs[i] + "\\";
                    }
                    pathTextBox.Text = p + projectNameTextBox.Text;

    ChooseComboBox_SelectionChanged code

    string str = pathTextBox.Text.Split('\\')[pathTextBox.Text.Split('\\').Length - 2];
    pathTextBox.Text = pathTextBox.Text.Replace(str, boardChooseComboBox.Text);
    
    
    

    pathTextBox sane as projectNameTextBox 

    • Edited by CY_Chen Monday, October 21, 2019 7:01 AM
    Monday, October 21, 2019 6:59 AM
  • projectNameTextBox code

    string[] strs = projectNameTextBox.Text.Split('\\');
                    string p = "";
                    for (int i = 0; i < strs.Length - 1; i++)
                    {
                        p += strs[i] + "\\";
                    }
                    pathTextBox.Text = p + projectNameTextBox.Text;

    ChooseComboBox_SelectionChanged code

    string str = pathTextBox.Text.Split('\\')[pathTextBox.Text.Split('\\').Length - 2];
    pathTextBox.Text = pathTextBox.Text.Replace(str, boardChooseComboBox.Text);

    The code is completely correct. If it is convenient, can you provide a simple project that can reproduce the problem? Via OneDrive?

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 21, 2019 7:03 AM
    Moderator
  • Below is my demo link

    https://1drv.ms/u/s!ApA8qVq_OYojiiTK7BTp4QYxyZzQ?e=vAufb1

    Monday, October 21, 2019 8:35 AM
  • Below is my demo link

    https://1drv.ms/u/s!ApA8qVq_OYojiiTK7BTp4QYxyZzQ?e=vAufb1

    Modify the code in projectNameTextBox like this.

    string[] strs = pathTextBox.Text.Split('\\');

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 21, 2019 9:01 AM
    Moderator
  • Hi Kyle

    ChooseComboBox_SelectionChanged code will wrong like below

    How to solve this?

    Thanks

    Monday, October 21, 2019 9:14 AM
  • Hi Kyle

    ChooseComboBox_SelectionChanged code will wrong like below

    How to solve this?

    Thanks

    In WPF, try the following code in "SelectionChanged".

        string str = pathTextBox.Text.Split('\\')[pathTextBox.Text.Split('\\').Length - 2];
        ComboBoxItem cbi = (ComboBoxItem)(sender as System.Windows.Controls.ComboBox).SelectedItem;
        string selectedText = cbi.Content.ToString();
        pathTextBox.Text = pathTextBox.Text.Replace(str, selectedText);

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by CY_Chen Tuesday, October 22, 2019 12:26 AM
    Monday, October 21, 2019 9:47 AM
    Moderator